Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by coolbird22 · Jun 17, 2014 at 06:43 PM · adsadmob

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
 }
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image coolbird22 · Jun 18, 2014 at 04:12 PM 0
Share

Anyone with any experience with Ad$$anonymous$$ob ? $$anonymous$$indly help ! :(

avatar image Mayank Ghanshala · Nov 20, 2014 at 07:09 AM 0
Share
 //$$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();
 
 }
 
 }
 
 
 
 


avatar image WardPeeters · Sep 07, 2015 at 09:09 PM 0
Share

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?)

8 Replies

· Add your reply
  • Sort: 
avatar image
2

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image PJisAnarchist · Sep 02, 2015 at 11:22 PM 0
Share

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

avatar image
0

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();}







Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image coolbird22 · Jun 20, 2014 at 02:53 AM 0
Share

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 .

avatar image koray1396 · Jun 21, 2014 at 07:30 AM 0
Share

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();

avatar image carrollh · Jun 28, 2014 at 11:46 PM 0
Share

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());

avatar image koray1396 · Jun 29, 2014 at 07:26 AM 0
Share

oops, that's right. thanks.

avatar image
0

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();

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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...

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

33 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges