public class AdmobManager : MonoBehaviour
{
private string _bannerAdUnitId = "ca-app-pub-3940256099942544/6300978111"; //test
private string _InterstitialAdUnitId = "ca-app-pub-3940256099942544/1033173712";
private InterstitialAd interstitialAd;
BannerView _bannerView;
[SerializeField] MoveNextScene moveNextScene;
void Start ()
{
if (!InitAds.IsInitAds)
{
// Initialize the Google Mobile Ads SDK.
MobileAds.Initialize((InitializationStatus initStatus) =>
{
// This callback is called once the MobileAds SDK is initialized.
});
MobileAds.RaiseAdEventsOnUnityMainThread = true;
InitAds.SetInitAdsToTrue();
}
LoadInterstitialAd();
LoadBannerAd();
}
public void StartInterstitialAD()
{
ShowAd();
}
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();
};
}
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);
};
}
/// <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.");
}
}
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();
adRequest.Keywords.Add("unity-admob-sample");
// send the request to load the ad.
InterstitialAd.Load(_InterstitialAdUnitId, 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;
});
}
}
The ShowAd
method is called when click a button.
and show Ad… It’s normal up to this point.
but when close the ads, events is not working. (RegisterEventHandlers
(InterstitialAd ad))
It doesn’t show logs that Debug.Log("Interstitial ad full screen content closed.");
.