Audience Network 可讓您利用 Facebook 廣告,將 Android 和 iOS 應用程式變成獲利來源。插頁廣告是一種完整的廣告螢幕體驗,可用於應用程式流程中的轉換點,例如活動之間,或是遊戲關卡之間的暫停期間。廣告創意內容可以是圖像、影片或輪播廣告。這份指南說明如何在應用程式加入插頁廣告。
繼續操作前,請務必詳閱 Audience Network 新手指南與 Unity 新手指南。
顯示插頁廣告的第一步是在 GameObject
附加的 C# 指令碼中建立 InterstitialAd
物件。
... using AudienceNetwork; ... public class InterstitialAdTest : MonoBehaviour { ... private InterstitialAd interstitialAd; private bool isLoaded; ... public void LoadInterstitial() { this.interstitialAd = new InterstitialAd("YOUR_PLACEMENT_ID"); this.interstitialAd.Register(this.gameObject); // Set delegates to get notified on changes or when the user interacts with the ad. this.interstitialAd.InterstitialAdDidLoad = (delegate() { Debug.Log("Interstitial ad loaded."); this.isLoaded = true; }); interstitialAd.InterstitialAdDidFailWithError = (delegate(string error) { Debug.Log("Interstitial ad failed to load with error: " + error); }); interstitialAd.InterstitialAdWillLogImpression = (delegate() { Debug.Log("Interstitial ad logged impression."); }); interstitialAd.InterstitialAdDidClick = (delegate() { Debug.Log("Interstitial ad clicked."); }); this.interstitialAd.interstitialAdDidClose = (delegate() { Debug.Log("Interstitial ad did close."); if (this.interstitialAd != null) { this.interstitialAd.Dispose(); } }); // Initiate the request to load the ad. this.interstitialAd.LoadAd(); } ... }
InterstitialAd
的建構函式有以下參數:
placementId
- 此插頁廣告單位的 Audience Network 版位編號。接下來,您可以實作一些回呼來訂閱廣告的生命週期事件。為事件註冊委派以接聽這些事件,如下例所示:
... // Set delegates to get notified on changes or when the user interacts with the ad. this.interstitialAd.InterstitialAdDidLoad = (delegate() { Debug.Log("Interstitial ad loaded."); this.isLoaded = true; }); interstitialAd.InterstitialAdDidFailWithError = (delegate(string error) { Debug.Log("Interstitial ad failed to load with error: " + error); }); interstitialAd.InterstitialAdWillLogImpression = (delegate() { Debug.Log("Interstitial ad logged impression."); }); interstitialAd.InterstitialAdDidClick = (delegate() { Debug.Log("Interstitial ad clicked."); }); this.interstitialAd.interstitialAdDidClose = (delegate() { Debug.Log("Interstitial ad did close."); if (this.interstitialAd != null) { this.interstitialAd.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
InterstitialAd
實例化後,下一步是載入廣告,這可以透過 InterstitialAd
類別的 loadAd() 方法完成。
在上方顯示的範例中,載入廣告的方法如下:
... this.interstitialAd.LoadAd(); ...
最後載入插頁廣告後,您可以呼叫 Show
方法,在畫面上呈現插頁廣告。例如,您可以為 ShowInterstitial
提供一個函式,並在顯示插頁廣告時呼叫此函式:
// Show button public void ShowInterstitial() { if (this.isLoaded) { this.interstitialAd.Show(); this.isLoaded = false; } else { Debug.Log("Interstitial Ad not loaded!"); } }
若要在 Unity 應用程式中整合不同的廣告格式,請依照指南操作:
當您準備好讓應用程式正式上線並開始營利,請確保您的應用程式並未違反 Audience Network 政策與 Facebook 社群守則,接著便可以將應用程式送審。