插頁式獎勵廣告 (Beta 版)

插頁式獎勵廣告會自動在應用程式的自然轉換點顯示,是用來向使用者發放獎勵的廣告格式。不同於獎勵廣告,插頁式獎勵廣告不需等使用者選擇觀看即可放送。

必要條件

請先設定 Google Mobile Ads Unity Plugin,再繼續操作。

請務必使用測試廣告進行測試

下列程式碼範例包含廣告單元 ID,可用於要求測試廣告。這類 ID 經特別設定,可針對每項要求傳回測試廣告,而非實際廣告,因此可安心使用。

不過,在 Ad Manager 網頁介面註冊應用程式,並建立要在應用程式中使用的廣告單元 ID 後,請在開發期間明確將裝置設為測試裝置。

/21775744923/example/interstitial

初始化 Google Mobile Ads Unity Plugin

應用程式必須先呼叫 MobileAds.Initialize(),初始化 Google Mobile Ads Unity Plugin 之後,才能載入廣告。這項操作只要執行一次,最佳時機是應用程式啟動時。

using GoogleMobileAds;
using GoogleMobileAds.Api;

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

如果使用中介服務,請等到回呼發生後再載入廣告,確保所有中介服務轉接程式都已初始化。

載入插頁式獎勵廣告

如要載入插頁式獎勵廣告,請使用 RewardedInterstitialAd 類別的靜態 Load 方法。載入的 RewardedInterstitialAd 物件會以參數形式,提供給完成處理常式。以下是載入插頁式獎勵廣告的程式碼範例:

// Create the request used to load the ad.
var adRequest = new AdRequest();

// Send the request to load the ad.
RewardedInterstitialAd.Load(AD_UNIT_ID, adRequest,
    (RewardedInterstitialAd ad, LoadAdError error) =>
    {
        if (error != null)
        {
            // The ad failed to load.
            Debug.LogError($"Rewarded interstitial ad failed to load: {error}");
            return;
        }
        // The ad loaded successfully.
        Debug.Log("Rewarded interstitial ad loaded.");
    });

將 AD_UNIT_ID 替換為廣告單元 ID。

[選用] 驗證伺服器端驗證 (SSV) 回呼

如果應用程式需要在伺服器端驗證回呼中加入額外資料,請使用插頁式獎勵廣告的自訂資料功能。在插頁式獎勵廣告物件上設定的任何字串值,都會傳遞至 SSV 回呼的 custom_data 查詢參數。如未設定任何自訂資料值,SSV 回呼就不會包含 custom_data 查詢參數值。

下列程式碼範例示範如何在載入插頁式獎勵廣告後設定 SSV 選項:

// Create and pass the SSV options to the rewarded interstitial ad.
var options = new ServerSideVerificationOptions
{
    CustomData = "SAMPLE_CUSTOM_DATA_STRING"
};

rewardedInterstitialAd.SetServerSideVerificationOptions(options);

將 SAMPLE_CUSTOM_DATA_STRING 替換成自訂資料。

如要設定自訂獎勵字串,請務必在廣告顯示前完成。

利用獎勵回呼顯示插頁式獎勵廣告

顯示廣告時須提供回呼,才能處理給使用者的獎勵。每次載入只能顯示一次廣告。請使用 CanShowAd 方法,驗證廣告是否已可顯示。

下列程式碼會顯示插頁式獎勵廣告:

if (rewardedInterstitialAd != null && rewardedInterstitialAd.CanShowAd())
{
    rewardedInterstitialAd.Show((Reward reward) =>
    {
        // The ad was shown and the user earned a reward.
        Debug.Log($"Rewarded interstitial ad rewarded user: {reward.Type}, {reward.Amount}");
    });
}

監聽插頁式獎勵廣告事件

如要進一步自訂廣告行為,您可以連結廣告生命週期中的多個事件。以下範例會監聽廣告事件:

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

清除插頁式獎勵廣告

使用完 RewardedInterstitialAd 物件後,請務必先呼叫 Destroy 方法,再捨棄對該物件的參照:

if (rewardedInterstitialAd != null)
{
    rewardedInterstitialAd.Destroy();
}

這會通知外掛程式該物件已不再使用,可以取回占用的記憶體。如果未呼叫這個方法,就會導致記憶體流失。

預先載入下一則插頁式獎勵廣告

RewardedInterstitialAd 物件只能使用一次。顯示插頁式獎勵廣告後,這個物件就無法再次使用。如要請求其他插頁式獎勵廣告,請載入新的 RewardedInterstitialAd 物件。

如想為下次曝光商機準備插頁式獎勵廣告,請在 OnAdFullScreenContentClosed 或 OnAdFullScreenContentFailed 廣告事件觸發後,預先載入該廣告。

rewardedInterstitialAd.OnAdFullScreenContentClosed += () =>
{
    // Reload the ad so that you can show another as soon as possible.
    var adRequest = new AdRequest();
    RewardedInterstitialAd.Load(AD_UNIT_ID, adRequest,
        (RewardedInterstitialAd ad, LoadAdError error) =>
        {
            // Handle ad loading here.
            Debug.Log("Rewarded interstitial ad reloaded.");
        });
};

將 AD_UNIT_ID 替換為廣告單元 ID。

其他資源