How To by example, Step by step guide to show full screen Interstitial Ads from ANY class you see fit to trigger them from.
If you haven’t already downloaded the Microsoft Advertising SDK and installed it, do it now.
I’m using Visual Studio 2017 and latest Monogame Dev as of 7/30/18
This example is from scratch so if you want to add this to a game you already made you might need to copy your code over to the “Monogame Windows 10 Universal (XAML)” build, if anyone figures out how to use this in the regular build please post it here too.
Ok, so create a new “Monogame Windows 10 Universal (XAML)” project, right click References, select Add Reference then Universal Windows->Extensions and select “Microsoft Advertising SDK for XAML” then click ok.
open up GamePage.xaml.cs
add this line at the top
"using Microsoft.Advertising.WinRT.UI;"
scroll down to the class select the whole class and replace it with this, or cut and paste how ever you wish
public sealed partial class GamePage : Page
{
private readonly Game1 _game;
// a static property that returns the current instance of the game page
public static GamePage Current { get; set; }
// setup the ad variables
public InterstitialAd interstitialAd = null;
//// TEST APP ID USE YOUR OWN FROM MICROSOFT DEVELOPER
private string myAppId = "d25517cb-12d4-4699-8bdc-52040c712cab";
//// TEST UNIT ID AGAIN FROM MICROSOFT DEVELOPER
private string myAdUnitId = "test";
//// variables accessed from anywhere via "GamePage.Current.xxxVariablexxx"
public bool adReady;
public bool adTrigger;
public AdType adType;
public bool reward;
public Task adTask;
public GamePage()
{
InitializeComponent();
// Create the game.
Current = this;
string launchArguments = string.Empty;
_game = MonoGame.Framework.XamlGame<Game1>.Create(launchArguments,Window.Current.CoreWindow,swapChainPanel);
// This section sets up the ad load and display
adType = AdType.Display;
interstitialAd = new InterstitialAd();
interstitialAd.AdReady += InterstitialAd_AdReady;
interstitialAd.ErrorOccurred += InterstitialAd_ErrorOccurred;
interstitialAd.Completed += InterstitialAd_Completed;
interstitialAd.Cancelled += InterstitialAd_Cancelled;
interstitialAd.RequestAd(adType,myAppId,myAdUnitId); // pre load an Ad
adReady = false; // reset Variables
adTrigger = false;
reward = false;
}
///*********************************************************************************************
// * Function Name : AdReady
// * Description : Triggered when the Ad is reaady to be displayed
// * ******************************************************************************************/
public async void InterstitialAd_AdReady(object sender,object e)
{
adReady = true;
// now go off and wait until we can come back
await Task.Run(() =>
{
while(true)
{
if(interstitialAd.State == InterstitialAdState.Ready)
{
if(adTrigger == true)
{
adReady = false;
break;
}
}
}
});
interstitialAd.Show(); }
///*********************************************************************************************
// * Function Name : Canceled
// * Description : User cancelled the ad
// * ******************************************************************************************/
private void InterstitialAd_Cancelled(object sender,object e)
{
interstitialAd.RequestAd(adType,myAppId,myAdUnitId); // ad was canceled load next
reward = false;
adTrigger = false;
adReady = false;
}
///*********************************************************************************************
// * Function Name : Completed
// * Description : triggered if the ad was left to run till the end
// * ******************************************************************************************/
private void InterstitialAd_Completed(object sender,object e)
{
interstitialAd.RequestAd(adType,myAppId,myAdUnitId); // ad finished
reward = true; // reward player for watching
adTrigger = false;
adReady = false;
}
///*********************************************************************************************
// * Function Name : ErrorOccurred
// * Description : could not fetch the ad or something bad happened
// * ******************************************************************************************/
private void InterstitialAd_ErrorOccurred(object sender,AdErrorEventArgs e)
{
// handle an error in here log it or what ever you want then just load (attempt) the next ad
interstitialAd.RequestAd(adType,myAppId,myAdUnitId); // ad was canceled load next
reward = false;
adTrigger = false;
adReady = false;
}
}
That’s it for this page, the next part can be placed in any class, for demo purposes i’ve just added it Game1.cs
I added a font just fyi and I do the call from inside a draw, doesn’t matter where the trigger is set any who.
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
private double waitTime=0; // wait 3 seconds bewteen ads
Random rnd = new Random(); // randomly show Display or Video ads
int count = 0;
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
spriteBatch.Begin();
// draw routines
if(waitTime < gameTime.TotalGameTime.TotalMilliseconds) // waiting 3 seconds for ad
{
if((rnd.Next() & 1) == 0)
{
GamePage.Current.adType = AdType.Video; // ad to come AFTER the next ad, preload already happened
}
else
{
GamePage.Current.adType = AdType.Display; // ad to come AFTER the next ad, preload already happened
}
ShowTheAd(); // show the ad
waitTime = gameTime.TotalGameTime.TotalMilliseconds + 5000; // reset the wait clock
}
else
spriteBatch.DrawString(font,"Waiting for an Ad and Testing game Pause Frame #="+count++,new Vector2(100,300),Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
///*********************************************************************************************
// * Function Name : ShowTheAd
// * Description : this function will show the ad when i call for it
// * ******************************************************************************************/
if(GamePage.Current.interstitialAd != null)
{
if(GamePage.Current.interstitialAd.State == InterstitialAdState.Ready)
{
GamePage.Current.adTrigger = true;
}
while(true) // stop here until the ad is finished
{
if(GamePage.Current.interstitialAd.State == InterstitialAdState.Ready)
{
if(!GamePage.Current.adTrigger)
{
break;
}
}
};
}
That’s it, the ShowTheAd() call can go anywhere you want.
Enjoy
Paul