- Home /
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 :)
I'm stuck on a very similar problem too. Here is the question I posted yesterday but have recd no answers yet :( - Here
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);
}
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.
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