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

Audience Network 可讓您利用 Facebook 廣告,將 Android 和 iOS 應用程式變成獲利來源。擁有全螢幕體驗的獎勵式影片廣告,可讓用戶自行選擇是否要透過檢視影片廣告來取得獎勵(例如虛擬貨幣、應用程式內部物品和專屬內容等)。廣告體驗是短篇影片,且會展示附有行動呼籲按鈕的結束圖卡。用戶看完整段影片後,您會收到授予用戶建議獎勵的回呼。

繼續操作前,請務必詳閱 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 有關。

目前 Android 適用的 Unity 遊戲僅讓主要 Unity Activity 擁有 singleTasklaunchMode。請參閱 Android 資訊清單的 Unity 文件活動的 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 版位編號
    • RewardData - RewardData 物件包含代表用戶編號的 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 回呼,應用程式才能收到通知,得知獎勵的驗證結果。

載入廣告後,您可以利用 Show 方法呈現廣告,就像一般 RewardedVideoAd 一樣。

請注意,伺服器驗證回呼可能在用戶關閉結束圖卡後才會發生。在收到回呼之前,您不應將獎勵式影片物件解除配置。

後續步驟

若要在 Unity 應用程式中整合不同的廣告格式,請依照指南操作:

當您準備好讓應用程式正式上線並開始營利,請確保您的應用程式並未違反 Audience Network 政策Facebook 社群守則,接著便可以將應用程式送審