- Home /
Showing interstitial ads when Player dies
Hi everyone,
I just finished my game and i was able to implement banner ads. But I have a problem with interstitial ads. I can't find a way to show ads after player dies. Can anyone share how I can do it?
Below is the interstitial part of my script:
private void RequestInterstitial() { //Determine which ID will be used according to the choosen platform. string adUnitId = "unexpected_platform"; #if UNITY_ANDROID adUnitId = androidInterstitialId; #elif UNITY_IPHONE adUnitId = iphoneInterstitialId; #endif
// Initialize an InterstitialAd.
interstitial = new InterstitialAd(adUnitId);
// Create an empty ad request.
AdRequest request = new AdRequest.Builder().AddTestDevice(AdRequest.TestDeviceSimulator).Build();
// Load the interstitial with the request.
interstitial.LoadAd(request);
// Interstitial Ad events callbacks.
interstitial.OnAdFailedToLoad += HandleOnInterstitialAdFailedToLoad;
interstitial.OnAdLoaded += HandleOnInterstitialAdLoaded;
interstitial.OnAdClosed += HandleOnInterstitialAdClosed;
}
private void HandleOnInterstitialAdFailedToLoad(object sender, AdFailedToLoadEventArgs args) {
StartCoroutine (ReRequestInterstitial ());
}
private void HandleOnInterstitialAdLoaded(object sender, EventArgs args) {
if (OnInterstitialAdLoaded != null)
OnInterstitialAdLoaded ();
}
private void HandleOnInterstitialAdClosed(object sender, EventArgs args) {
// Request new interstitial ad.
interstitial.Destroy();
RequestInterstitial();
}
private IEnumerator ReRequestInterstitial() {
// Rerequest a new interstitial ad after 30 seconds.
yield return new WaitForSeconds (30);
interstitial.Destroy ();
RequestInterstitial ();
}
/// <summary>
/// Shows the Interstitial Ad if loaded (e.g. Used at game over event).
/// </summary>
public void ShowInterstitial() {
string adsEnabled = PlayerPrefs.GetString("AdsEnabled");
if (adsEnabled == "Yes" && interstitial.IsLoaded())
interstitial.Show();
}
/// <summary>
/// Returns the state of the Interstitial Ad.
/// </summary>
/// <returns>bool</returns>
public bool IsInterstitialLoaded() {
return interstitial.IsLoaded ();
}
Comment