跳到主要内容

集成指南

简介

      在接入广告之前,首先需要完成SDK的初始化。

      MG Ads 支持【全屏1920*1080】【退屏】【横幅728*90】【插屏1024*768】【对联300*600】【激励视频1024*768】

全屏广告

全屏广告位一般在页面的load方法中,在SDK初始化完成事件中实现。


Platform::String^ MainPage::FullScreenAd()//MG FullScreenAd
{
try
{
//“XXXXXXXX”参数需要传入广告key,广告key 来自MG广告后台创建。
auto initTask = Concurrency::create_task(MiracleGamesAd::AdvertisingManager::ShowAd("XXXXXXXX", MiracleGamesAd::Models::AdType::FullScreen));
initTask.then([](MiracleGamesAd::Services::Core::Common::AsyncProcessResult^ result)
{
if (result->ReturnValue)//关闭广告时触发广告关闭事件
{

}
});
}
catch (...){}//添加异常处理机制,预防游戏崩溃.
return "";
}

退屏广告

      退屏广告是在退出游戏时触发,为了保证退出游戏时广告的弹出率,MG建议在初始化完成后首先将退屏广告的信息加载到内存中,在退出游戏时,SDK会自动直接打开退屏广告


//“XXXXXXXX”参数需要传入广告key,广告key来自MG广告后台创建。
try
{
MiracleGamesAd::AdvertisingManager::ShowExitAdBlocking(“XXXXXXXX”);
}
catch (...){}//添加异常处理机制,预防游戏崩溃.

接入退屏广告代码之后,还需要进行以下设置。

1.首先将主工程的Properties->Application页签中Target version改为19041(或者高于此版本)

2.右键Package.appxmanifest,选择Xml编辑器打开编辑

3.在Package标签中增加xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities",并在IgnorableNamespaces中补充rescap

4.在Capabilities节点下,增加这一行配置代码 <rescap:Capability Name="confirmAppClose"/>

5.保存即可

横幅广告


Platform::String ^MainPage::MainScreen_Binner()
{
try
{
auto opt = ref new MiracleGamesAd::Models::BannerAdSettingOptions();//设置广告的一些配置参数,未设置时使用默认状态
opt->MediaType = "image";//设置广告类型图片="image",网页="web"
//控制展示广告的位置
opt->HorizontalAlignment = Windows::UI::Xaml::HorizontalAlignment::Center;
opt->VerticalAlignment = Windows::UI::Xaml::VerticalAlignment::Bottom;
//“XXXXXXXX”参数需要传入广告key,广告key 来自MG后台创建。
auto initTask = Concurrency::create_task(MiracleGamesAd::AdvertisingManager::ShowAd(“XXXXXXXX”, MiracleGamesAd::Models::AdType::Banner, opt));
initTask.then([](MiracleGamesAd::Services::Core::Common::AsyncProcessResult^ result)
{
if (result->ReturnValue)//关闭广告时触发广告关闭事件
{

}
});
}
catch (...){}//添加异常处理机制,预防游戏崩溃.
return "";
}

插屏广告


Platform::String^ MainPage::MainScreen_TableAD()
{
try
{
auto opt = ref new MiracleGamesAd::Models::InterstitialAdSettingOptions();//设置广告的一些配置参数,未设置时使用默认状态
opt->MediaType = "image";//设置广告类型图片="image",网页="web",视频="video"
auto initTask = Concurrency::create_task(MiracleGamesAd::AdvertisingManager::ShowAd(“XXXXXXXX”, MiracleGamesAd::Models::AdType::Interstitial, opt));
initTask.then([](MiracleGamesAd::Services::Core::Common::AsyncProcessResult^ result)
{
if (result->ReturnValue)//关闭广告时触发广告关闭事件
{

}
});
}
catch (...){}//添加异常处理机制,预防游戏崩溃.
return "";
}

对联广告


Platform::String ^ ::MainPage::MainScreen_Couplet()
{
try
{
auto opt = ref new MiracleGamesAd::Models::CoupletAdSettingOptions();//设置广告的一些配置参数,未设置时使用默认状态
opt->MediaType = "image";//设置广告类型图片="image",网页="web"
//“XXXXXXXX”参数需要传入广告key,广告key 来自MG后台创建。
auto initTask = Concurrency::create_task(MiracleGamesAd::AdvertisingManager::ShowAd(“XXXXXXXX”, MiracleGamesAd::Models::AdType::Couplet, opt));
initTask.then([](MiracleGamesAd::Services::Core::Common::AsyncProcessResult^ result)
{
if (result->ReturnValue)//关闭广告时触发广告关闭事件
{

}
});
}
catch (...){}//添加异常处理机制,预防游戏崩溃.
return "";
}

激励视频

void ShowAdReword() {
auto opt = ref new MiracleGamesAd::Models::RewardAdSettingOptions();
opt->MediaType = "image";//设置广告类型图片="image",网页="web",视频="video"
opt->Comment = "{\"coin\":100}";//开发者自定义参数
//“XXXXXXXX”参数需要传入广告key,广告key 来自MG后台创建。
auto initTask = Concurrency::create_task(MiracleGamesAd::AdvertisingManager::ShowAd("XXXXXXXX", MiracleGamesAd::Models::AdType::Reward, opt));
initTask.then([](MiracleGamesAd::Services::Core::Common::AsyncProcessResult^ result)
{
auto completeState = dynamic_cast<MiracleGamesAd::Models::RewardAdCompleteState^>(result->Tag);
if (completeState != nullptr)
{
if (completeState->IsCompleted)
{
auto comment = completeState->Comment;//开发者自定义参数
MiracleGamesAd::AdvertisingManager::ReportAdRewardFulfillment(completeState->RewardId);
}
}
});
}

广告预加载功能

      预加载功能包含两个独立接口:预加载接口 和 展示预加载广告接口。您可以在需要展示广告之前,提前调用 PreloadAd 接口请求广告素材,待广告准备就绪后再通过 ShowPreloadAd 接口进行展示。
      预加载功能适用于所有广告类型。

预加载接口

Platform::String^ MainPage::PreloadAd()//示例:预加载插屏广告
{
//参数1:广告位ID
//参数2:广告位类型
//参数3:设置广告类型,图片="image",网页="web",视频="video"
try
{
auto initTask = Concurrency::create_task(MiracleGamesAd::AdvertisingManager::PreloadAd("xxxxxxxx", MiracleGamesAd::Models::AdType::Interstitial, "image"));//开始预加载广告
initTask.then([](MiracleGamesAd::Services::Core::Common::AsyncProcessResult^ result)
{
if (result->ReturnValue)
{
//广告预加载成功
}
else
{
//广告预加载失败
}
});
}
catch (...) {}//添加异常处理机制,预防游戏崩溃.
return "";
}

展示预加载广告接口

Platform::String^ MainPage::ShowMGPreloadAd//示例:展示预加载成功的插屏广告,调用前请确保对应的广告位已经预缓存成功。
{
//参数1:广告位ID
//参数2:广告位类型
try
{
auto initTask = Concurrency::create_task(MiracleGamesAd::AdvertisingManager::ShowPreloadAd("xxxxxxxx", MiracleGamesAd::Models::AdType::Interstitial));
initTask.then([](MiracleGamesAd::Services::Core::Common::AsyncProcessResult^ result)
{
if (result->ReturnValue)//关闭广告时触发广告关闭事件
{

}
});
}
catch (...) {}//添加异常处理机制,预防游戏崩溃.
return "";
}