插页式激励广告是一种激励广告格式,可让您在应用中的自然过渡点自动展示广告,并向观看广告的用户提供奖励。与激励广告不同,用户无需自行选择即可观看插页式激励广告。
前提条件
在继续操作之前,请设置 Google Mobile Ads Unity Plugin。
务必用测试广告进行测试
以下示例代码包含一个广告单元 ID,可供您用来请求测试广告。该 ID 已经过专门配置,可针对每个请求返回测试广告(而非实际投放的广告),因此能够安全地使用。
不过,您在 AdMob 网页界面中注册应用,并创建要在应用中使用的广告单元 ID 后,需要在开发期间明确将您的设备配置为测试设备。
Android
ca-app-pub-3940256099942544/1033173712
iOS
ca-app-pub-3940256099942544/4411468910
初始化 Google Mobile Ads Unity Plugin
加载广告之前,请先调用 MobileAds.Initialize(),以便让应用初始化 Google Mobile Ads Unity Plugin。此操作仅需执行一次,最好是在应用启动时执行。
using GoogleMobileAds;
using GoogleMobileAds.Api;
public class GoogleMobileAdsDemoScript : MonoBehaviour
{
public void Start()
{
// Initialize Google Mobile Ads Unity Plugin.
MobileAds.Initialize((InitializationStatus initStatus) =>
{
// This callback is called once the MobileAds SDK is initialized.
});
}
}
如果您使用的是中介功能,请等到发生回调后再加载广告,因为这可确保所有中介适配器都能得到初始化。
加载插页式激励广告
若要加载插页式激励广告,请使用 RewardedInterstitialAd 类中的静态 Load 方法。所加载的 RewardedInterstitialAd 对象会作为完成处理程序中的一个参数来提供。下例加载了一个插页式激励广告:
请将 AD_UNIT_ID 替换为您的广告单元 ID。
[可选] 验证服务器端验证 (SSV) 回调
如果应用需要在服务器端验证回调中包含额外的数据,请使用插页式激励广告的自定义数据功能。在插页式激励广告对象中设置的任何字符串值都将传递给 SSV 回调的 custom_data 查询参数。如果未设置自定义数据值,custom_data 查询参数值不会包含在 SSV 回调中。
以下代码示例演示了如何在加载插页式激励广告后设置 SSV 选项:
请将 SAMPLE_CUSTOM_DATA_STRING 替换为自定义数据。
如果您要设置自定义奖励字符串,则必须在展示广告之前设置。
通过奖励回调展示插页式激励广告
展示广告时,您必须提供一个回调,用于处理给用户的奖励。每次加载时,广告仅可展示一次。您可以使用 CanShowAd 方法验证广告是否已做好展示准备。
以下代码展示了一个插页式激励广告:
if (rewardedInterstitialAd != null && rewardedInterstitialAd.CanShowAd())
{
rewardedInterstitialAd.Show((Reward reward) =>
{
// The ad was shown and the user earned a reward.
Debug.Log($"Rewarded interstitial ad rewarded user: {reward.Type}, {reward.Amount}");
});
}
监听插页式激励广告事件
若要进一步自定义广告行为,您可以将代码与广告生命周期中的一些事件挂钩。以下示例就监听了一些广告事件:
rewardedInterstitialAd.OnAdPaid += (AdValue adValue) =>
{
// Raised when the ad is estimated to have earned money.
Debug.Log($"Rewarded interstitial ad paid {adValue.Value} {adValue.CurrencyCode}.");
};
rewardedInterstitialAd.OnAdImpressionRecorded += () =>
{
// Raised when an impression is recorded for an ad.
Debug.Log("Rewarded interstitial ad recorded an impression.");
};
rewardedInterstitialAd.OnAdClicked += () =>
{
// Raised when a click is recorded for an ad.
Debug.Log("Rewarded interstitial ad was clicked.");
};
rewardedInterstitialAd.OnAdFullScreenContentOpened += () =>
{
// Raised when the ad opened full screen content.
Debug.Log("Rewarded interstitial ad full screen content opened.");
};
rewardedInterstitialAd.OnAdFullScreenContentClosed += () =>
{
// Raised when the ad closed full screen content.
Debug.Log("Rewarded interstitial ad full screen content closed.");
};
rewardedInterstitialAd.OnAdFullScreenContentFailed += (AdError error) =>
{
// Raised when the ad failed to open full screen content.
Debug.LogError($"Rewarded interstitial ad failed to open full screen content: {error}");
};
清理插页式激励广告
使用完 RewardedInterstitialAd 对象后,请务必先调用 Destroy 方法,然后再删除对该对象的引用:
if (rewardedInterstitialAd != null)
{
rewardedInterstitialAd.Destroy();
}
这会通知插件已不再使用该对象,可回收它占用的内存。如果不调用此方法,将导致内存泄漏。
预加载下一个插页式激励广告
RewardedInterstitialAd 对象是一次性对象。插页式激励广告一经展示就无法再使用该对象了。若要再请求一个插页式激励广告,请加载一个新的 RewardedInterstitialAd 对象。
若要为下一次展示机会准备好插页式激励广告,请在 OnAdFullScreenContentClosed 或 OnAdFullScreenContentFailed 广告事件引发后预加载插页式激励广告:
请将 AD_UNIT_ID 替换为您的广告单元 ID。
其他资源
- HelloWorld 示例:所有广告格式的极简植入方案。