SHAH MD IMRAN HOSSAIN
SHAH MD IMRAN HOSSAIN

Reputation: 2899

Admob Banner Ads are not working properly in Unity3D

I integrate Ad-mob banner ads in my Unity game project android build

But Sometimes the ad is showing but most of time it's not

showing percentage will be just just 4% out of 100%

I don't know why this happening

I got this following code from developers.google and i am able to show ads but it does not show the banner app on every startup frankly speaking it only shows ads in first or 2nd startup after build and not anymore.

I try to find solutions but looks like all solutions are same.

So, I am Stuck in here

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

public class BannerAd : MonoBehaviour
{

    private BannerView bannerView;

    // Use this for initialization
    void Start ()
    {
        string appID = "ca-app-pub-id"; // this is appId

        MobileAds.Initialize (appID);

        string adUnitId = "ca-app-pub-adUnitId"; // adUnitID

        this.showBannerAd (adUnitId);
    }

    private void showBannerAd (string adUnitId)
    {



        //Create a custom ad size at the bottom of the screen

        AdSize adSize = new AdSize (250, 50);
        bannerView = new BannerView (adUnitId, adSize, AdPosition.Bottom);

        // Create an empty ad request.
        AdRequest request = new AdRequest.Builder ().Build ();

        // Load the banner with the request.
        bannerView.LoadAd (request);

        StartCoroutine(bannerAdTime());
    }

    IEnumerator bannerAdTime ()
    {
        yield return new WaitForSeconds (300f);

        RequestNewAd();
    }

    private void RequestNewAd ()
    {
        // Create an empty ad request.
        AdRequest request = new AdRequest.Builder ().Build ();

        // Load the banner with the request.
        bannerView.LoadAd (request);

        StartCoroutine(bannerAdTime());
    }

    // Update is called once per frame
    void Update ()
    {

    }
}

Upvotes: 2

Views: 2175

Answers (2)

happyj8
happyj8

Reputation: 21

1.Check you have install plugin as document https://github.com/unity-plugins/Unity-Admob

2.Edit AndroidManifest.xml and config Admob APP ID This configuration is reqired by admob from version 17.0,APP will crash if not config .Add a meta-data tag in application and set value to your admob appid

<meta-data
        android:name="com.google.android.gms.ads.APPLICATION_ID"
        android:value="your admob app id"/>

Sample code

    <application  android:theme="@style/UnityThemeSelector" 
   android:icon="@drawable/app_icon" 
   android:label="@string/app_name" >
           <activity
        android:name="com.unity3d.player.UnityPlayerActivity"
        android:label="@string/app_name" >
        <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        </activity>
        <meta-data
        android:name="com.google.android.gms.ads.APPLICATION_ID"
        android:value="ca-app-pub-3940256099942544~3347511713"/>
  </application>

3.Init Admob Unity Plugin Create A C# script ,drag the script to a object on scene , add the follow code in the script file

using admob;
Admob.Instance().initSDK("admob appid", new AdProperties());//admob id with format ca-app-pub-3940256099942544~3347511713
//Admob.Instance().initAdmob("ca-app-pub-3940256099942544/2934735716", new AdProperties());

4.Add Admob Banner in Unity App

Admob.Instance().showBannerRelative("your admob banner unit id",AdSize.BANNER, AdPosition.BOTTOM_CENTER, 0);

Upvotes: 2

SHAH MD IMRAN HOSSAIN
SHAH MD IMRAN HOSSAIN

Reputation: 2899

I found the solution

The problem was with my Emulator

With The Real Device it work fine

and there's no need to use the co-routine

the following codes will be enough to show the BannderAds

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

public class BannerAd : MonoBehaviour
{

    private BannerView bannerView;

    // Use this for initialization
    void Start ()
    {
        string appID = "ca-app-pub-id"; // this is appId

        MobileAds.Initialize (appID);

        string adUnitId = "ca-app-pub-adUnitId"; // adUnitID

        this.showBannerAd (adUnitId);
    }

    private void showBannerAd (string adUnitId)
    {

        //Create a custom ad size at the bottom of the screen

        AdSize adSize = new AdSize (250, 50);
        bannerView = new BannerView (adUnitId, adSize, AdPosition.Bottom);

        // Create an empty ad request.
        AdRequest request = new AdRequest.Builder ().Build ();

        // Load the banner with the request.
        bannerView.LoadAd (request);

    }


}

Upvotes: 1

Related Questions