- Home /
How do i hide iAd when scene change?
I am using the documentation code that unity gives for using ADBannerView, but I would like the ad to go away when scene change (or switch by a Bool variable). Any ideas? Here is the code I am using.
private var banner : ADBannerView = null;
function Start () {
DontDestroyOnLoad(this);
//StartCoroutine(ShowBanner());
}
function ShowBanner() {
//var banner:ADBannerView;
banner = new ADBannerView();
banner.autoSize = true;
banner.autoPosition = ADPosition.Top;
//if(!banner.visible) print("iAdBanner : banner.visible = False");
while (!banner.loaded && banner.error == null)
yield;
if (banner.error == null)
banner.Show();
else banner = null;
}
function Update(){
if(banner==null) StartCoroutine(ShowBanner());
}
Answer by GameVortex · Nov 15, 2013 at 11:08 AM
You can just set the visibility of the banner manually:
function HideBanner()
{
banner.visible = false;
}
Call the function when you change your scene.
Answer by TommyB · Apr 20, 2014 at 08:43 PM
I use the following for showing/hiding an ad on scene change
using UnityEngine;
using System.Collections;
public class adtest : MonoBehaviour {
private ADBannerView banner= null;
private bool show = true;
void Start(){
show = true;
}
void Update () {
if (banner == null && show == true) {
CreateBanner();
}
if (Input.GetMouseButtonDown (0)) {
if(banner != null){
show = false;
banner.visible = false;
banner = null;
ADBannerView.onBannerWasLoaded -= OnBannerLoaded;
}
Application.LoadLevel("scene1");
}
}
void CreateBanner(){
banner = new ADBannerView(ADBannerView.Type.Banner, ADBannerView.Layout.Top);
ADBannerView.onBannerWasLoaded += OnBannerLoaded;
}
void OnBannerLoaded()
{
Debug.Log("Loaded!\n");
banner.visible = true;
}
}
Answer by ultimatearcade · May 09, 2014 at 04:41 AM
Thank you TommyB!!! :) Here is the Java Script Version of your code I am using... works great, i just call "show" true or false to hide/show the iAD in various scenes.
#pragma strict
static var show : boolean = true;
private var banner : ADBannerView = null;
// do not destroy the iAD on scene switch
function Awake () {
DontDestroyOnLoad (transform.gameObject);
}
function Start () {
show = true;
}
function Update (){
if (banner == null && show == true) {
CreateBanner();
}
if(banner != null && show == false){
banner.visible = false;
banner = null;
ADBannerView.onBannerWasLoaded -= OnBannerLoaded;
}
}
function CreateBanner (){
banner = new ADBannerView(ADBannerView.Type.Banner, ADBannerView.Layout.BottomCenter);
//ADBannerView.onBannerWasClicked += OnBannerClicked;
ADBannerView.onBannerWasLoaded += OnBannerLoaded;
}
function OnBannerLoaded(){
banner.visible = true;
}
From what I get from your script, you create two copies of this script, and then attach the false to a empty game object in the scene you don't want the ads to be in. As for the true you set that in every other scene right? I'm having trouble with this too.
You probably won't answer, but it's worth a try.
@Dogg for what it's worth, I only have 1 instance of the script and it is attached to a game object in the scene I want the ads to dispaly on (which in my case is a game over scene) I don't show the ads at other times. You could in theory attach it to a single statemanger object and then have each scene set a variable on the statemanger to show or hide ads. In my C# example above the "show" variable is there to prevent multiple calls to load a banner as this was causing an exception due to trying to add multiple event handlers for the onBannerWasLoaded method. Then in my instance in the update method I have some code under Input.Get$$anonymous$$ouseButtonDown (0) that is responsible for removing the banner etc. and the event handlers if needed. Hope this helps
Your answer