Hey everyone,
I’ve rewritten my Ad Manager code so many times trying to figure it out, im starting to think in circles.
I have 4 scenes in my game in which i would like to display ads, the rest of the scenes i want nothing to be display.
My very first scene works fine, no Ads gets displayed as soon as i get to another scene the code gets messed up.
Im calling the HideBanner() and ShowBanner() functions infinitely and im not sure how to get out of this endless loop.
Im using 2 booleans below as in Update() the admob.HideBanner() and admob.ShowBanner() will get called every frame and i want it to be called only once per scene and reset when i go to another scene.
Im sure this is a easy fix however the syntax is beyond me. Please assist.
using UnityEngine;
using System.Collections;
public class AdManager : MonoBehaviour
{
private const string AD_UNIT_ID = "";
private AdMobPlugin admob;
public bool hidden = false;
public bool callHiddenOnce = true;
public void Awake()
{
DontDestroyOnLoad (this);
}
void Start ()
{
admob = GetComponent<AdMobPlugin>();
admob.CreateBanner (AD_UNIT_ID, AdMobPlugin.AdSize.SMART_BANNER, true);
admob.RequestAd ();
admob.ShowBanner ();
admob.HideBanner();
}
void Update()
{
//Scenes i do not want to display ads
if(callHiddenOnce && Application.loadedLevelName != "2D_Game_Scene_LevelSelect" || callHiddenOnce && Application.loadedLevelName != "2D_Game_Scene_LevelSelect_1" || callHiddenOnce && Application.loadedLevelName != "2D_Game_Scene_LostScreen" || callHiddenOnce && Application.loadedLevelName != "2D_Game_Scene_WonScreen")
{
admob.HideBanner();
callHiddenOnce = false;
hidden = true;
Debug.Log ("Admob Ads will NOT displayed in this scene!");
}
//Scenes i want to display ads
if(hidden && Application.loadedLevelName == "2D_Game_Scene_LevelSelect" || hidden && Application.loadedLevelName == "2D_Game_Scene_LevelSelect_1" || hidden && Application.loadedLevelName == "2D_Game_Scene_LostScreen" || hidden && Application.loadedLevelName == "2D_Game_Scene_WonScreen")
{
admob.ShowBanner ();
hidden = false;
callHiddenOnce = true;
Debug.Log ("Admob Ads WILL be displayed in this scene!!!");
}
}
}
Something I noticed off the bat:
if(callHiddenOnce && Application.loadedLevelName != "2D_Game_Scene_LevelSelect" ||
callHiddenOnce && Application.loadedLevelName != "2D_Game_Scene_LevelSelect_1" ||
callHiddenOnce && Application.loadedLevelName != "2D_Game_Scene_LostScreen" ||
callHiddenOnce && Application.loadedLevelName != "2D_Game_Scene_WonScreen")
That’s equivalent to just:if(callHiddenOnce)
The loadedLevelName can’t be all those things at once!
Which means that if your scene is any of the ones in the second if statement, then both those statements will get called on every update, causing the infinite loop.
What you probably want to do instead is first check whether this is a scene you want to show ads in, save that to a flag, and use the flag (appropriately) in your if statements.
@Errorsatz
Thanks for the reply, i was able to figure it out. I ended up writing code for all of the different scenes. It took a while longer however it worked!
using UnityEngine;
using System.Collections;
For anyone facing the same issue here is a section of the code which i used.
public class AdManager : MonoBehaviour
{
private const string AD_UNIT_ID = "XXXXXXXXXXXXXXX";
private AdMobPlugin admob;
public bool hidden = false;
public bool callHiddenOnce = true;
public void Awake()
{
DontDestroyOnLoad (this);
}
void Start ()
{
admob = GetComponent<AdMobPlugin>();
admob.CreateBanner (AD_UNIT_ID, AdMobPlugin.AdSize.SMART_BANNER, true);
admob.RequestAd ();
admob.ShowBanner ();
admob.HideBanner();
}
void Update()
{
if(Application.loadedLevelName == "2D_Game_Scene_LevelSelect")
{
if(hidden)
{
admob.ShowBanner ();
hidden = false;
Debug.Log ("Admob Ads WILL be displayed in this scene!!!");
}
}
if(Application.loadedLevelName == "2D_Game_Scene_LevelSelect_1")
{
if(hidden)
{
admob.ShowBanner ();
hidden = false;
Debug.Log ("Admob Ads WILL be displayed in this scene!!!");
}
}
if(Application.loadedLevelName == "2D_Game_Scene_LostScreen")
{
if(hidden)
{
admob.ShowBanner ();
hidden = false;
Debug.Log ("Admob Ads WILL be displayed in this scene!!!");
}
}
if(Application.loadedLevelName == "2D_Game_Scene_WonScreen")
{
if(hidden)
{
admob.ShowBanner ();
hidden = false;
Debug.Log ("Admob Ads WILL be displayed in this scene!!!");
}
}
if(Application.loadedLevelName == "2D_Game_Scene_MainMenu")
{
if(!hidden)
{
admob.HideBanner ();
hidden = true;
Debug.Log ("Admob Ads will NOT be displayed in this scene!!!");
}
}
if(Application.loadedLevelName == "2D_Game_AboutUs")
{
if(!hidden)
{
admob.HideBanner ();
hidden = true;
Debug.Log ("Admob Ads will NOT be displayed in this scene!!!");
}
}
if(Application.loadedLevelName == "2D_Game_Tutorial")
{
if(!hidden)
{
admob.HideBanner ();
hidden = true;
Debug.Log ("Admob Ads will NOT be displayed in this scene!!!");
}
}
if(Application.loadedLevelName == "2D_Game_Scene1")
{
if(!hidden)
{
admob.HideBanner ();
hidden = true;
Debug.Log ("Admob Ads will NOT be displayed in this scene!!!");
}
}
if(Application.loadedLevelName == "2D_Game_Scene2")
{
if(!hidden)
{
admob.HideBanner ();
hidden = true;
Debug.Log ("Admob Ads will NOT be displayed in this scene!!!");
}
}
if(Application.loadedLevelName == "2D_Game_Scene3")
{
if(!hidden)
{
admob.HideBanner ();
hidden = true;
Debug.Log ("Admob Ads will NOT be displayed in this scene!!!");
}
}