インタースティシャル広告は、ホストアプリのインターフェース上に全画面表示される広告です。通常は、ゲームレベルをクリアした後の合間など、アプリの操作中に画面が切り替わるタイミングで表示されます。アプリにインタースティシャル広告が表示されると、ユーザーは広告をタップしてリンク先に移動するか、広告を閉じてアプリに戻るかを選択できます。事例紹介
このガイドでは、インタースティシャル広告を Unity アプリに組み込む方法について説明します。
前提条件
- スタートガイドの手順を完了していること。
テストは必ずテスト広告で行う
下のサンプルコードには、テスト広告のリクエストに使用できる広告ユニット ID が含まれています。この ID は、どのリクエストに対しても実際の広告ではなくテスト広告を返すように設定されており、安全に使用できます。
ただし、AdMob 管理画面でアプリを登録し、アプリで使用する独自の広告ユニット ID を作成した後は、開発中に使用するデバイスを明示的にテストデバイスとして設定する必要があります。
Android
ca-app-pub-3940256099942544/1033173712
iOS
ca-app-pub-3940256099942544/4411468910
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.
});
}
}
メディエーションを使用している場合、すべてのメディエーション アダプタを確実に初期化できるよう、広告の読み込みはコールバックが発生してから行います。
実装
インタースティシャル広告を組み込む主な手順は、以下のとおりです。
- インタースティシャル広告を読み込む
- インタースティシャル広告を表示する
- インタースティシャル広告のイベントをリッスンする
- インタースティシャル広告をクリーンアップする
- 次のインタースティシャル広告をプリロードする
インタースティシャル広告を読み込む
インタースティシャル広告の読み込みは、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.Builder()
.AddKeyword("unity-admob-sample")
.Build();
// 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()
メソッドを呼び出します。読み込んだ広告は 1 回だけ表示できます。広告を表示する準備ができていることを確認するには、CanShowAd()
メソッドを使用します。
/// <summary>
/// Shows the interstitial ad.
/// </summary>
public void ShowAd()
{
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 ad)
{
// Raised when the ad is estimated to have earned money.
ad.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.
ad.OnAdImpressionRecorded += () =>
{
Debug.Log("Interstitial ad recorded an impression.");
};
// Raised when a click is recorded for an ad.
ad.OnAdClicked += () =>
{
Debug.Log("Interstitial ad was clicked.");
};
// Raised when an ad opened full screen content.
ad.OnAdFullScreenContentOpened += () =>
{
Debug.Log("Interstitial ad full screen content opened.");
};
// Raised when the ad closed full screen content.
ad.OnAdFullScreenContentClosed += () =>
{
Debug.Log("Interstitial ad full screen content closed.");
};
// Raised when the ad failed to open full screen content.
ad.OnAdFullScreenContentFailed += (AdError error) =>
{
Debug.LogError("Interstitial ad failed to open full screen content " +
"with error : " + error);
};
}
インタースティシャル広告をクリーンアップする
使い終わった InterstitialAd
への参照を破棄する前に、必ず Destroy()
メソッドを呼び出してください。
interstitialAd.Destroy();
これによって、そのオブジェクトはもう使用されておらず、オブジェクトが占有していたメモリを回収しても問題ないことを、プラグインに通知できます。このメソッドを呼び出さないとメモリリークが発生します。
次のインタースティシャル広告をプリロードする
インタースティシャル広告は使い捨てオブジェクトです。一度インタースティシャル広告を表示した後、同じオブジェクトを再度使うことはできません。次にインタースティシャル広告をリクエストするには、新しい InterstitialAd
オブジェクトを作成する必要があります。
次のインプレッションの機会に備えてインタースティシャル広告を用意するには、広告イベント OnAdFullScreenContentClosed
または OnAdFullScreenContentFailed
が発生した時点でインタースティシャル広告をプリロードするようにします。
private void RegisterReloadHandler(InterstitialAd ad)
{
// Raised when the ad closed full screen content.
ad.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.
ad.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()
イベントで再開するといいでしょう。また、負荷の大きい計算処理(ゲームループなど)も、広告の表示中は一時的に止めておくことを検討しましょう。グラフィックの遅延や反応の遅れ、動画の乱れなどを防ぐことができます。 - 過度に広告を表示しないよう注意しましょう。
- インタースティシャル広告の表示頻度を上げることは、収益を増やすうえでは魅力的に思えるかもしれませんが、ユーザー エクスペリエンスの悪化やクリック率低下を招くおそれもあります。ユーザーがアプリを楽しめなくなるほど頻繁に広告を挟むことは避けましょう。
その他の参考情報
サンプル
- HelloWorld の例: すべての広告フォーマットの最小限の実装