- Home /
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
}
Anyone with any experience with Ad$$anonymous$$ob ? $$anonymous$$indly help ! :(
//$$anonymous$$ake an Singleton instance of this class
public class Google$$anonymous$$obileAdsDemoScript : $$anonymous$$onoBehaviour
{
public static Google$$anonymous$$obileAdsDemoScript Instance;
public void Awake()
{
Instance = this;
}
public void HideBannerView()
{
bannerView.Hide();
}
public void ShowBannerView()
{
bannerView.Show();
}
}
Now In other Scene in any monobehaviour class which is not destroying till another scene you can do like in start
public class Leve$$anonymous$$anager : $$anonymous$$onoBehaviour
{
public void Start()
{
if(Google$$anonymous$$obileAdsDemoScript.Instance!=null)
Google$$anonymous$$obileAdsDemoScript.Instance.ShowBannerView();
//or Google$$anonymous$$obileAdsDemoScript.Instance.ShowBannerView();
}
}
I have a problem with showing a banner using this democode.. Erros says: Type Google$$anonymous$$obileAds.Api.BannerView' does not contain a definition for
IsLoaded' and no extension method IsLoaded' of type
Google$$anonymous$$obileAds.Api.BannerView' could be found (are you missing a using directive or an assembly reference?)
Answer by PJisAnarchist · Sep 02, 2015 at 10:23 PM
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
Oh, and I found the solution on the Internetz. It $$anonymous$$UST become the way to request ads in Unity. $$anonymous$$ayank was pointing in the right direction. Check Eric's answer, on the 21/06/2014 here: https://groups.google.com/forum/#!topic/google-admob-ads-sdk/ILPfzBfdWvQ
It precisely answer that thread. Julien
Answer by koray1396 · Jun 19, 2014 at 06:03 PM
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();}
I'm able to make the banners/interstitials show, but the main problem I'm having is to hide the ads in other scenes. Suppose I requested a banner and showed the banner in the $$anonymous$$ain $$anonymous$$enu(Scene 0), then when I start playing the game(Scene 1), I wish to hide the banner, so how do I call the 'bannerView.Hide()' while the game is in progress ? Is there like a generic function available, that I can call and just basically say that if any ad is visible, just hide it. I even asked on the Official Ad$$anonymous$$ob Unity plugin page by Google and Eric Leichtenschlag, one of the devs who helped author the plugin, had a suggestion that I destroy the ad before moving into the game, but that this might increase the number of request ad calls. The link to my question is HERE. I don't want to make these many calls for the ad. Eric, too, is unsure about how to go about with multiple scenes in question. It would be great if you could check out the question and devise a workaround solution for this. Thanks @koray1396 .
Okay, you can create a seperate gameobject, for advertisements, or not, it's up to you. also you can create seperate objects, or create one for the whole game, again it's up to you. What I can suggest is;
Create an empty gameObject and attach this Google$$anonymous$$obileAdsDemoScript to it.
Delete the extra lines from adRequest, you just need, AdRequest.Builder().Build(), if you don't have any specific target.
After interstitial.Show(), add interstitial.LoadAd(createAdRequest()) //You want the next one to be ready
In start, delete bannerView.Show() and bannerView.Hide()
$$anonymous$$ake ShowInterstitial public, and create another public function for showing the banner.
Now you can show an from other scripts wiith GameObject.Find("GameObjectName").GetComponent().ShowInterstitial() or GameObject.Find("GameObjectName").GetComponent().ShowBanner();
I'm mostly being nit picky here, but your examples are missing the 'new' keyword on lines 7 and 11. Should read: .LoadAd(new AdRequest.Builder().Build());
Answer by izazhaque · Sep 19, 2014 at 10:30 PM
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();
Answer by palash_bhowmick1 · Oct 21, 2014 at 06:13 PM
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...
Answer by marvelshaan · Nov 20, 2014 at 06:37 AM
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
Your answer
Follow this Question
Related Questions
Google banner Ads display problem 0 Answers
do dummy ads mean real ads will load 0 Answers
Admob ads are not working. 1 Answer
Admob banner not showing up? 2 Answers
why i can't find Android Manifest inside stagingArea 0 Answers