I want to show interstitial ads after 5 times of gameplay. I have written the below code and added it to a game object. So that when the user clicks the restart button 5 times (ie, after 5 gameplays) an interstitial ad should show up but its not. Banner ads are showing correctly but interstitial ads are not showing?
using System;
using UnityEngine;
using GoogleMobileAds;
using GoogleMobileAds.Api;
public class GoogleAdmob : MonoBehaviour {
private int count; private BannerView bannerView; private InterstitialAd interstitial;
void Start(){
count = PlayerPrefs.GetInt("Count");
RequestBanner ();
RequestInterstitial ();
bannerView.Show ();
if (count > 5) {
ShowInterstitial();
count = 0;
PlayerPrefs.SetInt ("Count", count);
}
count += 1;
PlayerPrefs.SetInt ("Count", count);
}
void Update(){
if(Input.GetKeyDown(KeyCode.Escape)){
bannerView.Destroy();
interstitial.Destroy();
Application.LoadLevel("MainMenu");
}
}
private void RequestBanner() {
#if UNITY_ANDROID
string adUnitId = "ca-app-pub-xxxxxxxxxxxxxxxxxxxxxxxxxx";
#elif UNITY_IPHONE
string adUnitId = "INSERT_IOS_BANNER_AD_UNIT_ID_HERE";
#else
string adUnitId = "unexpected_platform";
#endif
// Create a 320x50 banner at the top of the screen.
bannerView = new BannerView(adUnitId, AdSize.SmartBanner, AdPosition.Bottom);
AdRequest request = new AdRequest.Builder().Build();
// Register for ad events.
bannerView.AdLoaded += HandleAdLoaded;
bannerView.AdFailedToLoad += HandleAdFailedToLoad;
bannerView.AdOpened += HandleAdOpened;
bannerView.AdClosing += HandleAdClosing;
bannerView.AdClosed += HandleAdClosed;
bannerView.AdLeftApplication += HandleAdLeftApplication;
// Load a banner ad.
bannerView.LoadAd(request);
}
private void RequestInterstitial() {
#if UNITY_ANDROID
string adUnitId = "ca-app-pub-xxxxxxxxxxxxxxxxxxxxxxxxxx";
#elif UNITY_IPHONE
string adUnitId = "INSERT_IOS_INTERSTITIAL_AD_UNIT_ID_HERE";
#else
string adUnitId = "unexpected_platform";
#endif
// Create an interstitial.
interstitial = new InterstitialAd(adUnitId);
AdRequest request = new AdRequest.Builder().Build();
// Register for ad events.
interstitial.AdLoaded += HandleInterstitialLoaded;
interstitial.AdFailedToLoad += HandleInterstitialFailedToLoad;
interstitial.AdOpened += HandleInterstitialOpened;
interstitial.AdClosing += HandleInterstitialClosing;
interstitial.AdClosed += HandleInterstitialClosed;
interstitial.AdLeftApplication += HandleInterstitialLeftApplication;
// Load an interstitial ad.
interstitial.LoadAd(request);
}
// Returns an ad request with custom ad targeting.
private void ShowInterstitial() {
if (interstitial.IsLoaded()) {
interstitial.Show();
}
}