插頁式廣告

插頁式廣告會全螢幕顯示,覆蓋整個應用程式的介面。這類廣告通常顯示在應用程式流程中的自然轉換點,例如遊戲關卡間的暫停時間。應用程式顯示插頁式廣告時,使用者可選擇輕觸廣告前往到達網頁,或是關閉廣告返回應用程式。 個案研究

本指南說明如何將插頁式廣告整合至 Unity 應用程式。

必要條件

請先設定 Google Mobile Ads Unity Plugin,再繼續操作。

請務必使用測試廣告進行測試

下列程式碼範例包含廣告單元 ID,可用於要求測試廣告。這類 ID 經特別設定,可針對每項要求傳回測試廣告,而非實際廣告,因此可安心使用。

不過,在 AdMob 網頁介面中註冊應用程式,並建立要在應用程式中使用的廣告單元 ID 後,請在開發期間將裝置明確設為測試裝置

Android

ca-app-pub-3940256099942544/1033173712

iOS

ca-app-pub-3940256099942544/4411468910

初始化 Google Mobile Ads Unity Plugin

應用程式須先呼叫 MobileAds.Initialize(),初始化 Google Mobile Ads Unity Plugin後才能載入廣告。這項操作只要執行一次,最佳時機是應用程式啟動時。

using GoogleMobileAds;
using GoogleMobileAds.Api;

public class GoogleMobileAdsDemoScript : MonoBehaviour
{
    public void Start()
    {
        // Initialize Google Mobile Ads Unity Plugin.
        MobileAds.Initialize((InitializationStatus initStatus) =>
        {
            // This callback is called once the MobileAds SDK is initialized.
        });
    }
}

如果使用中介服務,請等到回呼發生後再載入廣告,確保所有中介服務轉接程式都已初始化。

載入插頁式廣告

如要載入插頁式廣告,請使用 InterstitialAd 類別的靜態 Load() 方法。該載入方法需有廣告單元 ID、AdRequest 物件,以及在廣告載入成功或失敗時呼叫的完成處理常式。載入的 InterstitialAd 物件會以參數形式,提供給完成處理常式。以下範例會載入 InterstitialAd:

// Create our request used to load the ad.
var adRequest = new AdRequest();

// Send the request to load the ad.
InterstitialAd.Load("AD_UNIT_ID", adRequest, (InterstitialAd ad, LoadAdError error) =>
{
    if (error != null)
    {
        // The ad failed to load.
        return;
    }
    // The ad loaded successfully.
});

AD_UNIT_ID 替換為廣告單元 ID。

顯示插頁式廣告

如要顯示已載入的插頁式廣告,請在 InterstitialAd 例項上呼叫 Show() 方法。每次載入可能只顯示一次廣告。請使用 CanShowAd() 方法,驗證廣告是否已可顯示。

if (interstitialAd != null && interstitialAd.CanShowAd())
{
    interstitialAd.Show();
}

監聽插頁式廣告事件

如要進一步自訂廣告行為,您可以連結廣告生命週期中的多個事件。以下範例會監聽廣告事件:

interstitialAd.OnAdPaid += (AdValue adValue) =>
{
    // Raised when the ad is estimated to have earned money.
};
interstitialAd.OnAdImpressionRecorded += () =>
{
    // Raised when an impression is recorded for an ad.
};
interstitialAd.OnAdClicked += () =>
{
    // Raised when a click is recorded for an ad.
};
interstitialAd.OnAdFullScreenContentOpened += () =>
{
    // Raised when the ad opened full screen content.
};
interstitialAd.OnAdFullScreenContentClosed += () =>
{
    // Raised when the ad closed full screen content.
};
interstitialAd.OnAdFullScreenContentFailed += (AdError