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
1
Question by jerickdlee · Jun 19, 2014 at 04:18 PM · admobbannerglobalvariables

Google Mobile Ads Unity Plugin - global bannerView

Hi Everyone

I am working with my very first game, and I am now in the monetization part. I successfully integrated Chartboost and admob but I am having a very hard time controlling admob

I used the Google Mobile Ads Unity API (https://github.com/googleads/googleads-mobile-plugins/tree/master/unity)

Without the hideadmob() function (and the script under Update calling it) , I was able to make the banner appear using the code below:

 using UnityEngine;
 using System.Collections;
 using GoogleMobileAds;
 using GoogleMobileAds.Api;
 public class admobhandler : MonoBehaviour {
     public BannerView bannerView;
     public int ctr = 0;
   
     void Start () {
         BannerView bannerView = new BannerView("xxxxxxxxx", AdSize.Banner, AdPosition.Top);
         AdRequest request = new AdRequest.Builder().Build();
         bannerView.LoadAd(request);
     }
   
     void Update () {
         if (ctr == 0)
         {
             ctr = 1;
             hideadmob();
         }
   
     }
     void hideadmob()
     {
         Debug.Log("Went in here.");
         bannerView.Hide ();
     }
 }


For some reason, the variable bannerView is not globalized within the class, even if I have declared it outside the methods. I receive the following error:

NullReferenceException: Object reference not set to an instance of an object admobhandler.hideadmob () (at Assets/Scripts/admobhandler.cs:39) admobhandler.Update () (at Assets/Scripts/admobhandler.cs:30)

The "Went in here" log is displayed though.

I have no problems globalizing within the class with other Unity type variables like Transform, Animation, etc.

Hopefully someone can help and thanks everyone :)

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 19, 2014 at 04:45 PM 0
Share

I'm stuck on a very similar problem too. Here is the question I posted yesterday but have recd no answers yet :( - Here

avatar image koray1396 · Jun 19, 2014 at 05:02 PM 0
Share

coolbird, you hide the banner after loading the scene "Game", so it will stay in $$anonymous$$ain $$anonymous$$enu scene. you should decide when to hide it. You can start a counter and show it for 3 seconds etc...

and jerick, you can not declare what you have declared earlier, in this case bannerView. Can you try this? hideadmob might not be able to find bannerView.

 public class admobhandler : $$anonymous$$onoBehaviour {
     public BannerView bannerView;
     public int ctr = 0;
  
     void Start () {
         bannerView = new BannerView("xxxxxxxxx", AdSize.Banner, AdPosition.Top);
         AdRequest request = new AdRequest.Builder().Build();
         bannerView.LoadAd(request);
     }

avatar image coolbird22 · Jun 19, 2014 at 05:30 PM 0
Share

Extremely sorry to hijack your question thread here, jerick :( @koray1396 - I need the banner to be visible for however long the $$anonymous$$ain$$anonymous$$enu is visible for, so a counter can't work. I'll also be loading an interstitial, that I want to show in the Game Over screen that follows the Game scene. How do I call the - interstitial.LoadAd() function for the interstitial I requested in the $$anonymous$$ain $$anonymous$$enu screen ? Basically, how do I call a banner/interstitial from the API, so if I say bannerView.Hide(), whichever banner is loaded in the Game scene will get hidden.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by wuym67123 · Feb 21, 2017 at 07:34 AM

using UnityEngine; using System.Collections; using admob; public class admobdemo : MonoBehaviour {

 // Use this for initialization
 void Start () {
     Debug.Log("start unity demo-------------");
      initAdmob();
 }
 
 // Update is called once per frame
 void Update () {
 
 }
 Admob ad;
 //bool isAdmobInited = false;
 void initAdmob()
 {
     
       //  isAdmobInited = true;
          ad = Admob.Instance();
         ad.bannerEventHandler += onBannerEvent;
         ad.interstitialEventHandler += onInterstitialEvent;
         ad.rewardedVideoEventHandler += onRewardedVideoEvent;
         ad.nativeBannerEventHandler += onNativeBannerEvent;
         ad.initAdmob("ca-app-pub-3940256099942544/2934735716", "ca-app-pub-3940256099942544/4411468910");
     //   ad.setTesting(true);
         ad.setGender(AdmobGender.MALE);
         string[] keywords = { "game","crash","male game"};
         ad.setKeywords(keywords);
         Debug.Log("admob inited -------------");
     
 }
 void OnGUI(){
     if (GUI.Button(new Rect(120, 0, 100, 60), "showInterstitial"))
     {
       
         if (ad.isInterstitialReady())
         {
             ad.showInterstitial();
         }
         else
         {
             ad.loadInterstitial();
         }
     }
     if (GUI.Button(new Rect(240, 0, 100, 60), "showRewardVideo"))
     {
         
         if (ad.isRewardedVideoReady())
         {
             ad.showRewardedVideo();
         }
         else
         {
             
                 //ad.loadRewardedVideo("ca-app-pub-3120877784109998/4714341465");
             ad.loadRewardedVideo("ca-app-pub-4622924889224894/8984406965");
         }
     }
     if (GUI.Button(new Rect(0, 100, 100, 60), "showbanner"))
     {
         Admob.Instance().showBannerRelative(AdSize.SmartBanner, AdPosition.BOTTOM_CENTER, 0);
     }
     if (GUI.Button(new Rect(120, 100, 100, 60), "showbannerABS"))
     {
         Admob.Instance().showBannerAbsolute(AdSize.Banner, 0, 300);
     }
     if (GUI.Button(new Rect(240, 100, 100, 60), "removebanner"))
     {
         Admob.Instance().removeBanner();
     }
     string nativeBannerID = "ca-app-pub-6908989844804937/5541215009";//
    // string nativeBannerID = "ca-app-pub-3940256099942544/2562852117";//google
     if (GUI.Button(new Rect(0, 200, 100, 60), "showNative"))
     {
         
         Admob.Instance().showNativeBannerRelative(new AdSize(320,120), AdPosition.BOTTOM_CENTER, 0,nativeBannerID);
     }
     if (GUI.Button(new Rect(120, 200, 100, 60), "showNativeABS"))
     {
         Admob.Instance().showNativeBannerAbsolute(new AdSize(320,120), 0, 300, nativeBannerID);
     }
     if (GUI.Button(new Rect(240, 200, 100, 60), "removeNative"))
     {
         Admob.Instance().removeNativeBanner();
     }
 }
 void onInterstitialEvent(string eventName, string msg)
 {
     Debug.Log("handler onAdmobEvent---" + eventName + "   " + msg);
     if (eventName == AdmobEvent.onAdLoaded)
     {
         Admob.Instance().showInterstitial();
     }
 }
 void onBannerEvent(string eventName, string msg)
 {
     Debug.Log("handler onAdmobBannerEvent---" + eventName + "   " + msg);
 }
 void onRewardedVideoEvent(string eventName, string msg)
 {
     Debug.Log("handler onRewardedVideoEvent---" + eventName + "   " + msg);
 }
 void onNativeBannerEvent(string eventName, string msg)
 {
     Debug.Log("handler onAdmobNativeBannerEvent---" + eventName + "   " + msg);
 }

}

https://github.com/unity-plugins/Unity-Admob/blob/master/AdmobPluginRes/admobdemo.cs

New demo for new googleads unity plugin

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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Admob banner is not refreshing because of Show and Hide. 0 Answers

Android Banner disappears after touching the screen 2 Answers

AdMob Custom Banner Size 1 Answer

[Admob] Hiding Banner instantly 1 Answer

How to Support AdMob Banner for Landscape and Portrait Modes 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