插頁式廣告

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

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

先備知識

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

以下程式碼範例包含廣告單元 ID,供您請求 測試廣告該功能已設定為傳回測試廣告,而不是 導入及導入廣告,以備不時之需。

不過,在 Ad Manager 網頁介面,並建立自己的廣告單元 用於應用程式的 ID,請明確將裝置設為測試裝置 裝置

/6499/example/interstitial

初始化 Mobile Ads SDK

載入廣告前,請呼叫應用程式,以便呼叫 MobileAds.Initialize()。這項操作只需進行一次,最好在應用程式啟動時。

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. 預先載入下一個插頁式廣告

載入插頁式廣告

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


  // This ad unit is configured to always serve test ads.
  private string _adUnitId = "/6499/example/interstitial";

  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 AdManagerAdRequest();

      // send the request to load the ad.
      AdManagerInterstitialAd.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;
          });
  }

顯示插頁式廣告

如要顯示已載入的插頁式廣告,請在Show() AdManagerInterstitialAd 執行個體。廣告每段可顯示一次 載入。請使用 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);
    };
}

清除插頁式廣告

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

_interstitialAd.Destroy();

這會通知外掛程式,說明該物件已不再使用,以及該物件的記憶體 可收回這些物件。無法呼叫此方法會導致記憶體流失。

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

插頁式廣告是一次性的物件。具體來說 ,即無法再次使用該物件。如需請求其他插頁式廣告, 建立新的 AdManagerInterstitialAd 物件。

如要為下一個曝光商機做好準備,請預先載入 OnAdFullScreenContentClosed 或 會引發 OnAdFullScreenContentFailed 個廣告事件。

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

應用程式事件

應用程式事件可讓您建立廣告,以便傳送訊息到應用程式的程式碼。應用程式 就能依據這些訊息採取動作

您可以使用 AppEvent 監聽 Ad Manager 專屬的應用程式事件。這些活動 這種情況在廣告生命週期的任何時間點發生,即使是在呼叫載入之前亦然。

namespace GoogleMobileAds.Api.AdManager;

/// The App event message sent from the ad.
public class AppEvent
{
    // Name of the app event.
    string Name;
    // Argument passed from the app event.
    string Value;
}

廣告發生應用程式事件時,系統會產生 OnAppEventReceived。以下是 以下舉例說明如何在程式碼中處理此事件:

_interstitialAd.OnAppEventReceived += (AppEvent args) =>
{
    Debug.Log($"Received app event from the ad: {args.Name}, {args.Value}.");
};

以下範例說明如何變更應用程式的背景顏色 根據包含顏色名稱的應用程式事件進行選擇:

_interstitialAd.OnAppEventReceived += (AppEvent args) =>
{
  if (args.Name == "color")
  {
    Color color;
    if (ColorUtility.TryParseColor(arg.Value, out color))
    {
      gameObject.GetComponent<Renderer>().material.color = color;
    }
  }
};

以下的對應廣告素材會傳送顏色應用程式事件:

<html>
<head>
  <script src="//www.gstatic.com/afma/api/v1/google_mobile_app_ads.js"></script>
  <script>
    document.addEventListener("DOMContentLoaded", function() {
      // Send a color=green event when ad loads.
      admob.events.dispatchAppEvent("color", "green");

      document.getElementById("ad").addEventListener("click", function() {
        // Send a color=blue event when ad is clicked.
        admob.events.dispatchAppEvent("color", "blue");
      });
    });
  </script>
  <style>
    #ad {
      width: 320px;
      height: 50px;
      top: 0px;
      left: 0px;
      font-size: 24pt;
      font-weight: bold;
      position: absolute;
      background: black;
      color: white;
      text-align: center;
    }
  </style>
</head>
<body>
  <div id="ad">Carpe diem!</div>
</body>
</html>

最佳做法

判斷插頁式廣告是否適合您應用程式的廣告類型。
插頁式廣告最適合包含自然轉換點的應用程式。 應用程式內工作的結論,例如分享圖片或完成 就是所謂的遊戲層級務必考量到 顯示插頁式廣告的最佳流程,以及使用者的回應方式。
在顯示插頁式廣告時暫停動作。
插頁式廣告有多種類型,如文字、廣告、 圖片、圖片或影片請務必確認應用程式顯示 也會暫停使用部分資源,以便讓廣告 善加利用這些原則舉例來說,如果您呼叫以顯示 插頁式廣告,請務必暫停應用程式產生的任何音訊輸出。 您可以在「OnAdFullScreenContentClosed()」事件中繼續播放音效。 在使用者與廣告互動完畢後即可叫用。於 此外,請考慮暫時停止所有高強度運算工作,例如 遊戲迴圈。這可確保使用者不會 畫面出現速度緩慢或沒有回應的畫面,或影片延遲。
不要讓應用程式廣告氾濫。
雖然增加應用程式內插頁式廣告的顯示頻率, 不但有助於增加收益,也可能損及使用者體驗 導致點閱率降低確認使用者不常使用 幹擾,無法繼續享受您的應用程式。

其他資源