插頁式獎勵廣告 (Beta 版)

插頁式獎勵廣告是一種獎勵廣告格式,可讓您在應用程式自然轉換時自動顯示廣告,並提供獎勵。有別於獎勵廣告 必須啟用才能觀看插頁式獎勵廣告。

必要條件

一律使用測試廣告進行測試

以下程式碼範例包含廣告單元 ID,供您請求 測試廣告該功能已設定為傳回測試廣告,而不是 導入及導入廣告,以備不時之需。

不過,在 Ad Manager 網頁介面,並建立自己的廣告單元 用於應用程式的 ID,請明確將裝置設為測試裝置 裝置

/21775744923/example/rewarded-interstitial

初始化 Mobile Ads SDK

在載入廣告之前,請讓應用程式呼叫 MobileAds.Initialize(),初始化 Mobile Ads SDK。這項操作只需進行一次,最好在應用程式啟動時。

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 = "/21775744923/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) 回呼

如果應用程式需要在伺服器端驗證回呼中使用額外資料,則應使用獎勵插頁式廣告的自訂資料功能。獎勵廣告物件上設定的任何字串值,都會傳遞至 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();
    };
}

其他資源