【Unity】Admobインタースティシャル広告の終了を受け取る
(2024/10/06)
Android向けのスマホゲームにAdmobを利用してインタースティシャル広告を実装したのですが、広告が現れたと思ったらすぐに消えてゲームが停止してしまいました。
どうも広告リクエストの直前や直後にシーンのロードなどの処理が書かれていると広告が止まってしまうようなので処理の順番と待機を考えます。
フェードアウトしてシーンの読み込み、遷移を済ませる。
(この時点で画面は真っ暗)
↓
広告リクエスト
↓
広告終了したらフェードイン開始
この流れをSceneManagerなどのコルーチンの中で待機、開始をさせました。シーン遷移の処理については割愛します。
というわけで広告終了を受け取る必要があるので調べました。
こちらGoogle for developersのAdmob Mobile ads SDK(Unity)に記載されているコードでインタースティシャル広告の動作を確認できます。adomobは初見だったのでこれを丸っとコピペして使っています。
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);
};
}
この一番下に広告終了時に行う処理を追記します。↓
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.");
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);
LoadInterstitialAd();
};
//追記
interstitialAd.OnAdFullScreenContentClosed+=HandleOnAdClosed;
}
//追記 終了後の処理
public void HandleOnAdClosed()
{
//ここに処理を書く
}
LoadInterstitialAd();よりしたが追記部分になります。
OnAdFullScreenContentClosed+=の後にメソッド名なり処理を書けば広告終了時に実行してくれます。
私の場合はここでSceneManagerに終了を知らせるようにしています。
以上です。ちょっと説明が下手だったらスイマセン。