獎勵廣告

選取平台: Android iOS Unity Flutter

獎勵廣告是一種可讓使用者選擇互動,以換取應用程式內獎勵的廣告形式。本指南說明如何將 AdMob 的獎勵廣告整合至 Unity 應用程式。

請參考以下內容,瞭解具體操作方式。

先決條件

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

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

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

/21775744923/example/rewarded

初始化 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.
        });
    }
}

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

載入獎勵廣告

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

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

// Send the request to load the ad.
RewardedAd.Load("AD_UNIT_ID", adRequest, (RewardedAd ad, LoadAdError error) =>
{
    if (error != null)
    {
        // The ad failed to load.
        return;
    }
    // The ad loaded successfully.
});

AD_UNIT_ID 替換為廣告單元 ID。

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

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

請參考下列程式碼範例,瞭解如何在獎勵廣告載入後設定 SSV 選項。

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

rewardedAd.SetServerSideVerificationOptions(options);

SAMPLE_CUSTOM_DATA_STRING 替換為自訂資料。

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

以獎勵回呼顯示獎勵廣告

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

請參考下列程式碼,瞭解如何以最佳方法顯示獎勵廣告。

if (rewardedAd != null && rewardedAd.CanShowAd()) { rewardedAd.Show((Reward reward) => { // The ad was showen and the user earned a reward. }); }

監聽獎勵廣告事件

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

rewardedAd.OnAdPaid += (AdValue adValue) => { // Raised when the ad is estimated to have earned money. }; rewardedAd.OnAdImpressionRecorded += () => { // Raised when an impression is recorded for an ad. }; rewardedAd.OnAdClicked += () => { // Raised when a click is recorded for an ad. }; rewardedAd.OnAdFullScreenContentOpened += () => { // Raised when the ad opened full screen content. }; rewardedAd.OnAdFullScreenContentClosed += () => { // Raised when the ad closed full screen content. }; rewardedAd.OnAdFullScreenContentFailed += (AdError error) => { // Raised when the ad failed to open full screen content. };

清除獎勵廣告

完成 RewardedAd 後,請務必先呼叫 Destroy(),再捨棄對該方法的參照:

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

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

預先載入下一則獎勵廣告

RewardedAd 是一次性物件,顯示過獎勵廣告後便無法再次使用。如要請求其他獎勵廣告,請建立新的 RewardedAd 物件。

如要提前準備獎勵廣告,並在下次曝光商機投放,請在 OnAdFullScreenContentClosedOnAdFullScreenContentFailed 廣告事件觸發後,預先載入該廣告。

rewardedAd.OnAdFullScreenContentClosed += () =>
{
    // Reload the ad so that we can show another as soon as possible.
    var adRequest = new AdRequest();
    RewardedAd.Load("AD_UNIT_ID", adRequest, (RewardedAd ad, LoadAdError error) =>
    {
        // Handle ad loading here.
    });
};

其他資源