插頁式廣告

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

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

必要條件

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

以下程式碼範例內含用來請求測試廣告的廣告單元 ID。已設為針對每個請求傳回測試廣告,而非實際廣告,因此安全無虞。

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

Android

ca-app-pub-3940256099942544/1033173712

iOS

ca-app-pub-3940256099942544/4411468910

初始化 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.
        });
    }
}

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

導入作業

整合插頁式廣告的主要步驟如下:

  1. 載入插頁式廣告
  2. 顯示插頁式廣告
  3. 監聽插頁式廣告事件
  4. 清除插頁式廣告
  5. 預先載入下一則插頁式廣告

載入插頁式廣告

系統會使用 InterstitialAd 類別上的靜態 Load() 方法載入插頁式廣告。載入方法需要廣告單元 ID、AdRequest 物件和完成處理常式,以便在廣告載入成功或失敗時呼叫。系統會在完成處理常式中,提供已載入的 InterstitialAd 物件做為參數。以下範例說明如何載入 InterstitialAd


  // These ad units are configured to always serve test ads.
#if UNITY_ANDROID
  private string _adUnitId = "ca-app-pub-3940256099942544/1033173712";
#elif UNITY_IPHONE
  private string _adUnitId = "ca-app-pub-3940256099942544/4411468910";
#else
  private string _adUnitId = "unused";
#endif

  private InterstitialAd _interstitialAd;

  /// <summary>
  /// Loads the interstitial ad.
  /// </summary>
  public void LoadInterstitialAd()
  {
      // Clean up the old ad before loading a new one.
      if (_interstitialAd != null)
      {
            _interstitialAd.Destroy();
            _interstitialAd = null;
      }

      Debug.Log("Loading the interstitial ad.");

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

      // send the request to load the ad.
      InterstitialAd.Load(_adUnitId, adRequest,
          (InterstitialAd ad, LoadAdError error) =>
          {
              // if error is not null, the load request failed.
              if (error != null || ad == null)
              {
                  Debug.LogError("interstitial ad failed to load an ad " +
                                 "with error : " + error);
                  return;
              }

              Debug.Log("Interstitial ad loaded with response : "
                        + ad.GetResponseInfo());

              _interstitialAd = ad;
          });
  }

顯示插頁式廣告

如要顯示已載入的插頁式廣告,請對 InterstitialAd 例項呼叫 Show() 方法。每次載入時可能會顯示廣告一次。請使用 CanShowAd() 方法確認廣告已準備好顯示。

/// <summary>
/// Shows the interstitial ad.
/// </summary>
public void ShowInterstitialAd()
{
    if (_interstitialAd != null && _interstitialAd.CanShowAd())
    {
        Debug.Log("Showing interstitial ad.");
        _interstitialAd.Show();
    }
    else
    {
        Debug.LogError("Interstitial ad is not ready yet.");
    }
}

監聽插頁式廣告事件

如要進一步自訂廣告行為,您可以捕捉廣告生命週期中的多個事件。請註冊委派代表,以便監聽這些事件,如下所示。

private void RegisterEventHandlers(InterstitialAd interstitialAd)
{
    // Raised when the ad is estimated to have earned money.
    interstitialAd.OnAdPaid += (AdValue adValue) =>
    {
        Debug.Log(String.Format("Interstitial ad paid {0} {1}.",
            adValue.Value,
            adValue.CurrencyCode));
    };
    // Raised when an impression is recorded for an ad.
    interstitialAd.OnAdImpressionRecorded += () =>
    {
        Debug.Log("Interstitial ad recorded an impression.");
    };
    // Raised when a click is recorded for an ad.
    interstitialAd.OnAdClicked += () =>
    {
        Debug.Log("Interstitial ad was clicked.");
    };
    // Raised when an ad opened full screen content.
    interstitialAd.OnAdFullScreenContentOpened += () =>
    {
        Debug.Log("Interstitial ad full screen content opened.");
    };
    // Raised when the ad closed full screen content.
    interstitialAd.OnAdFullScreenContentClosed += () =>
    {
        Debug.Log("Interstitial ad full screen content closed.");
    };
    // Raised when the ad failed to open full screen content.
    interstitialAd.OnAdFullScreenContentFailed += (AdError error) =>
    {
        Debug.LogError("Interstitial ad failed to open full screen content " +
                       "with error : " + error);
    };
}

清除插頁式廣告

使用 InterstitialAd 後,請務必先呼叫 Destroy() 方法,再放置對該方法的參照:

_interstitialAd.Destroy();

這會通知外掛程式,該物件已無法使用,且可收回物件佔用的記憶體。如未呼叫此方法,可能造成記憶體流失。

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

插頁式廣告是一次性的物件。這表示插頁式廣告一旦顯示,就無法再次使用。如要請求其他插頁式廣告,請建立新的 InterstitialAd 物件。

如要為下一個曝光商機準備插頁式廣告,請在 OnAdFullScreenContentClosedOnAdFullScreenContentFailed 廣告事件發生後預先載入插頁式廣告。

private void RegisterReloadHandler(InterstitialAd interstitialAd)
{
    // Raised when the ad closed full screen content.
    interstitialAd.OnAdFullScreenContentClosed += ()
    {
        Debug.Log("Interstitial Ad full screen content closed.");

        // Reload the ad so that we can show another as soon as possible.
        LoadInterstitialAd();
    };
    // Raised when the ad failed to open full screen content.
    interstitialAd.OnAdFullScreenContentFailed += (AdError error) =>
    {
        Debug.LogError("Interstitial ad failed to open full screen content " +
                       "with error : " + error);

        // Reload the ad so that we can show another as soon as possible.
        LoadInterstitialAd();
    };
}

最佳做法

判斷插頁式廣告是否適合您應用程式的廣告類型。
插頁式廣告最適合用於包含自然轉換點的應用程式。 應用程式內的工作結束時 (例如分享圖片或完成遊戲關卡),即會建立這類時間點。此外,請務必考量應用程式流程中的哪個時間點顯示插頁式廣告的最佳顯示時機,以及使用者的可能反應。
顯示插頁式廣告時,請將動作暫停。
插頁式廣告有多種類型,例如文字、圖片或影片。請務必確保應用程式顯示插頁式廣告時,一併暫停使用部分資源,廣告才能利用資源。舉例來說,當您呼叫顯示插頁式廣告時,請務必暫停應用程式產生的任何音訊輸出。您可以在使用者完成與廣告互動後叫用 OnAdFullScreenContentClosed() 事件中的音效。此外,也建議您在廣告放送期間暫時停止任何密集的運算工作,例如遊戲迴圈。確保使用者不會覺得顯示速度緩慢/沒有回應的圖像或影片跳動不流暢。
不要讓應用程式廣告氾濫。
提高在應用程式中顯示插頁式廣告的頻率也許是提升收益的好方法,但也有可能導致使用者體驗和點閱率下降。請確保使用者不會經常中斷,以免他們無法再使用您的應用程式。

其他資源

* 用途範例