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

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

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

前提条件

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

次のサンプルコードには、広告ユニット ID が含まれています。この ID を使用して、 テスト広告。このオブジェクトは、特定のリソースではなく、テスト広告を返すように 安全に使用できます。

ただし、 独自の広告ユニットを作成し アプリで使用する ID。デバイスをテスト用として明示的に設定します デバイス 必要があります。

Android

ca-app-pub-3940256099942544/1033173712

iOS

ca-app-pub-3940256099942544/4411468910

Mobile Ads SDK を初期化する

広告を読み込む前に、以下を呼び出して Mobile Ads SDK を初期化します。 MobileAds.Initialize()。この処理は 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. 次のインタースティシャル広告をプリロードする

インタースティシャル広告を読み込む

インタースティシャル広告を読み込むには、Load() InterstitialAd クラス。読み込みメソッドでは、広告ユニット ID、 AdRequest オブジェクトと、次の読み込みを行う完了ハンドラ は、広告の読み込みが成功または失敗したときに呼び出されます。読み込まれた InterstitialAd オブジェクトは、 完了ハンドラがあります。以下の例は、Python や Curl で 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;
          });
  }

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

読み込まれたインタースティシャル広告を表示するには、Show() InterstitialAd インスタンス。広告は 1 回につき 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);
    };
}

インタースティシャル広告をクリーンアップする

InterstitialAd が終了したら、次のように設定します。 参照を削除する前に、必ず Destroy() メソッドを呼び出します。

_interstitialAd.Destroy();

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

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

インタースティシャル広告は 1 回限りのオブジェクトです。つまりインタースティシャル広告を実装すると そのオブジェクトは再使用できません。別のインタースティシャル広告をリクエストするには、 新しい InterstitialAd オブジェクトを作成します。

次のインプレッションの機会に備えてインタースティシャル広告を用意するには、 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();
    };
}

ベスト プラクティス

インタースティシャル広告がアプリに適した広告タイプかどうかを判断する。
インタースティシャル広告は、画面の切り替わりがあるアプリで最適に機能します。 画像の共有や ゲームレベルでは、そのようなポイントが生まれます。学習計画で注力すべきポイントを インタースティシャル広告を適切に表示するためのフローと、ユーザーが反応する可能性を把握します。
インタースティシャル広告を表示する際にアクションを一時停止する。
インタースティシャル広告にはさまざまなタイプがあり、テキスト、 画像、動画などですアプリの画面にコードを表示する際は、 リソースの使用を中断して広告が 活用してください。たとえば、スペースを表示するための呼び出しを行うと、 アプリからの音声出力は、必ず一時停止してください。 OnAdFullScreenContentClosed() イベントで音声の再生を再開できます。 ユーザーが広告の操作を終了したときに呼び出すことができます。イン さらに、負荷の高い計算タスクを一時的に停止することを 広告が表示されている間、ゲームループに入ります。これにより ユーザーは グラフィックが遅い、反応しない、動画がスムーズに再生されない。
大量の広告をユーザーに表示することは避けてください。
アプリでインタースティシャル広告の表示頻度を増やすと、 ユーザーエクスペリエンスの低下にも つながります クリック率の低下につながりますユーザーがそれほど頻繁にアクセスしないようにします。 中断されることがなくなります。

参考情報

で確認できます。 * サンプル ユースケース