Audience Network 可讓您利用 Facebook 廣告,將 Android 和 iOS 應用程式變成獲利來源。擁有全螢幕體驗的獎勵式影片廣告,可讓用戶自行選擇是否要透過檢視影片廣告來取得獎勵(例如虛擬貨幣、應用程式內部物品和專屬內容等)。廣告體驗是短篇影片,且會展示附有行動呼籲按鈕的結束圖卡。用戶看完整段影片後,您會收到授予用戶建議獎勵的回呼。
繼續操作前,請務必詳閱 Audience Network 新手指南與 Unity 新手指南。
顯示獎勵式影片廣告的第一步是在 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 版位編號。接下來,您可以實作一些回呼來訂閱廣告的生命週期事件。為事件註冊委派以接聽這些事件,如下例所示:
... // 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(); } }); ...
本節僅與 Android 有關。
目前 Android 適用的 Unity 遊戲僅讓主要 Unity Activity
擁有 singleTask
的 launchMode
。請參閱 Android 資訊清單的 Unity 文件和活動的 Android 文件。
因為我們使用 Activity
顯示 Interstitial
和 Rewarded 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
RewardedVideoAd
實例化後,下一步是載入廣告,採用的方法是呼叫 loadAd
。
在上方顯示的範例中,啟動廣告載入的方法如下:
... this.rewardedVideoAd.LoadAd(); ...
最後載入廣告後,您可以呼叫 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 端點通訊,以驗證各次廣告曝光,確認是否應授予獎勵。
placementId
:Audience Network 版位編號RewardData
- RewardData
物件包含代表用戶編號的 UserId
和代表真正遊戲獎勵的 Currency
。200 response
:有效的要求且應給予獎勵Non 200 response
:無效的要求且不應給予獎勵。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
的其他回呼外,您必須實作 RewardedVideoAdDidSucceed
和 RewardedVideoAdDidFail
回呼,應用程式才能收到通知,得知獎勵的驗證結果。
載入廣告後,您可以利用 Show
方法呈現廣告,就像一般 RewardedVideoAd
一樣。
請注意,伺服器驗證回呼可能在用戶關閉結束圖卡後才會發生。在收到回呼之前,您不應將獎勵式影片物件解除配置。
若要在 Unity 應用程式中整合不同的廣告格式,請依照指南操作:
當您準備好讓應用程式正式上線並開始營利,請確保您的應用程式並未違反 Audience Network 政策與 Facebook 社群守則,接著便可以將應用程式送審。