using UnityEngine;
using GoogleMobileAds.Api;
using System;
public class AdmobUnitInterstitial : AdmobUnitBase
{
private InterstitialAd interstitialAd;
public bool IsReady
{
get
{
if (AdmobManager.Instance.IsReady == false)
{
return false;
}
return interstitialAd != null && interstitialAd.CanShowAd();
}
}
protected override void Initialize()
{
LoadInterstitialAd();
}
public void ShowInterstitial()
{
if (IsReady)
{
interstitialAd.Show();
}
else
{
Debug.Log("Interstitial ad is not ready yet.");
}
}
private void LoadInterstitialAd()
{
// Clean up the old ad before loading a new one.
if (interstitialAd != null)
{
interstitialAd.Destroy();
interstitialAd = null;
}
var adRequest = new AdRequest();
InterstitialAd.Load(UnitID, adRequest,
(InterstitialAd ad, LoadAdError error) =>
{
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;
RegisterEventHandlers(interstitialAd);
});
}
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();
};
}
}
スライム
スクリプトを保存して、エラーが出てないかなど確認して下さい。
フェアリー
ベースクラスが無いとエラーが出てしまうので過去資料も参考にしてくださいね
テスト用スクリプトを作成
実際に広告を使う場合、外部から表示依頼を行います。
インタースティシャル広告が出せるようになると表示ボタンが押せるようになる
表示ボタンを押すと広告が表示される
using UnityEngine;
using UnityEngine.UI;
public class TestInterstitial : MonoBehaviour
{
public Button showInterstitialButton;
public AdmobUnitInterstitial admobUnitInterstitial;
private void Start()
{
showInterstitialButton.interactable = false;
showInterstitialButton.onClick.AddListener(() =>
{
admobUnitInterstitial.ShowInterstitial();
showInterstitialButton.interactable = false;
});
}
private void Update()
{
showInterstitialButton.interactable = admobUnitInterstitial.IsReady;
}
}
コメント
コメント一覧 (3件)
リワード広告の実装についても解説していただきたいです!
その相談、Youtubeも含めてたくさん来てます。そろそろ対応しますので、しばし待たれよ!
[…] 基本的にはインタースティシャルのときと同じ。 […]