在 Unity 應用程式中加入插頁廣告

有了 Audience Network,您的 Android 和 iOS 應用程式便可以透過 Facebook 廣告營利。插頁廣告能為用戶提供全螢幕廣告體驗,可用於應用程式流程中的各個轉換點,例如在不同活動之間或遊戲關卡改變時的暫停時段。廣告創意內容可以是圖片、影片或輪播廣告。本指南說明如何在應用程式中加入插頁廣告。

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

插頁廣告步驟

步驟 1:建立插頁廣告物件

步驟 2:新增回呼事件

第 3 步:載入廣告

步驟 4:顯示廣告

步驟 1:建立插頁廣告物件

如要顯示插頁廣告,首先您要在附加至 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 版位編號。

步驟 2:新增回呼事件

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

...
// 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();
    }
});
...

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:載入廣告

InterstitialAd 實例化後,下一步就是載入廣告。您可以使用 InterstitialAd 類別的 loadAd() 方法載入廣告。

以下透過上述範例,展示了如何載入廣告:

...
this.interstitialAd.LoadAd();
...

步驟 4:顯示廣告

最後,在載入插頁廣告後,您可以呼叫 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 社群守則後,然後再提交您的應用程式以供審查