插页式激励广告(Beta 版)

插页式激励广告是一种 一种激励性广告格式,可让您为每次广告展示提供奖励 自动应用。与激励广告不同,用户 用户才能选择观看插页式激励广告。

前提条件

始终使用测试广告进行测试

以下示例代码包含一个广告单元 ID,可用于请求 测试广告。该测试广告单元已经过专门配置 会返回测试广告 制作出来的广告,确保安全使用。

不过,当您在 在 Ad Manager 网页界面中创建了自己的广告单元 要在您的应用中使用的 ID,请明确将设备配置为测试 设备 开发。

/6499/example/rewarded-interstitial

初始化移动广告 SDK

加载广告之前,请先调用 MobileAds.Initialize().此操作仅需执行一次,最好是在应用启动时执行。

using GoogleMobileAds;
using GoogleMobileAds.Api;

public class GoogleMobileAdsDemoScript : MonoBehaviour
{
    public void Start()
    {
        // Initialize the Google Mobile Ads SDK.
        MobileAds.Initialize((InitializationStatus initStatus) =>
        {
            // This callback is called once the MobileAds SDK is initialized.
        });
    }
}

如果您使用的是中介功能,请等到回调发生后再按 这样可确保初始化所有中介适配器

实现

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

  1. 加载插页式激励广告
  2. [可选] 验证服务器端验证 (SSV) 回调
  3. 通过奖励回调展示插页式激励广告
  4. 监听插页式激励广告事件
  5. 清理插页式激励广告
  6. 预加载下一个插页式激励广告

加载插页式激励广告

插页式激励广告加载是使用静态 Load() 完成的 (针对 RewardedInterstitialAd 类)。该加载方法需要一个广告 单元 ID、AdManagerAdRequest 对象和完成 处理程序将在广告加载成功或失败时调用。加载的 RewardedInterstitialAd 对象作为补全中的参数提供 处理程序。以下示例展示了如何加载 RewardedInterstitialAd


  // This ad unit is configured to always serve test ads.
  private string _adUnitId = "/6499/example/rewarded-interstitial";

  private RewardedInterstitialAd _rewardedInterstitialAd;

  /// <summary>
  /// Loads the rewarded interstitial ad.
  /// </summary>
  public void LoadRewardedInterstitialAd()
  {
      // Clean up the old ad before loading a new one.
      if (_rewardedInterstitialAd != null)
      {
            _rewardedInterstitialAd.Destroy();
            _rewardedInterstitialAd = null;
      }

      Debug.Log("Loading the rewarded interstitial ad.");

      // create our request used to load the ad.
      var adRequest = new AdManagerAdRequest();
      adRequest.Keywords.Add("unity-admob-sample");

      // send the request to load the ad.
      RewardedInterstitialAd.Load(_adUnitId, adRequest,
          (RewardedInterstitialAd ad, LoadAdError error) =>
          {
              // if error is not null, the load request failed.
              if (error != null || ad == null)
              {
                  Debug.LogError("rewarded interstitial ad failed to load an ad " +
                                 "with error : " + error);
                  return;
              }

              Debug.Log("Rewarded interstitial ad loaded with response : "
                        + ad.GetResponseInfo());

              _rewardedInterstitialAd = ad;
          });
  }

[可选] 验证服务器端验证 (SSV) 回调

服务器端验证中需要额外数据的应用 回调应使用插页式激励广告的自定义数据功能。 在激励广告对象上设置的任何字符串值都会传递给 custom_data 查询参数。如果未设置自定义数据值, custom_data 查询参数值将不会包含在 SSV 回调中。

以下代码示例演示了如何在 插页式激励广告加载完毕。

// send the request to load the ad.
RewardedInterstitialAd.Load(_adUnitId,
                            adRequest,
                            (RewardedInterstitialAd ad, LoadAdError error) =>
    {
        // If the operation failed, an error is returned.
        if (error != null || ad == null)
        {
            Debug.LogError("Rewarded interstitial ad failed to load an ad " +
                           " with error : " + error);
            return;
        }

        // If the operation completed successfully, no error is returned.
        Debug.Log("Rewarded interstitial ad loaded with response : " +
                   ad.GetResponseInfo());
        
        // Create and pass the SSV options to the rewarded ad.
        var options = new ServerSideVerificationOptions
                              .Builder()
                              .SetCustomData("SAMPLE_CUSTOM_DATA_STRING")
                              .Build()
        ad.SetServerSideVerificationOptions(options);
        
});

如果您要设置自定义奖励字符串,则必须在显示 。

通过奖励回调展示插页式激励广告

展示广告时,您必须提供一个回调来处理奖励 用户。每次加载时,广告仅可展示一次。使用 CanShowAd() 方法 确认广告已做好展示准备。

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

public void ShowRewardedInterstitialAd()
{
    const string rewardMsg =
        "Rewarded interstitial ad rewarded the user. Type: {0}, amount: {1}.";

    if (rewardedInterstitialAd != null && rewardedInterstitialAd.CanShowAd())
    {
        rewardedInterstitialAd.Show((Reward reward) =>
        {
            // TODO: Reward the user.
            Debug.Log(String.Format(rewardMsg, reward.Type, reward.Amount));
        });
    }
}

监听插页式激励广告事件

要进一步自定义您的广告行为,您可以加入许多 触发这些事件通过注册代理来监听这些事件 如下所示。

private void RegisterEventHandlers(RewardedInterstitialAd ad)
{
    // Raised when the ad is estimated to have earned money.
    ad.OnAdPaid += (AdValue adValue) =>
    {
        Debug.Log(String.Format("Rewarded interstitial ad paid {0} {1}.",
            adValue.Value,
            adValue.CurrencyCode));
    };
    // Raised when an impression is recorded for an ad.
    ad.OnAdImpressionRecorded += () =>
    {
        Debug.Log("Rewarded interstitial ad recorded an impression.");
    };
    // Raised when a click is recorded for an ad.
    ad.OnAdClicked += () =>
    {
        Debug.Log("Rewarded interstitial ad was clicked.");
    };
    // Raised when an ad opened full screen content.
    ad.OnAdFullScreenContentOpened += () =>
    {
        Debug.Log("Rewarded interstitial ad full screen content opened.");
    };
    // Raised when the ad closed full screen content.
    ad.OnAdFullScreenContentClosed += () =>
    {
        Debug.Log("Rewarded interstitial ad full screen content closed.");
    };
    // Raised when the ad failed to open full screen content.
    ad.OnAdFullScreenContentFailed += (AdError error) =>
    {
        Debug.LogError("Rewarded interstitial ad failed to open " +
                       "full screen content with error : " + error);
    };
}

清理插页式激励广告

使用完 RewardedInterstitialAd 后,请务必调用 Destroy() 方法,然后再放弃对它的引用:

_rewardedInterstitialAd.Destroy();

这会通知插件该对象已不再使用,以及 可收回。此方法调用失败会导致内存泄漏。

预加载下一个插页式激励广告

RewardedInterstitialAd 是一次性对象。也就是说,每次 展示插页式广告时,您将无法再次使用该对象。申请新的 插页式激励广告,您需要加载新的 RewardedInterstitialAd 对象。

若要为下一次展示机会准备好插页式激励广告, OnAdFullScreenContentClosed 或 引发了 OnAdFullScreenContentFailed 广告事件。

private void RegisterReloadHandler(RewardedInterstitialAd ad)
{
    // Raised when the ad closed full screen content.
    ad.OnAdFullScreenContentClosed += ()
    {
        Debug.Log("Rewarded interstitial ad full screen content closed.");

        // Reload the ad so that we can show another as soon as possible.
        LoadRewardedInterstitialAd();
    };
    // Raised when the ad failed to open full screen content.
    ad.OnAdFullScreenContentFailed += (AdError error) =>
    {
        Debug.LogError("Rewarded interstitial ad failed to open " +
                       "full screen content with error : " + error);

        // Reload the ad so that we can show another as soon as possible.
        LoadRewardedInterstitialAd();
    };
}

其他资源