插頁式廣告

選取平台: Android iOS Unity Flutter

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

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

先決條件

請一律使用測試廣告進行測試

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

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

/21775744923/example/interstitial

初始化 Mobile Ads SDK

應用程式必須先呼叫 MobileAds.Initialize(),初始化 Mobile Ads SDK 之後,才能載入廣告。這項操作只需執行一次,且最好在應用程式啟動時執行。

using GoogleMobileAds;
using GoogleMobileAds.Api;

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

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

載入插頁式廣告

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

// 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。

顯示插頁式廣告

如要顯示已載入的插頁式廣告,請在 AdManagerInterstitialAd 例項上呼叫 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 error) => { // Raised when the ad failed to open full screen content. };

清除插頁式廣告

完成 AdManagerInterstitialAd 後,請務必先呼叫 Destroy(),再捨棄對該方法的參照:

if (interstitialAd != null) { interstitialAd.Destroy(); }

這會通知外掛程式該物件已不再使用,可以取回占用的記憶體。如未呼叫這個方法,就會導致記憶體流失。

預先載入下一則插頁式廣告

插頁式廣告是一次性物件,顯示過後便無法再次使用。如要請求其他插頁式廣告,請建立新的 AdManagerInterstitialAd 物件。

如要提前準備插頁式廣告,並在下次曝光商機投放,請在 OnAdFullScreenContentClosedOnAdFullScreenContentFailed 廣告事件觸發後,預先載入該廣告。

interstitialAd.OnAdFullScreenContentClosed += () =>
{
    // Reload the ad so that we can show another as soon as possible.
    var adRequest = new AdRequest();
    InterstitialAd.Load("AD_UNIT_ID", adRequest, (InterstitialAd ad, LoadAdError error) =>
    {
        // Handle ad loading here.
    });
};

最佳做法

思考插頁式廣告是否適合您的應用程式。
插頁式廣告最適合用於包含自然轉換點的應用程式。每當使用者在應用程式中完成操作 (例如分享圖片或通過遊戲關卡) 時,就會形成這類轉換點。請務必考量在應用程式流程的哪些時間點顯示插頁式廣告為佳,以及使用者可能的反應。
顯示插頁式廣告時應暫停應用程式動作。
插頁式廣告可能會用到文字、圖片或影片等形式。應用程式顯示此類廣告時,請務必暫停使用部分資源,以利廣告放送。舉例來說,當您發出「顯示插頁式廣告」的呼叫時,請務必暫停應用程式的音訊輸出。您可以在使用者完成與廣告的互動後,叫用 OnAdFullScreenContentClosed() 活動來繼續播放音效。此外,顯示廣告時,建議暫停密集的運算工作 (如遊戲迴圈),確保使用者不會遇到圖像載入緩慢/沒有回應,或影片斷斷續續的問題。
不要向使用者大量放送廣告。
提高插頁式廣告在應用程式內的顯示頻率,乍看之下能有效提高收益,但也可能破壞使用者體驗並降低點閱率。為確保愉快的應用程式操作體驗,請不要頻繁打斷使用者。

其他資源