Question by
SpeedRunSpeed · Feb 08 at 08:10 PM ·
adsadmobgameover
admob problem
When a ball is destroyed, an admob ad is displayed. I want the admob ad to be seen after all the balls are gone
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;
public class gameover : MonoBehaviour {
private List<GameObject> collidedObjects = new List<GameObject>();
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("g") && !collidedObjects.Contains(collision.gameObject))
{
collidedObjects.Add(collision.gameObject);
if (collidedObjects.Count == 3)
{
SceneManager.LoadScene("GameOverLoad");
}
AdManager.instance.ShowInterstitial();
}
}
private void Start()
{
AdManager.instance.RequestInterstitial();
}
and AdManager.cs
using UnityEngine; using GoogleMobileAds.Api; using System;
public class AdManager : MonoBehaviour { private BannerView bannerAd; static bool bannerAdRequested = false;
private InterstitialAd interstitial;
public static AdManager instance;
private void Awake()
{
if (instance == null)
{
instance = this;
}
else
{
Destroy(gameObject);
return;
}
}
void Start()
{
MobileAds.Initialize(InitializationStatus => { });
// Get singleton reward based video ad reference.
// RewardBasedVideoAd is a singleton, so handlers should only be registered once.
if (bannerAdRequested)
return;
this.RequestBanner();
bannerAdRequested = true;
}
private AdRequest CreateAdRequest()
{
return new AdRequest.Builder().Build();
}
private void RequestBanner()
{
string adUnitId = "my id";
this.bannerAd = new BannerView(adUnitId, AdSize.SmartBanner, AdPosition.Bottom);
// Clean up banner ad before creating a new one.
if (this.bannerAd != null)
{
this.bannerAd.Destroy();
}
// Create a 320x50 banner at the top of the screen.
this.bannerAd = new BannerView(adUnitId, AdSize.SmartBanner, AdPosition.Bottom);
// Load a banner ad.
this.bannerAd.LoadAd(this.CreateAdRequest());
}
public void RequestInterstitial()
{
string adUnitId = "my id";
// Clean up interstitial ad before creating a new one.
if (this.interstitial != null)
this.interstitial.Destroy();
// Create an interstitial.
this.interstitial = new InterstitialAd(adUnitId);
// Load an interstitial ad.
this.interstitial.LoadAd(this.CreateAdRequest());
}
public void ShowInterstitial()
{
if (this.interstitial.IsLoaded())
{
interstitial.Show();
}
else
{
Debug.Log("Inerstitial Ad is not ready yet");
}
}
}
Comment