激励视频广告(旧版 API)

激励视频广告是一种全屏视频广告,用户可选择使用全屏模式观看,以换取应用内奖励

本指南向您介绍如何在 Unity 应用中植入 AdMob 激励视频广告。

前提条件

完成入门指南。您的 Unity 应用应该已经导入了 Google 移动广告 Unity 插件。

获取对单实例的引用

要展示激励视频广告,首先要获取对单 RewardBasedVideoAd 实例的引用。您可以通过调用 RewardBasedVideoAd.Instance 来检索该参考。

using GoogleMobileAds.Api;
...
public class GoogleMobileAdsDemoScript : MonoBehaviour
{
    private RewardBasedVideoAd rewardBasedVideo;
    ...

    public void Start()
    {
        #if UNITY_ANDROID
            string appId = "ca-app-pub-3940256099942544~3347511713";
        #elif UNITY_IPHONE
            string appId = "ca-app-pub-3940256099942544~1458002511";
        #else
            string appId = "unexpected_platform";
        #endif

        // Initialize the Google Mobile Ads SDK.
        MobileAds.Initialize(appId);

        // Get singleton reward based video ad reference.
        this.rewardBasedVideo = RewardBasedVideoAd.Instance;

    }
}

加载广告

由于 RewardedBasedVideoAd 是一个单实例,因此应使用共享实例发出加载广告的请求。

强烈建议您尽早调用 LoadAd()(例如,在附加到 GameObject 的脚本的 Start() 方法中调用),以便可以预加载视频,如下所示。

...
using GoogleMobileAds.Api;
...
public class GoogleMobileAdsDemoScript : MonoBehaviour
{
    private RewardBasedVideoAd rewardBasedVideo;
    ...

    public void Start()
    {
        #if UNITY_ANDROID
            string appId = "ca-app-pub-3940256099942544~3347511713";
        #elif UNITY_IPHONE
            string appId = "ca-app-pub-3940256099942544~1458002511";
        #else
            string appId = "unexpected_platform";
        #endif

        // Initialize the Google Mobile Ads SDK.
        MobileAds.Initialize(appId);

        // Get singleton reward based video ad reference.
        this.rewardBasedVideo = RewardBasedVideoAd.Instance;

        this.RequestRewardBasedVideo();
    }

    private void RequestRewardBasedVideo()
    {
        #if UNITY_ANDROID
            string adUnitId = "ca-app-pub-3940256099942544/5224354917";
        #elif UNITY_IPHONE
            string adUnitId = "ca-app-pub-3940256099942544/1712485313";
        #else
            string adUnitId = "unexpected_platform";
        #endif

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

务必用测试广告进行测试

上述示例代码包含一个广告单元 ID,您可以随意使用该 ID 请求广告。该测试广告单元 ID 已经过专门配置,可为每个请求返回测试广告(而不是实际投放的广告),从而能够安全地使用。

但是,在 AdMob 界面中注册应用并创建您自己的广告单元 ID 以便在应用中使用后,您需要在开发期间明确地将您的设备配置为测试设备。这极其重要。使用真正的广告进行测试(即使您从未点按它们)会违反 AdMob 政策,可能会导致您的帐号被暂停。请参阅测试广告,了解如何确保在开发期间始终获得测试广告。

广告事件

要进一步自定义您的广告行为,您可以在广告生命周期内加入许多事件,如加载、打开、关闭等等。您可以通过为相应的 EventHandler 注册代理来监听这些事件。其中最重要的事件是 OnAdRewarded,当用户因观看视频而应获得奖励时会调用该事件。您还可以实现其他广告事件,如下所示。

...
using GoogleMobileAds.Api;
...
public class GoogleMobileAdsDemoScript : MonoBehaviour
{
    private RewardBasedVideoAd rewardBasedVideo;
    ...

    public void Start()
    {
        // Get singleton reward based video ad reference.
        this.rewardBasedVideo = RewardBasedVideoAd.Instance;

        // Called when an ad request has successfully loaded.
        rewardBasedVideo.OnAdLoaded += HandleRewardBasedVideoLoaded;
        // Called when an ad request failed to load.
        rewardBasedVideo.OnAdFailedToLoad += HandleRewardBasedVideoFailedToLoad;
        // Called when an ad is shown.
        rewardBasedVideo.OnAdOpening += HandleRewardBasedVideoOpened;
        // Called when the ad starts to play.
        rewardBasedVideo.OnAdStarted += HandleRewardBasedVideoStarted;
        // Called when the user should be rewarded for watching a video.
        rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
        // Called when the ad is closed.
        rewardBasedVideo.OnAdClosed += HandleRewardBasedVideoClosed;
        // Called when the ad click caused the user to leave the application.
        rewardBasedVideo.OnAdLeavingApplication += HandleRewardBasedVideoLeftApplication;

        this.RequestRewardBasedVideo();
    }

    private void RequestRewardBasedVideo()
    {
        #if UNITY_ANDROID
            string adUnitId = "ca-app-pub-3940256099942544/5224354917";
        #elif UNITY_IPHONE
            string adUnitId = "ca-app-pub-3940256099942544/1712485313";
        #else
            string adUnitId = "unexpected_platform";
        #endif

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

    public void HandleRewardBasedVideoLoaded(object sender, EventArgs args)
    {
        MonoBehaviour.print("HandleRewardBasedVideoLoaded event received");
    }

    public void HandleRewardBasedVideoFailedToLoad(object sender, AdFailedToLoadEventArgs args)
    {
        MonoBehaviour.print(
            "HandleRewardBasedVideoFailedToLoad event received with message: "
                             + args.Message);
    }

    public void HandleRewardBasedVideoOpened(object sender, EventArgs args)
    {
        MonoBehaviour.print("HandleRewardBasedVideoOpened event received");
    }

    public void HandleRewardBasedVideoStarted(object sender, EventArgs args)
    {
        MonoBehaviour.print("HandleRewardBasedVideoStarted event received");
    }

    public void HandleRewardBasedVideoClosed(object sender, EventArgs args)
    {
        MonoBehaviour.print("HandleRewardBasedVideoClosed event received");
    }

    public void HandleRewardBasedVideoRewarded(object sender, Reward args)
    {
        string type = args.Type;
        double amount = args.Amount;
        MonoBehaviour.print(
            "HandleRewardBasedVideoRewarded event received for "
                        + amount.ToString() + " " + type);
    }

    public void HandleRewardBasedVideoLeftApplication(object sender, EventArgs args)
    {
        MonoBehaviour.print("HandleRewardBasedVideoLeftApplication event received");
    }
}

OnAdRewarded 事件包含特殊的事件参数。该事件会传递一个 Reward 实例,其中包含的 TypeAmount 用于描述用户所获奖励:

public void HandleRewardBasedVideoRewarded(object sender, Reward args)
{
    string type = args.Type;
    double amount = args.Amount;
    print("User rewarded with: " + amount.ToString() + " " + type);
}

OnAdFailedToLoad 事件也包含了特殊的事件参数。该事件会传递一个 HandleAdFailedToLoadEventArgs 实例,其中包含了用于描述错误的 Message

public void HandleOnAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
  print("Rewarded video ad failed to load: " + args.Message);
  // Handle the ad failed to load event.
}
广告事件说明
OnAdLoaded 广告加载完成时,系统会调用 OnAdLoaded 事件。
OnAdFailedToLoad 广告加载失败时,系统会调用 OnAdFailedToLoad 事件。Message 参数描述了发生的失败类型。
OnAdOpening 在广告开始展示并铺满设备屏幕时,系统会调用此方法。如果您使用分析产品包跟踪点击,则此方法很适合记录点击。
OnAdStarted 此方法会在广告开始播放时被调用
OnAdRewarded 此方法会在用户因观看视频而应获得奖励时被调用。Reward 参数描述了要呈现给用户的奖励。
OnAdClosed 此方法会在用户点按“关闭”图标或使用“返回”按钮关闭激励视频广告时被调用。如果您的应用暂停了音频输出或游戏循环,则非常适合使用此方法恢复这些活动。
OnAdLeavingApplication 用户点击打开其他应用(例如,Google Play 商店)时,系统会先调用 OnAdOpened,再调用此方法,从而在后台运行当前应用。

展示广告

在向用户展示激励广告之前,必须为用户提供明确的选项,让其可以自行选择是否通过观看激励广告内容来换取奖励。 激励广告必须始终是一项可由用户自行选择的体验。

要展示激励视频广告,请使用 IsLoaded() 方法验证广告是否已完成加载,然后再调用 Show()

以下代码段演示了如何展示激励视频广告。

private void UserOptToWatchAd()
{
  if (rewardBasedVideo.IsLoaded()) {
    rewardBasedVideo.Show();
  }
}

重新加载广告

在展示完上一个激励视频广告后,您可以非常方便地使用 OnAdClosed 事件加载新的激励视频广告:

public void Start()
{
    this.rewardBasedVideo = RewardBasedVideoAd.Instance;

    rewardBasedVideo.OnAdClosed += HandleRewardBasedVideoClosed;
}

public void HandleRewardBasedVideoClosed(object sender, EventArgs args)
{
    this.RequestRewardBasedVideo();
}

其他资源

示例

后续步骤