Skip to main content

Ad Integration Guide

Introduction

Before integrating ads, the SDK initialization must be completed first.

MG Ads supports 【Splash Ad 19201080】【Exit Ad】【Banner 72890】【Interstitial 1024768】【Couplet 300600】【Rewarded 1024*768】【CustomAd】

Splash Ad

The splash ad slot is typically implemented in the page's load method, within the SDK initialization completion event.

public async void SplashAd()
{
//"XXXXXXXX" parameter needs to be passed with the ad key, which comes from the MG ad backend.
var ad = await AdvertisingManager.ShowAd("XXXXXXXX", AdType.FullScreen);
if (ad.ReturnValue)//Triggers ad close event when ad is closed
{

}
}

Exit Ad

The exit ad is triggered when exiting the game. To ensure the pop-up rate of the ad upon game exit, MG recommends loading the exit screen ad information into memory after initialization is complete. When exiting the game, the SDK will automatically open the exit screen ad directly.

The code to load the exit ad is as follows:

//"XXXXXXXX" parameter needs to be passed with the ad key, which comes from the MG ad backend.
AdvertisingManager.SetupExitunitId("XXXXXXXX");

After integrating the exit ad code, the following settings are also required.

  1. First, change the Target version on the Properties->Application tab of the main project to 19041 (or a higher version).

  2. Right-click Package.appxmanifest and choose to open and edit it with the Xml editor.

  3. In the Package tag, add xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities", and supplement rescap in IgnorableNamespaces.

  4. Under the Capabilities node, add this line.

  5. Save.

public async void ShowBannerAdImage()
{
var opt = new BannerAdSettingOptions();//Set some configuration parameters for the ad; defaults are used when not set.
opt.MediaType = "image";//Set ad type: image="image", web="web".
//Control the position where the ad is displayed.
opt.VerticalAlignment = VerticalAlignment.Center;
opt.HorizontalAlignment = HorizontalAlignment.Center;
//"XXXXXXXX" parameter needs to be passed with the ad key, which comes from the MG ad backend.
var bannerAd = await AdvertisingManager.ShowAd("XXXXXXXX", AdType.Banner, opt);
if (bannerAd.ReturnValue)//Triggers ad close event when ad is closed.
{

}
}

Interstitial Ad

public async void ShowInterstitialAdDefault()
{
var opt = new InterstitialAdSettingOptions();//Set some configuration parameters for the ad; defaults are used when not set.
opt.MediaType = "image";//Set ad type: image="image", web="web", video="video".
//"XXXXXXXX" parameter needs to be passed with the ad key, which comes from the MG ad backend.
var interstitialAd = await AdvertisingManager.ShowAd("XXXXXXXX", AdType.Interstitial,opt);
if (interstitialAd.ReturnValue)//Triggers ad close event when ad is closed.
{

}
}

Couplet Ad

public async void ShowCoupletAdDefault()
{
var opt = new CoupletAdSettingOptions();//Set some configuration parameters for the ad; defaults are used when not set.
opt.MediaType = "image";//Set ad type: image="image", web="web".
//"XXXXXXXX" parameter needs to be passed with the ad key, which comes from the MG ad backend.
var coupletAd = await AdvertisingManager.ShowAd("XXXXXXXX", AdType.Couplet,opt);
if (coupletAd.ReturnValue)//Triggers ad close event when ad is closed.
{

}
}

Rewarded

public async void ShowRewardAd()
{
var json = "{\"coin\":100}";
var rewardAd = await AdvertisingManager.ShowAd(RewardAdUnitId,
AdType.Reward,
new RewardAdSettingOptions
{
MediaType = "video",//Set ad type: image="image", web="web", video="video".
Comment = WebUtility.UrlEncode(json),//Developer-defined parameter.
CallbackId = ""//Callback address, can be empty.
});
if (rewardAd.Tag is RewardAdCompleteState completeState)
{
if (completeState.IsCompleted)
{
//Game logic issues reward, then report fulfillment.
var comment = WebUtility.UrlDecode(completeState.Comment);
await AdvertisingManager.ReportAdRewardFulfillment(completeState.RewardId);
}
}
}

CustomAd

Custom ads require the developer to create and maintain a control, and then pass the control instance to the SDK.

public async void ShowCustomAdDefault()
{
//The supported developer control types that can be passed in are Panel, ContentControl, UserControl, and their derived classes.
var adSettingOptions = new CustomAdSettingOptions
{
Container = CustomContainer
};
//"XXXXXXXX" parameter needs to be passed with the ad key, which comes from the MG ad backend.
var feed = await AdvertisingManager.ShowAd("XXXXXXXX", AdType.Custom, adSettingOptions);
}

Ad Preloading Feature

The preloading feature includes two independent interfaces: the preload interface and the show preloaded ad interface. You can call the PreloadAd interface in advance to request ad assets before displaying ads, and then use the ShowPreloadAd interface to display them once they are ready.
The preloading feature applies to all ad types.

Preload Interface

private void PreloadInterstitialAd()//Example: preload interstitial ad
{
//Parameter 1: Ad unit ID
//Parameter 2: Ad type
//Parameter 3: Ad content type, image="image", web="web", video="video"
var interstitialAd = await AdvertisingManager.PreloadAd("XXXXXXXX", AdType.Interstitial, "image");
if (interstitialAd.ReturnValue)
{
var messageDialog = new Windows.UI.Popups.MessageDialog("Interstitial ad preloading completed", "Success");
await messageDialog.ShowAsync();
}
else
{
var messageDialog1 = new Windows.UI.Popups.MessageDialog("Interstitial ad preloading failed", "Failure");
await messageDialog1.ShowAsync();
}
}

Show Preloaded Ad Interface

public async void ShowPreloadInterstitialAd()//Example: show successfully preloaded interstitial ad, Before doing so, please ensure that the corresponding advertisement slot has been pre-cached successfully.
{
//Parameter 1: Ad unit ID
//Parameter 2: Ad type
var interstitialAd = await AdvertisingManager.ShowPreloadAd("XXXXXXXX", AdType.Interstitial);
if (interstitialAd.ReturnValue)
{
var messageDialog = new Windows.UI.Popups.MessageDialog("Interstitial ad display completed", "Success");
await messageDialog.ShowAsync();
}
else
{
var messageDialog1 = new Windows.UI.Popups.MessageDialog("Interstitial ad display failed", "Failure");
await messageDialog1.ShowAsync();
}
}