On using the official AdMob Unity plugin, how do I make the ad hidden during the game, but show up in the Main Menu and Game Over Screen ?

In the Main Menu scene, I attached the below code on an empty gameobject, and it successfully requests for ads and shows it during the entire duration of the Main Menu scene. Though, I want to the ads to not show while the game is in progress. For this, I need to call the bannerView.Hide(); function from the Main Menu empty game object, DURING the Game level is on. How do I achieve this ? How do I add a check, that whenever my Game level plays, the banner gets hidden ?
PS: Adding a check inside the Update function doesn’t work.

The code:

using System;
using UnityEngine;
using GoogleMobileAds;
using GoogleMobileAds.Api;

// Example script showing how to invoke the Google Mobile Ads Unity plugin.
public class GoogleMobileAdsDemoScript : MonoBehaviour
{
	
	private BannerView bannerView;
	private InterstitialAd interstitial;

	void Start()
	{
		DontDestroyOnLoad(this);
		RequestBanner ();
		RequestInterstitial ();
		bannerView.Show ();
if (Application.loadedLevelName == "Game") {
     bannerView.Hide ();
 }
	}
	
	private void RequestBanner()
	{
		#if UNITY_EDITOR
		string adUnitId = "unused";
		#elif UNITY_ANDROID
		string adUnitId = "ca-app-pub-xxxxxxxxxxxxxxxxxxxxxxx";
		#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);
		// 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(createAdRequest());
	}
	
	private void RequestInterstitial()
	{
		#if UNITY_EDITOR
		string adUnitId = "unused";
		#elif UNITY_ANDROID
		string adUnitId = "ca-app-pub-xxxxxxxxxxxxxxxxxxxxxxxx";
		#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);
		// 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(createAdRequest());
	}
	
	// Returns an ad request with custom ad targeting.
	private AdRequest createAdRequest()
	{
		return new AdRequest.Builder()
			.AddTestDevice(AdRequest.TestDeviceSimulator)
				.AddTestDevice("9ACD01B470951161A30C0AA4B6DA3A7D")
				.AddKeyword("game")
				.SetGender(Gender.Male)
				.SetBirthday(new DateTime(1985, 1, 1))
				.TagForChildDirectedTreatment(false)
				.AddExtra("color_bg", "9B30FF")
				.Build();
		
	}
	
	private void ShowInterstitial()
	{
		if (interstitial.IsLoaded())
		{
			interstitial.Show();
		}
		else
		{
			print("Interstitial is not ready yet.");
		}
	}
	
	#region Banner callback handlers
	
	public void HandleAdLoaded(object sender, EventArgs args)
	{
		print("HandleAdLoaded event received.");
	}
	
	public void HandleAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
	{
		print("HandleFailedToReceiveAd event received with message: " + args.Message);
	}
	
	public void HandleAdOpened(object sender, EventArgs args)
	{
		print("HandleAdOpened event received");
	}
	
	void HandleAdClosing(object sender, EventArgs args)
	{
		print("HandleAdClosing event received");
	}
	
	public void HandleAdClosed(object sender, EventArgs args)
	{
		print("HandleAdClosed event received");
	}
	
	public void HandleAdLeftApplication(object sender, EventArgs args)
	{
		print("HandleAdLeftApplication event received");
	}
	
	#endregion
	
	#region Interstitial callback handlers
	
	public void HandleInterstitialLoaded(object sender, EventArgs args)
	{
		print("HandleInterstitialLoaded event received.");
	}
	
	public void HandleInterstitialFailedToLoad(object sender, AdFailedToLoadEventArgs args)
	{
		print("HandleInterstitialFailedToLoad event received with message: " + args.Message);
	}
	
	public void HandleInterstitialOpened(object sender, EventArgs args)
	{
		print("HandleInterstitialOpened event received");
	}
	
	void HandleInterstitialClosing(object sender, EventArgs args)
	{
		print("HandleInterstitialClosing event received");
	}
	
	public void HandleInterstitialClosed(object sender, EventArgs args)
	{
		print("HandleInterstitialClosed event received");
	}
	
	public void HandleInterstitialLeftApplication(object sender, EventArgs args)
	{
		print("HandleInterstitialLeftApplication event received");
	}
	
	#endregion
}

Well, you don’t need to use all this example code, for just showing an interstitial or banner. you just need the following lines. So you can just put these in any of your scripts and call for the ad, for example game over, or exit. the order should be like this in order to show an ad;

  • create the banner or interstitial (bannerView and interstitial ad)
  • Load ad properly. (You can do these on Start, and show the ad when desired.)
  • Show the ad. (if interstitial, check if it’s loaded)
  • Load another ad (if interstitial)

Hope this helps.

using GoogleMobileAds.Api;

private BannerView bannerView;
private InterstitialAd interstitial;

bannerView = new BannerView(adUnitId, AdSize.SmartBanner, AdPosition.Bottom);
bannerView.LoadAd(new AdRequest.Builder().Build());
bannerView.Show();

interstitial = new InterstitialAd(adUnitId);
interstitial.LoadAd(new AdRequest.Builder().Build());
if (interstitial.IsLoaded()){interstitial.Show();}

if(!Player.DEAD)
this.Hide();
else
this.Show();
,Hi,
make a small script of your AdMobPlugin prefab just write

if(!Player.DEAD)
this.Hide();
else
this.Show();

well this is an old thread…but you can change show or hide banner using folleing code:-

void OnLevelWasLoaded(int Level)
	{
		if(Application.loadedLevelName=="Menu")
		{
			bannerView.Show();
		}
		else if(Application.loadedLevelName=="Menu")
			bannerView.Hide();
}

you can change loadedlevelname according to your need…Key factor is call this in OnLevelWasLoaded() function…

i purchased admob neatplugin and very easy integration for banners and interstitial ads.no bugs.

i used

https://play.google.com/store/apps/details?id=com.AcidCoffe.PixelBoyRunner2

Once you setup the plugin you should be able to use one of these two methods:

Method 1 - Add this to GoogleMobileAdsDemoScript and put GoogleMobileAdsDemoScript only on levels you want ads to appear on:

void Update()     {         
	//Destroy when leaving the level
	if (Input.GetKeyDown(KeyCode.Escape)) {
		bannerView.Hide (); 
		bannerView.Destroy();
	}
}

Method 2 - Modify GoogleMobileAdsDemoScript with this minor change:

public static BannerView bannerView;

Then add this to your levels to control hiding and showing ads:

void OnGUI()     {         

	try {	
		GoogleMobileAdsDemoScript.bannerView.Hide (); //if you want to hide it
		
		//or 
	
		GoogleMobileAdsDemoScript.bannerView.Show (); //if you want to show it 	
	}
	catch(Exception e) {
		Debug.Log(e.ToString());
	}
	
}

The GoogleMobileAdsDemoScript would only be attached to the main scene in Method 2 as it would only be loaded at the beginning and would not be destroyed at any time.

So many dummies on this thread… Either I get it all wrong, or Coolbird22 is asking for a way to kill/hide an ad that has been created in a previous scene. So why just pasting “bannerView.Hide ();” everywhere when obviously “bannerView” is not referenced anymore?!

I have the same issue, I’m gonna check Mayank Ghanshala answer, wich is the only one that seems to make sense. Coolbird22, did you find any workaround?

Julien

maybe you can have a try this
https://github.com/unity-plugins/Unity-Admob

just call Admob.Instance().removeBanner();

This is Tutorial to make advertising admob with unity 100% works…