インタースティシャル広告

インタースティシャル広告は、ホストアプリのインターフェースを覆うようにフルスクリーンで表示される広告です。通常、ゲームのレベルが切り替わる合間の一時停止時など、アプリの操作中に画面が自然に切り替わるタイミングで表示されます。アプリにインタースティシャル広告が表示されると、ユーザーは広告をタップしてリンク先に移動するか、広告を閉じてアプリに戻るかを選択できます。

このガイドでは、インタースティシャル広告を Unity アプリに組み込む方法について説明します。

前提条件

必ずテスト広告でテストする

次のサンプルコードには、テスト広告のリクエストに使用できる広告ユニット ID が含まれています。この ID は、すべてのリクエストに対して実際の広告ではなくテスト広告を返すように設定されており、安全に使用できます。

ただし、Ad Manager 管理画面でアプリを登録し、アプリで使用する独自の広告ユニット ID を作成したら、開発中に明示的にデバイスをテストデバイスとして設定します。

/6499/example/interstitial

Mobile Ads SDK を初期化する

広告を読み込む前に MobileAds.Initialize() を呼び出して、アプリで Mobile Ads SDK を初期化します。この処理は 1 回だけ行います(アプリの起動時に行うのが理想的です)。

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

インタースティシャル広告を表示する

読み込まれたインタースティシャル広告を表示するには、AdManagerInterstitialAd インスタンスの Show() メソッドを呼び出します。広告は読み込みごとに 1 回表示できます。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();

これにより、オブジェクトが使用されなくなり、占有していたメモリを再利用できることがプラグインに通知されます。このメソッドを呼び出さないと、メモリリークが発生します。

次のインタースティシャル広告をプリロードする

インタースティシャル広告は 1 回限りのオブジェクトです。つまり、一度インタースティシャル広告を表示すると、そのオブジェクトは二度と使用できません。別のインタースティシャル広告をリクエストするには、新しい 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 を使用します。これらのイベントは、広告ライフサイクルのあらゆるタイミングで(読み込みが呼び出される前でも)発生する可能性があります。

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() イベントで再開できます。このイベントは、ユーザーが広告の操作を終了したときに呼び出すことができます。また、広告を表示している間は、ゲームループなどの負荷の高い計算処理を一時的に停止することをおすすめします。これにより、グラフィックが遅くなったり、応答しなくなったり、動画がスムーズに再生されなかったりすることはありません。
大量の広告をユーザーに表示することは避けてください。
アプリでのインタースティシャル広告の表示頻度を増やすことは、増収に効果的な方法のように思えますが、ユーザー エクスペリエンスが低下し、クリック率が低下する可能性もあります。ユーザーがアプリを楽しめなくなるほど頻繁に邪魔されないようにします。

補足資料