简体   繁体   中英

AdMob Unity Not displaying

I have set up a Google AdMob banner that doesn't seem to be working when I compile it to an Android Application.

I've added an empty object and attached the following script to it.

在此处输入图片说明

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

public class AdController : MonoBehaviour {

    // Use this for initialization
    void Start () {
        RequestBanner ();
    }

    private void RequestBanner() {
        print ("running admob");

        #if UNITY_EDITOR
        string adUnitId = "unused";
        #elif UNITY_ANDROID
        string adUnitId = "ca-app-pub-3940256099942544/6300978111";
        #elif UNITY_IPHONE
        string adUnitId = "INSERT_IOS_BANNER_AD_UNIT_ID_HERE";
        #else
        string adUnitId = "unexpected_platform";
        #endif

        // Create a 320x50 banner at the top of the screen.
        BannerView bannerView = new BannerView(adUnitId, AdSize.Banner, AdPosition.Bottom);
        // Create an empty ad request.
        //AdRequest request = new AdRequest.Builder().Build();
        AdRequest request = new AdRequest.Builder()
        .AddTestDevice(AdRequest.TestDeviceSimulator)       // Simulator.
        .AddTestDevice(SystemInfo.deviceUniqueIdentifier)  // My test device.
        .Build();

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

Is there anything I am doing wrong? The console log does show up in the log. But I don't understand why the test ad is not showing.

You cannot show anything before its loaded.

private void ShowAd()
{
  if (bannerView.IsLoaded()) {
    bannerView.Show();
  }
}

you can use handlers to listen to OnLoad event.

read this guides completely then write your code again...

https://developers.google.com/admob/unity/start

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM