Search code examples
androidunity-game-engineadmob2d-gamesadbannerview

Unity adMobs Android not displaying bannerView


I am trying to integrate AdMobs into my unity 2D project (game is designed for mobile platforms).

After searching the web and answers here i could not find the solution to my problem.

  • When i port my game to each platform iOS works and displays the banner view.

  • Android does not display the banner view.

  • I tried deleting and re importing the google package and still Android wont show the banner.

I did exactly what the google tutorial in this link describes. https://developers.google.com/admob/unity/start

But still no go Android won't display the banner view with the ad (tested on 2 seperate devices). Here is my code i added the appId string to both manifest and plist handlers in the project.

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

public class GoogleAdsHandler:MonoBehaviour {

    private BannerView bannerView;

    // Use this for initialization
    void Start () {
#if UNITY_ANDROID
                string appId = Consts.ANDROID_AD_APPID;
#elif UNITY_IPHONE
        string appId = Consts.IOS_AD_APPID;
#else
                string appId = "unexpected_platform";
#endif
        InitilizeAdMob ();
    }

    private void InitilizeAdMob () {
#if UNITY_ANDROID
        string appId = Consts.ANDROID_AD_APPID;
#elif UNITY_IPHONE
        string appId = Consts.IOS_AD_APPID;
#else
        string appId = "unexpected_platform";
#endif
        MobileAds.Initialize (appId);

        this.RequestBanner ();
    }

    private void RequestBanner () {
#if UNITY_ANDROID
        string adUnitId = Consts.ANDROID_BANNER_ID;
#elif UNITY_IPHONE
        string adUnitId = Consts.IOS_BANNER_ID;
#else
            string adUnitId = "unexpected_platform";
#endif
        // Create a 320x50 banner at the top of the screen.
        bannerView = new BannerView (adUnitId, AdSize.Banner, AdPosition.Bottom);
        AdRequest request = new AdRequest.Builder ().Build ();
        bannerView.LoadAd (request);
        bannerView.Show ();
        bannerView.OnAdLoaded += HandleOnAdLoaded;
    }

    public void HandleOnAdLoaded (object sender, EventArgs args) {
        MonoBehaviour.print ("HandleAdLoaded event received");
    }
}

This script is attached to a game object on my main menu scene.

Would appreciate help with the matter.

Kindest regards.

Rony.


Solution

  • You are trying to show the add without checking if the ad is loaded and it propably isn't. You should subscribe to HandleOnAdLoaded event before calling bannerView.LoadAd() and bannerView.Show().

    Your Start method is just calling the InitilizeAdMob as the string your are assigning is not passed anywhere and you are doing the same thing in InitilizeAdMob so I would rewrite your class like this:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using GoogleMobileAds.Api;
    using System;
    
    public class GoogleAdsHandler : MonoBehaviour {
    
        private BannerView bannerView;
    
        // Use this for initialization
        void Start () {
           InitilizeAdMob ();
        }
    
        private void InitilizeAdMob () {
    #if UNITY_ANDROID
            string appId = Consts.ANDROID_AD_APPID;
    #elif UNITY_IPHONE
            string appId = Consts.IOS_AD_APPID;
    #else
            string appId = "unexpected_platform";
    #endif
            MobileAds.Initialize (appId);
            this.RequestBanner ();
        }
    
        private void RequestBanner () {
    #if UNITY_ANDROID
            string adUnitId = Consts.ANDROID_BANNER_ID;
    #elif UNITY_IPHONE
            string adUnitId = Consts.IOS_BANNER_ID;
    #else
            string adUnitId = "unexpected_platform";
    #endif
            // Create a 320x50 banner at the top of the screen.
            bannerView = new BannerView (adUnitId, AdSize.Banner, AdPosition.Bottom);
            bannerView.OnAdLoaded += HandleOnAdLoaded;
            AdRequest request = new AdRequest.Builder ().Build ();
            bannerView.LoadAd (request);
        }
    
        public void HandleOnAdLoaded (object sender, EventArgs args) {
            MonoBehaviour.print ("HandleAdLoaded event received");
            bannerView.Show();
        }
    }