激勵廣告

獎勵廣告是用戶可以選擇與之互動以換取應用內獎勵的廣告。本指南向您展示瞭如何將來自 AdMob 的激勵廣告集成到 Unity 應用程序中。閱讀一些客戶的成功案例:案例1案例2

先決條件

  • Unity 插件 3.16.0 或更高版本。
  • 完整的使用入門。您的 Unity 應用程序應該已經導入了 Google 移動廣告 Unity 插件。

創建激勵廣告對象

獎勵廣告請求和顯示RewardedAd對象。以顯示獎勵廣告所需的第一個步驟是實例化一個RewardedAd通過與廣告單元ID調用該對象的構造被用來加載廣告。下面的代碼片段演示了這一點:

對於使用Unity編輯器更容易ADS整合功能,嘗試新的廣告展示位置測試版

using UnityEngine.Events;
using UnityEngine;
using GoogleMobileAds.Api;
using GoogleMobileAds.Common;
using UnityEngine.UI;
using System;
using System.Collections.Generic;

public class GoogleMobileAdsDemoScript : MonoBehaviour
{
    private RewardedAd rewardedAd;
    ...

    public void Start()
    {
        ...

        this.rewardedAd = new RewardedAd(adUnitId);
    }
}

始終使用測試廣告進行測試

在構建和測試您的應用程序時,請確保您使用的是測試廣告,而不是實況廣告。否則可能會導致您的帳戶被暫停。

加載測試廣告的最簡單方法是為 Android 和 iOS 激勵廣告使用我們專用的測試廣告單元 ID:

安卓

ca-app-pub-3940256099942544/5224354917

iOS

ca-app-pub-3940256099942544/1712485313

它們經過專門配置,可以針對每個請求返回測試廣告,您可以在編碼、測試和調試時在自己的應用中自由使用它們。在發布應用之前,請確保將它們替換為您自己的廣告單元 ID。

有關移動廣告SDK的測試廣告如何工作的詳細信息,請參見測試廣告

加載廣告

要加載的廣告回報,調用RewardedAd對象的loadAd()方法。此方法需要的一個實例AdRequest作為參數。

...
public class GoogleMobileAdsDemoScript : MonoBehaviour
{
    private RewardedAd rewardedAd;
    ...

    public void Start()
    {
        ...

        this.rewardedAd = new RewardedAd(adUnitId);

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

廣告活動

要進一步自定義廣告的行為,您可以掛鉤廣告生命週期中的許多事件:加載、打開、關閉等。通過為適當的事件處理程序註冊一個委託來監聽這些事件。最重要的事件是OnUserEarnedReward ,當用戶獎勵觀看視頻時調用。您還可以實施其他廣告事件,如下所示。

using GoogleMobileAds.Api;
...
public class GoogleMobileAdsDemoScript : MonoBehaviour
{
    private RewardedAd rewardedAd;
    ...

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

        this.rewardedAd = new RewardedAd(adUnitId);

        // Called when an ad request has successfully loaded.
        this.rewardedAd.OnAdLoaded += HandleRewardedAdLoaded;
        // Called when an ad request failed to load.
        this.rewardedAd.OnAdFailedToLoad += HandleRewardedAdFailedToLoad;
        // Called when an ad is shown.
        this.rewardedAd.OnAdOpening += HandleRewardedAdOpening;
        // Called when an ad request failed to show.
        this.rewardedAd.OnAdFailedToShow += HandleRewardedAdFailedToShow;
        // Called when the user should be rewarded for interacting with the ad.
        this.rewardedAd.OnUserEarnedReward += HandleUserEarnedReward;
        // Called when the ad is closed.
        this.rewardedAd.OnAdClosed += HandleRewardedAdClosed;

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

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

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

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

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

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

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

OnUserEarnedReward事件提供特殊事件參數。它傳遞的實例RewardTypeAmount描述給予用戶獎勵:

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

可用的廣告事件

OnAdLoaded
在廣告完成加載時調用。
OnAdFailedToLoad
在廣告加載失敗時調用。的Message所提供的屬性AdErrorEventArgs描述發生故障的類型。
OnAdOpening
在顯示廣告並覆蓋設備屏幕時調用。如有必要,這是暫停應用音頻輸出或遊戲循環的好地方。
OnAdFailedToShow
當廣告無法顯示時調用。的Message所提供的屬性AdErrorEventArgs描述發生故障的類型。
OnUserEarnedReward
當用戶因觀看視頻而應獲得獎勵時調用。該Reward參數描述獎勵才會呈現給用戶。
OnAdClosed
當激勵視頻廣告由於用戶點擊關閉圖標或使用後退按鈕而關閉時調用。如果您的應用暫停其音頻輸出或遊戲循環,這是恢復它的好地方。

展示廣告

在向用戶展示激勵廣告之前,他們必須明確選擇查看激勵廣告內容以換取獎勵。激勵廣告必須始終是一種選擇加入的體驗。

要顯示廣告的回報,檢查IsLoaded()方法來驗證它的完成載入,然後調用show()以下是如何執行此操作的示例:

private void UserChoseToWatchAd()
{
  if (this.rewardedAd.IsLoaded()) {
    this.rewardedAd.Show();
  }
}

使用OnAdClosed預加載下一有好報廣告

RewardedAd是一次性使用的對象。這意味著一旦顯示激勵廣告,該對象就不能用於加載另一個廣告。要請求其他獎勵的廣告,你需要創建一個新的RewardedAd對象。

最好的做法是加載另一個獎勵廣告在OnAdClosed廣告事件,以便與前一個被解僱的下一個有好報廣告開始加載,一旦:

...
public class GoogleMobileAdsDemoScript : MonoBehaviour
{
    private RewardedAd rewardedAd;
    ...

    public void CreateAndLoadRewardedAd()
    {
        #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

        this.rewardedAd = new RewardedAd(adUnitId);

        this.rewardedAd.OnAdLoaded += HandleRewardedAdLoaded;
        this.rewardedAd.OnUserEarnedReward += HandleUserEarnedReward;
        this.rewardedAd.OnAdClosed += HandleRewardedAdClosed;

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

...

    public void HandleRewardedAdClosed(object sender, EventArgs args)
    {
        this.CreateAndLoadRewardedAd();
    }
}

加載多個激勵廣告

加載多個獎勵廣告,請按照列出的步驟創建獎勵廣告對象加載廣告節你打算負荷每個廣告。下面的代碼段演示瞭如何為兩個不同的廣告展示位置加載兩個激勵廣告。

...
public class GoogleMobileAdsDemoScript : MonoBehaviour
{
    private RewardedAd gameOverRewardedAd;
    private RewardedAd extraCoinsRewardedAd;
    ...
    public void Start()
    {
        ...

        this.gameOverRewardedAd = CreateAndLoadRewardedAd(adUnitId);
        this.extraCoinsRewardedAd = CreateAndLoadRewardedAd(adUnitId);
    }

    public RewardedAd CreateAndLoadRewardedAd(string adUnitId)
    {
        RewardedAd rewardedAd = new RewardedAd(adUnitId);

        rewardedAd.OnAdLoaded += HandleRewardedAdLoaded;
        rewardedAd.OnUserEarnedReward += HandleUserEarnedReward;
        rewardedAd.OnAdClosed += HandleRewardedAdClosed;

        // Create an empty ad request.
        AdRequest request = new AdRequest.Builder().Build();
        // Load the rewarded ad with the request.
        rewardedAd.LoadAd(request);
        return rewardedAd;
    }
}

樣品