在 Unity 應用程式中加入獎勵式影片廣告

有了 Audience Network,您的 Android 和 iOS 應用程式便可以透過 Facebook 廣告營利。獎勵式影片廣告能提供全螢幕體驗,讓用戶自行選擇是否要透過觀看影片來取得獎勵(例如虛擬貨幣、應用程式內物品和隱藏內容等)。此廣告體驗是一段短片,內附的結束說明卡帶有呼籲字句。整段影片播完後,您會收到回呼以將建議的獎勵頒給用戶。

確保您在開始操作前,已經先行參閱 Audience Network 新手指南Unity 新手指南

獎勵式影片廣告步驟

第 1 步:初始化獎勵式影片廣告

第 2 步:新增回呼事件

第 3 步:載入廣告

第 4 步:顯示廣告

第 1 步:初始化獎勵式影片廣告

如要顯示獎勵式影片廣告,首先您要在附至 GameObject 的 C# 指令碼中建立 RewardedVideoAd 物件。

...
using AudienceNetwork;
...

public class RewardedVideoAdTest : MonoBehaviour
{
    public void LoadRewardedVideo()
    {
        // Create the rewarded video unit with a placement ID (generate your own on the Facebook app settings).
        // Use different ID for each ad placement in your app.
        this.rewardedVideoAd = new RewardedVideoAd("YOUR_PLACEMENT_ID");

        this.rewardedVideoAd.Register(this.gameObject);

        // Set delegates to get notified on changes or when the user interacts with the ad.
        this.rewardedVideoAd.RewardedVideoAdDidLoad = (delegate() {
            Debug.Log("RewardedVideo ad loaded.");
            this.isLoaded = true;
        });
        this.rewardedVideoAd.RewardedVideoAdDidFailWithError = (delegate(string error) {
            Debug.Log("RewardedVideo ad failed to load with error: " + error);
        });
        this.rewardedVideoAd.RewardedVideoAdWillLogImpression = (delegate() {
            Debug.Log("RewardedVideo ad logged impression.");
        });
        this.rewardedVideoAd.RewardedVideoAdDidClick = (delegate() {
            Debug.Log("RewardedVideo ad clicked.");
        });

        this.rewardedVideoAd.RewardedVideoAdDidClose = (delegate() {
            Debug.Log("Rewarded video ad did close.");
            if (this.rewardedVideoAd != null) {
                this.rewardedVideoAd.Dispose();
            }
        });

        // Initiate the request to load the ad.
        this.rewardedVideoAd.LoadAd();
    }
}

RewardedVideoAd 的建構函式包含以下參數:

  • placementId:此獎勵式影片廣告單位的 Audience Network 版位編號。

第 2 步:新增回呼事件

然後,您可以執行幾次回呼,以訂閱廣告的生命週期事件。為事件註冊委派,聆聽是否有這些事件,正如下列範例所示:

...
// Set delegates to get notified on changes or when the user interacts with the ad.
this.rewardedVideoAd.RewardedVideoAdDidLoad = (delegate() {
    Debug.Log("RewardedVideo ad loaded.");
    this.isLoaded = true;
});
this.rewardedVideoAd.RewardedVideoAdDidFailWithError = (delegate(string error) {
    Debug.Log("RewardedVideo ad failed to load with error: " + error);
});
this.rewardedVideoAd.RewardedVideoAdWillLogImpression = (delegate() {
    Debug.Log("RewardedVideo ad logged impression.");
});
this.rewardedVideoAd.RewardedVideoAdDidClick = (delegate() {
    Debug.Log("RewardedVideo ad clicked.");
});

this.rewardedVideoAd.RewardedVideoAdDidClose = (delegate() {
    Debug.Log("Rewarded video ad did close.");
    if (this.rewardedVideoAd != null) {
        this.rewardedVideoAd.Dispose();
    }
});
...

Unity Android 中的已終結廣告活動回調

這份資料只適用於 Android。

目前,Unity Android 遊戲只支援主要的 Unity Activity 擁有 singleTasklaunchMode。請參閱 Unity Android Manifest 文件Android 活動文件

由於我們使用 Activity 來顯示 InterstitialRewarded Video 廣告,如果用戶將應用程式退至背景,然後使用圖示(而非應用程式切換器)重新啟動應用程式,廣告活動就可能會在沒有正確關閉的情況下遭到終結。您可以使用下列回調,檢查用戶是否關閉了廣告:

適用於插頁廣告:

this.interstitialAd.interstitialAdDidClose = (delegate() { Debug.Log("Interstitial ad did close."); this.didClose = true; if (this.interstitialAd != null) { this.interstitialAd.Dispose(); } }); #if UNITY_ANDROID /* * Only relevant to Android. * This callback will only be triggered if the Interstitial activity has * been destroyed without being properly closed.This can happen if an * app with launchMode:singleTask (such as a Unity game) goes to * background and is then relaunched by tapping the icon. */ this.interstitialAd.interstitialAdActivityDestroyed = (delegate() { if (!this.didClose) { Debug.Log("Interstitial activity destroyed without being closed first."); Debug.Log("Game should resume."); } }); #endif 

適用於獎勵式影片:

this.rewardedVideoAd.rewardedVideoAdDidClose = (delegate() { Debug.Log("Rewarded video ad did close."); this.didClose = true; if (this.rewardedVideoAd != null) { this.rewardedVideoAd.Dispose(); } }); #if UNITY_ANDROID /* * Only relevant to Android. * This callback will only be triggered if the Rewarded Video activity * has been destroyed without being properly closed.This can happen if * an app with launchMode:singleTask (such as a Unity game) goes to * background and is then relaunched by tapping the icon. */ this.rewardedVideoAd.rewardedVideoAdActivityDestroyed = (delegate() { if (!this.didClose) { Debug.Log("Rewarded video activity destroyed without being closed first."); Debug.Log("Game should resume. User should not get a reward."); } }); #endif 

第 3 步:載入廣告

RewardedVideoAd 實例化後,下一步就是載入廣告。您可以呼叫 loadAd 方法來載入廣告。

以下將使用上述範例,展示如何初始化廣告載入:

...
this.rewardedVideoAd.LoadAd();
...

第 4 步:顯示廣告

最後,在載入廣告後,您可以呼叫 Show 方法,在畫面上顯示獎勵式影片廣告。例如,您可以建立 ShowRewardedVideo 的函數,然後在需要顯示廣告時呼叫此函數:

public void ShowRewardedVideo()
{
    if (this.isLoaded) {
        this.rewardedVideoAd.Show();
        this.isLoaded = false;
    } else {
        Debug.Log("Ad not loaded. Click load to request an ad.");
    }
}

伺服器端獎勵驗證

此為自選功能!您不需要執行伺服器端獎勵驗證,就能使用獎勵式影片廣告。只有您決定在自有伺服器上引進驗證步驟,以便驗證獎勵來提高安全性時,才需要用到這項功能。請將您的發佈商端點提供給 Facebook 代表,以便啟用這項功能。

概覽

如果您要管理用戶獎勵伺服器端,Facebook 就會採用某種驗證技術來提供安全的實行方案。我們的伺服器會與指定 https 端點進行通訊,以便驗證每次廣告展示,並確認是否應發放獎勵。

  1. Audience Network SDK 會以下列參數要求獎勵式影片廣告:
    • placementId:Audience Network 版位編號
    • RewardDataRewardData 物件包含代表用戶編號的 UserId,以及代表實際遊戲內獎勵的 Currency
  2. 影片播完後,Facebook 伺服器會將這些價值與應用程式密鑰和專屬交易編號一併傳遞到您指定的端點。
  3. 收到之後,伺服器會驗證該項要求,並作出下列回應:
    • 200 response:要求有效,應發放獎勵
    • Non 200 response:要求無效,不應發放獎勵。
  4. 影片播完後,結束說明卡會顯示,下列其中一個事件也會觸發。
    • RewardedVideoAdDidSucceed:只有在第 3 步收到 200 回應時才會觸發。
    • RewardedVideoAdDidFail:只有在第 3 步收到非 200 回應時才會觸發。

會從 Facebook 伺服器抵達發佈商端點的範例網址:https://www.your_end_point.com/?token=APP_SECRET&puid=USER_ID&pc=REWARD_ID&ptid=UNIQUE_TRANSACTION_ID

工作流程看起來如下所示:

安裝

初始化獎勵式影片物件後,您必須先將用戶編號和獎勵金額傳入獎勵式廣告資料,再載入廣告。您可以使用 RewardData 類別完成這項操作。用戶編號和獎勵金額都是字串。例如:

public class RewardedVideoAdTest : MonoBehaviour
{
  ...
  private bool isLoaded;
  private RewardedVideoAd rewardedVideoAd;

  public void LoadRewardedVideo()

    //Set the rewarded ad data
    RewardData rewardData = new RewardData();
    rewardData.UserId = "USER_ID";
    rewardData.Currency = "REWARD_ID";

    // Instantiate RewardedVideoAd with reward data
    this.rewardedVideoAd = new RewardedVideoAd("YOUR_PLACEMENT_ID", rewardData);
    this.rewardedVideoAd.Register(this.gameObject);

    // Set delegates to get notified on changes or when the user interacts with the ad.
    this.rewardedVideoAd.RewardedVideoAdDidLoad = (delegate() {
        Debug.Log("RewardedVideo ad loaded.");
        this.isLoaded = true;
    });
    this.rewardedVideoAd.RewardedVideoAdDidFailWithError = (delegate(string error) {
        Debug.Log("RewardedVideo ad failed to load with error: " + error);
    });
    this.rewardedVideoAd.RewardedVideoAdWillLogImpression = (delegate() {
        Debug.Log("RewardedVideo ad logged impression.");
    });
    this.rewardedVideoAd.RewardedVideoAdDidClick = (delegate() {
        Debug.Log("RewardedVideo ad clicked.");
    });
    this.rewardedVideoAd.RewardedVideoAdDidClose = (delegate() {
        Debug.Log("Rewarded video ad did close.");
        if (this.rewardedVideoAd != null) {
            this.rewardedVideoAd.Dispose();
        }
    });

    // For S2S validation you need to register the following two callback
    this.rewardedVideoAd.RewardedVideoAdDidSucceed = (delegate() {
        Debug.Log("Rewarded video ad validated by server");
    });
    this.rewardedVideoAd.RewardedVideoAdDidFail = (delegate() {
        Debug.Log("Rewarded video ad not validated, or no response from server");
    });       

    this.rewardedVideoAd.loadAd();
  }
  ...
}

若要讓應用程式獲知是否已驗證獎勵,除了 RewardedVideoAd 的其他回呼外,您也需要執行 RewardedVideoAdDidSucceedRewardedVideoAdDidFail 回呼。

載入廣告後,您可以像顯示一般的 RewardedVideoAd 一樣,使用 Show 方法來顯示廣告。

請注意:伺服器驗證回呼可能會在用戶關閉結束說明卡後發生。您必須等到上述其中一個回呼發生後,才能取消分配獎勵式影片物件。

後續步驟

請遵從我們的指示,在您的 Unity 應用程式中整合不同的廣告格式:

一旦您準備好正式推出應用程式並開始創造收入,請先確定應用程式完全符合 Audience Network 政策以及 Facebook 社群守則後,然後再提交您的應用程式以供審查