Andrew Schaff
Andrew Schaff

Reputation: 85

How do you hide banner ads on scene changes Unity AdMob?

I am creating a new Unity app, and I want to have banner ads run at the top of the screen during game play. However, I don't want to have banner ads in any other scene. I have tried just about everything I can, but only one combination of code lets me even run the ads. Any other combination results in an immediate crash. I have attached the code that I am using below, but it only shows the ads and is incapable of hiding them.

using UnityEngine;
using System.Collections;
using GoogleMobileAds.Api;

public class Banner : MonoBehaviour {
    void Start(){
        BannerView bannerView = new BannerView ("************", AdSize.Banner, AdPosition.Top);
        AdRequest request = new AdRequest.Builder().Build ();
        bannerView.LoadAd(request);
        bannerView.Show();
    }
}

Upvotes: 2

Views: 5786

Answers (1)

itchee
itchee

Reputation: 820

Destroy the BannerView upon unloading (destroying) the scene:

using UnityEngine;
using System.Collections;
using GoogleMobileAds.Api;

public class Banner : MonoBehaviour {
    private BannerView bannerView;

    void Start() {
        bannerView = new BannerView ("************", AdSize.Banner, AdPosition.Top);
        AdRequest request = new AdRequest.Builder().Build ();
        bannerView.LoadAd(request);
        bannerView.Show();
    }

    void OnDestroy() {
        bannerView.Destroy();
    }
}

Upvotes: 8

Related Questions