Search code examples
c#unity-game-engineadmobadsadbannerview

Admob banner is invisible but still clickable in Unity


I'm working on an app that displays a banner depending on the scene. I use Show and Hide to control this behavior.

It works perfectly the first time that the banner is shown, but the second (after being hidden) the banner does not appear. However, the banner zone still clickable and it performs as expected.

I'm using the last release of the AdMob plugin for unity (v3.15.1) and I have not been able of finding any solution to the problem.

This is my code:

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

public class AdsManager //Ads information: https://developers.google.com/admob/unity (see left menu fo detailed info of each ad type)
{
    public static AdsManager instance;
    public BannerView bannerAd;
    public InterstitialAd  interstitialAd;
    public RewardBasedVideoAd rewardedVideoAd;

    #if UNITY_EDITOR
    private static String appId = "unused";
    private static String bannerAdId = "unused";
    private static String interistitialAdId = "unused";
    private static String rewardedVideAdId = "TBD";
    #elif UNITY_ANDROID
    private static String appId = "ca-app-pub-6685658831663319~7807395346";
    private static String bannerAdId = "ca-app-pub-3940256099942544/6300978111"; //"ca-app-pub-6685658831663319/9607562172";
    private static String interistitialAdId = "ca-app-pub-3940256099942544/1033173712"; //"ca-app-pub-6685658831663319/4875778545";
    private static String rewardedVideAdId = "ca-app-pub-3940256099942544/5224354917"; //"ca-app-pub-6685658831663319/2971919290";
    #elif UNITY_IOS
    private static String appId = "ca-app-pub-6685658831663319~7807395346";
    private static String bannerAdId = "ca-app-pub-6685658831663319/9607562172";
    private static String interistitialAdId = "ca-app-pub-6685658831663319/4875778545";
    private static String rewardedVideAdId = "ca-app-pub-6685658831663319/2971919290";
    #else
    private static String appId = "unexpected_platform";
    private static String bannerAdId = "unexpected_platform";
    private static String interistitialAdId = "unexpected_platform";
    private static String rewardedVideAdId = "unexpected_platform";
    #endif


    public enum TypeOfAd
    {
        Banner,
        Interestitial, 
        RewardedVideo
    }

    public AdsManager()
    {
        Debug.Log("Initializing a new AdsManager.");
        if (instance == null)
        {
            instance = this;
            Setup();
            Debug.Log("AdsManager initialization successful.");
        }
        else
        {
            Debug.Log("AdsManager already exists. New initialization unsuccessful.");
        }
    }

    private void Setup()
    {
        // Initialize the Google Mobile Ads SDK.
        MobileAds.Initialize(appId);

        this.bannerAd = new BannerView(bannerAdId, AdSize.SmartBanner, AdPosition.Bottom); //Info to chang banner size: https://developers.google.com/admob/unity/banner#banner_sizes
        SetupBannerEventHandlers();

        this.interstitialAd = new InterstitialAd(interistitialAdId);
        SetupInterstitialEventHandlers();

        this.rewardedVideoAd = RewardBasedVideoAd.Instance;
        SetupRewardedVideoAdsEventHandlers();

        //Load firsts ads
        instance.LoadAd(TypeOfAd.Interestitial);
        instance.LoadAd(TypeOfAd.Banner);
    }

    private void SetupBannerEventHandlers()
    {
        this.bannerAd.OnAdLoaded += HandlerOnBannerAdLoaded;
        this.bannerAd.OnAdFailedToLoad += HandlrOnBannerAdFailedToLoad;
        this.bannerAd.OnAdOpening += HandlerOnBannerAdOpening;
        this.bannerAd.OnAdClosed += HandlerOnBannerAdClosed;
        this.bannerAd.OnAdLeavingApplication += HandlerOnBannerAdLeavingApplication;
    }

    private void SetupInterstitialEventHandlers()
    {
        this.interstitialAd.OnAdLoaded += HandlerOnInterstitialAdLoaded;
        this.interstitialAd.OnAdFailedToLoad += HandlerOnInterstitialAdFailedToLoad;
        this.interstitialAd.OnAdOpening += HandlerOnInterstitialAdOpening;
        this.interstitialAd.OnAdClosed += HandlerOnInterstitialAdClosed;
        this.interstitialAd.OnAdLeavingApplication += HandlerOnInterstitialAdLeavingApplication;
    }

    private void SetupRewardedVideoAdsEventHandlers()
    {
        this.rewardedVideoAd.OnAdLoaded += HandlerRewardVideoAdLoaded;
        this.rewardedVideoAd.OnAdFailedToLoad += HandlerRewardVideoAdFailedToLoad;
        this.rewardedVideoAd.OnAdOpening += HandlerRewardVideoAdOpening;
        this.rewardedVideoAd.OnAdStarted += HandlerRewardVideoAdStarted;
        this.rewardedVideoAd.OnAdRewarded += HandlerRewardVideoAdRewarded;
        this.rewardedVideoAd.OnAdClosed += HandlerRewardVideoAdClosed;
        this.rewardedVideoAd.OnAdLeavingApplication += HandlerRewardVideoAdLeftApplication;
    }

    public void LoadAd(TypeOfAd typeOfAd)
    {
        if (DataManager.instance.showAds)
            switch (typeOfAd)
            {
                case TypeOfAd.Banner:
                    Debug.Log("Loading Banner ad");
                    this.bannerAd.LoadAd(new AdRequest.Builder().Build());  //After loading it, it will be automatically displayed
                    break;

                case TypeOfAd.Interestitial:
                    Debug.Log("Loading Interestitial ad");
                    if (!this.interstitialAd.IsLoaded())
                        this.interstitialAd.LoadAd(new AdRequest.Builder().Build());

                    break;

                case TypeOfAd.RewardedVideo:
                    Debug.Log("Loading RewardedVideo ad");
                    if (!this.rewardedVideoAd.IsLoaded())
                        this.rewardedVideoAd.LoadAd(new AdRequest.Builder().Build(), rewardedVideAdId);
                    break;
            }
    }

    public bool ShowAd(TypeOfAd typeOfAd)
    {
        if (DataManager.instance.showAds)
            switch (typeOfAd)
            {
                case TypeOfAd.Banner:
                    Debug.Log("Showing Banner ad");
                    this.bannerAd.Show(); //No avaliable check to know if the ad has been loaded...
                    return true; //...so maybe the return is true but the ad is not being displayed because it was not loaded before.


                case TypeOfAd.Interestitial:
                    Debug.Log("Showing Interestitial ad");
                    if (this.interstitialAd.IsLoaded())
                    {
                        this.interstitialAd.Show();
                        return true;
                    }
                    else
                    {
                        Debug.LogWarning("Trying to show InterstitialAd but it is not loaded");
                        //TBD: Automaitcally load?
                    }
                    break;


                case TypeOfAd.RewardedVideo:
                    Debug.Log("Showing RewardedVideo ad");
                    if (this.rewardedVideoAd.IsLoaded())
                    {
                        this.rewardedVideoAd.Show();
                        return true;
                    } else {
                        Debug.LogWarning("Trying to show RewardedBasedVideoAd but it is not loaded");
                        //TBD: Automaitcally load?
                    }
                    break;
            }

        return false;
    }

    public void QuitAd(TypeOfAd typeOfAd)
    {
        switch (typeOfAd)
        {
            case TypeOfAd.Banner:
                Debug.Log("Quiting Banner ad");
                bannerAd.Hide();
                break;


            case TypeOfAd.Interestitial:
                Debug.Log("Quiting Interestitial ad");
                Debug.LogError("QuitAd Interestitial Not Implemented");
                break;


            case TypeOfAd.RewardedVideo:
                Debug.Log("Quiting RewardedVideo ad");
                Debug.LogError("QuitAd RewardedVideo Not Implemented");
                break;
        }
    }

    //BANNER EVENT HANDLERS
    public void HandlerOnBannerAdLoaded(object sender, EventArgs args) { } // Called when an ad request has successfully loaded.
    public void HandlrOnBannerAdFailedToLoad(object sender, AdFailedToLoadEventArgs args) { Debug.LogWarning("Banner failed to load: " + args.Message); LoadAd(TypeOfAd.Banner); } // Called when an ad request failed to load.
    public void HandlerOnBannerAdOpening(object sender, EventArgs args) { } // Called when an ad is clicked.
    public void HandlerOnBannerAdClosed(object sender, EventArgs args) { } // Called when the user returned from the app after an ad click.
    public void HandlerOnBannerAdLeavingApplication(object sender, EventArgs args) { } // Called when the ad click caused the user to leave the application.

    //INTERSTITIAL EVENT HANDLERS
    public void HandlerOnInterstitialAdLoaded(object sender, EventArgs args) { } // Called when an ad request has successfully loaded.
    public void HandlerOnInterstitialAdFailedToLoad(object sender, AdFailedToLoadEventArgs args) { Debug.LogWarning("Interstitial failed to load: " + args.Message); LoadAd(TypeOfAd.Interestitial); } // Called when an ad request failed to load.
    public void HandlerOnInterstitialAdOpening(object sender, EventArgs args) { } // Called when an ad is shown.
    public void HandlerOnInterstitialAdClosed(object sender, EventArgs args) { } // Called when the ad is closed.
    public void HandlerOnInterstitialAdLeavingApplication(object sender, EventArgs args) { } // Called when the ad click caused the user to leave the application.

    //REWARDED VIDEO AD EVENT HANDLERS
    public void HandlerRewardVideoAdLoaded(object sender, EventArgs args) { } // Called when an ad request has successfully loaded.
    public void HandlerRewardVideoAdFailedToLoad(object sender, AdFailedToLoadEventArgs args) { Debug.LogWarning("RewardedVideo failed to load: " + args.Message); LoadAd(TypeOfAd.RewardedVideo); } // Called when an ad request failed to load.
    public void HandlerRewardVideoAdOpening(object sender, EventArgs args) { } // Called when an ad is shown.
    public void HandlerRewardVideoAdStarted(object sender, EventArgs args) { } // Called when the ad starts to play.
    public void HandlerRewardVideoAdRewarded(object sender, Reward args) { Debug.Log("Rewarded video ad completed. Reward info: " + args.Amount + " of " + args.Type); } // Called when the user should be rewarded for watching a video.
    public void HandlerRewardVideoAdClosed(object sender, EventArgs args) { } // Called when the ad is closed.
    public void HandlerRewardVideoAdLeftApplication(object sender, EventArgs args) { } // Called when the ad click caused the user to leave the application.

}

To show the ads I'm simply calling the method ShowAd and to hide them I'm calling the method QuitAd. I think that both perform as expected because they work the first time they are called and because in the scenes where the HideAd is called the "banner area" is not clickable but when te ShowAd method is called you can click on the invisible banner.

Thank you for any help!


Solution

  • I solved the problem by destroying the banner in the QuitAd method:

    public void QuitAd(TypeOfAd typeOfAd)
    {
        switch (typeOfAd)
        {
            case TypeOfAd.Banner:
                Debug.Log("Quiting Banner ad");
                bannerAd.Destroy();
                break;
    
    
            case TypeOfAd.Interestitial:
                Debug.Log("Quiting Interestitial ad");
                Debug.LogError("QuitAd Interestitial Not Implemented");
                break;
    
    
            case TypeOfAd.RewardedVideo:
                Debug.Log("Quiting RewardedVideo ad");
                Debug.LogError("QuitAd RewardedVideo Not Implemented");
                break;
        }
    }
    

    And then I've modified the ShowAd method loading the banner before showing it:

    public bool ShowAd(TypeOfAd typeOfAd)
    {
        if (DataManager.instance.showAds)
            switch (typeOfAd)
            {
                case TypeOfAd.Banner:
                    Debug.Log("Showing Banner ad");
                    LoadAd(TypeOfAd.Banner); //Every time the banner is asked to be shown it will try to load before being shown.
                    this.bannerAd.Show(); //Will be show after loading
                    return true;
    
    
                case TypeOfAd.Interestitial:
                    Debug.Log("Showing Interestitial ad");
                    if (this.interstitialAd.IsLoaded())
                    {
                        this.interstitialAd.Show();
                        return true;
                    }
                    else
                    {
                        Debug.LogWarning("Trying to show InterstitialAd but it is not loaded");
                        //TBD: Automaitcally load?
                    }
                    break;
    
    
                case TypeOfAd.RewardedVideo:
                    Debug.Log("Showing RewardedVideo ad");
                    if (this.rewardedVideoAd.IsLoaded())
                    {
                        this.rewardedVideoAd.Show();
                        return true;
                    } else {
                        Debug.LogWarning("Trying to show RewardedBasedVideoAd but it is not loaded");
                        //TBD: Automaitcally load?
                    }
                    break;
            }
    
        return false;
    }
    

    However, I do not know if this is a proper solution because a new load request will be done every time a banner has to be shown (after a scene where it is not shown).

    Additionally, this "solution" is only a different approach for the same objective, not a fix for the original approach.

    So if anyone knows why the original code is not working I would appreciate a lot the sharing of this knowledge.