插页式激励广告

插页式激励广告是一种激励用户的广告格式,采用这种格式时,您可以通过在应用中的自然过渡点自动展示的广告向用户提供奖励。与激励广告不同,用户无需自行选择即可观看插页式激励广告。

前提条件

  • Unity 插件 5.4.0 或更高版本。

植入

植入插页式激励广告的主要步骤如下所示:

  1. 加载广告。
  2. 展示广告并处理奖励事件。
  3. 使用广告事件回调

加载广告

广告的加载是通过针对 RewardedInterstitialAd 类使用静态 LoadAd() 方法完成的。load 方法需要使用广告单元 ID、AdRequest 对象以及在广告加载成功或失败时调用的完成处理程序。已加载的 RewardedInterstitialAd 对象以完成处理程序中的参数的形式提供。以下示例展示了如何加载 RewardedInterstitialAd

...
public class GoogleMobileAdsDemoScript : MonoBehaviour
{
    private RewardedInterstitialAd rewardedInterstitialAd;
    ...

    public void Start()
    {
        ...

        // Create an empty ad request.
        AdRequest request = new AdRequest.Builder().Build();
        // Load the rewarded ad with the request.
        RewardedInterstitialAd.LoadAd(adUnitId, request, adLoadCallback);
    }

    private void adLoadCallback(RewardedInterstitialAd ad, string error)
    {
        if (error == null)
        {
            rewardedInterstitialAd = ad;
        }
    }
}

展示广告并处理奖励事件

展示广告时,您必须提供一个回调,用于处理用户奖励。

以下代码演示了展示插页式激励广告的最佳方法:

public void ShowRewardedInterstitialAd()
{
    if (rewardedInterstitialAd != null)
    {
        rewardedInterstitialAd.Show(userEarnedRewardCallback);
    }
}

private void userEarnedRewardCallback(Reward reward)
{
    // TODO: Reward the user.
}

使用广告事件回调

要进一步自定义您广告的行为,您可以加入许多事件。您可以通过为相应的事件处理脚本注册代理来监听这些事件。

...
public class GoogleMobileAdsDemoScript : MonoBehaviour
{
    private RewardedInterstitialAd rewardedInterstitialAd;
    ...

    public void Start()
    {
        ...

        // Create an empty ad request.
        AdRequest request = new AdRequest.Builder().Build();
        // Load the rewarded ad with the request.
        RewardedInterstitialAd.LoadAd(adUnitId, request, adLoadCallback);
    }

    private void adLoadCallback(RewardedInterstitialAd ad, string error)
    {
        if (error == null)
        {
            rewardedInterstitialAd = ad;

            rewardedInterstitialAd.OnAdFailedToPresentFullScreenContent += HandleAdFailedToPresent;
            rewardedInterstitialAd.OnAdDidPresentFullScreenContent += HandleAdDidPresent;
            rewardedInterstitialAd.OnAdDidDismissFullScreenContent += HandleAdDidDismiss;
            rewardedInterstitialAd.OnPaidEvent += HandlePaidEvent;
        }
    }

    private void HandleAdFailedToPresent(object sender, AdErrorEventArgs args)
    {
        MonoBehavior.print("Rewarded interstitial ad has failed to present.");
    }

    private void HandleAdDidPresent(object sender, EventArgs args)
    {
        MonoBehavior.print("Rewarded interstitial ad has presented.");
    }

    private void HandleAdDidDismiss(object sender, EventArgs args)
    {
        MonoBehavior.print("Rewarded interstitial ad has dismissed presentation.");
    }

    private void HandlePaidEvent(object sender, AdValueEventArgs args)
    {
        MonoBehaviour.print(
            "Rewarded interstitial ad has received a paid event.");
    }
}