In my game I want to show interstitials when someone loses. I had banner ads working great, but then I changed my code to adjust for interstitials, and now it takes a while for my banner ad to show up, and when it does it lags for a second. I also am having trouble figuring out how to get my interstitials to pop up and then request another one. I have an ad manager and the way I am telling my game when to display an ad is when the death screen pops up, then an ad should be displayed, and when the death screen goes away, I want to request another ad. So far, I haven’t been able to get it to work. Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GoogleMobileAds.Api;
public class AdManager : MonoBehaviour {
//public bool dead;
public int showAd;
public InterstitialAd interstitial;
public BannerView bannerView;
void Start () {
RequestBanner ();
showAd = Random.Range (1, 3);
RequestInterstitial ();
}
void Awake () {
DontDestroyOnLoad (this);
}
void Update () {
}
void ShowInterstitial () {
if (GameObject.Find ("Canvas1").GetComponent <Canvas>().enabled) {
if (showAd == 1) {
if (interstitial.IsLoaded ()) {
interstitial.Show ();
interstitial.Destroy ();
}
}
}
if (!GameObject.Find ("Canvas1").GetComponent <Canvas> ().enabled) {
RequestInterstitial ();
}
}
public void RequestInterstitial()
{
#if UNITY_EDITOR
string adUnitId = "unused";
#elif UNITY_ANDROID
string adUnitId = "xxxxxxxxxxxx";
#elif UNITY_IPHONE
string adUnitId = "xxxxxxxxxxxx";
#else
string adUnitId = "unexpected_platform";
#endif
// Initialize an InterstitialAd.
interstitial = new InterstitialAd(adUnitId);
// Create an empty ad request.
AdRequest request = new AdRequest.Builder()
.AddTestDevice ("xxxxxxxxxxxx")
.Build();
// Load the interstitial with the request.
interstitial.LoadAd(request);
}
void RequestBanner()
{
#if UNITY_EDITOR
string adUnitId = "unused";
#elif UNITY_ANDROID
string adUnitId = "xxxxxxxxxxxxx";
#elif UNITY_IPHONE
string adUnitId = "xxxxxxxxxxxxx";
#else
string adUnitId = "unexpected_platform";
#endif
// Create a 320x50 banner at the top of the screen.
bannerView = new BannerView(adUnitId, AdSize.Banner, AdPosition.Bottom);
// Create an empty ad request.
AdRequest request = new AdRequest.Builder()
//.AddTestDevice(AdRequest.TestDeviceSimulator) // Simulator.
.AddTestDevice("xxxxxxxxxxx") // My test device.
.Build();
// Load the banner with the request.
bannerView.LoadAd(request);
}
}