测试广告正常运行,但是当我在Play商店上发布测试广告时,这些广告无法正常工作。链接到游戏https://play.google.com/store/apps/details?id=com.GreyMacleno.FruitChop。这是我的AdManager的代码]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using GoogleMobileAds.Api;
public class AdManager : MonoBehaviour
{
public static AdManager Instance{get;set;}
private BannerView bannerAd;
private InterstitialAd interstitialAd;
private RewardBasedVideoAd rewardVideoAd;
void Awake()
{
if (Instance == null)
{
DontDestroyOnLoad(this.gameObject);
Instance = this;
}
else if (Instance != this)
{
Destroy(this.gameObject);
return;
}
}
void Start()
{
MobileAds.Initialize(initStatus => { });
RequestBanner();
RequestInterstitial();
}
void RequestBanner(){
// for real
string banner_ID = "not showing but correctly entered";
// for testing
// string banner_ID = "ca-app-pub-3940256099942544/6300978111";
bannerAd = new BannerView(banner_ID, AdSize.Banner, AdPosition.Bottom);
// for real
AdRequest adRequest = new AdRequest.Builder().Build();
// For testing
// AdRequest adRequest = new AdRequest.Builder().AddTestDevice("2077ef9a63d2b398840261c8221a0c9b").Build();
bannerAd.LoadAd(adRequest);
}
void RequestInterstitial(){
if(interstitialAd != null)
interstitialAd.Destroy();
// for real
string interstitial_ID = "not showing but correctly entered";
// for testing
// string interstitial_ID = "ca-app-pub-3940256099942544/1033173712";
interstitialAd = new InterstitialAd(interstitial_ID);
// Called when an ad request has successfully loaded.
interstitialAd.OnAdLoaded += HandleOnAdLoaded;
// for real
AdRequest adRequest = new AdRequest.Builder().Build();
// For testing
// AdRequest adRequest = new AdRequest.Builder().AddTestDevice("2077ef9a63d2b398840261c8221a0c9b").Build();
interstitialAd.LoadAd(adRequest);
}
public void DisplayBanner(){
bannerAd.Show();
}
public void DisplayInterstitialAd(){
if(interstitialAd.IsLoaded()){
interstitialAd.Show();
}
else{
RequestInterstitial();
interstitialAd.Show();
}
}
public void HandleOnAdLoaded(object sender, EventArgs args)
{
DisplayBanner();
}
public void HandleOnAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
RequestBanner();
}
public void HandleOnAdOpened(object sender, EventArgs args)
{
MonoBehaviour.print("HandleAdOpened event received");
}
public void HandleOnAdClosed(object sender, EventArgs args)
{
MonoBehaviour.print("HandleAdClosed event received");
}
public void HandleOnAdLeavingApplication(object sender, EventArgs args)
{
MonoBehaviour.print("HandleAdLeavingApplication event received");
}
void HandleBannerADEvents(bool subscribe){
if(subscribe){
// Called when an ad request has successfully loaded.
this.bannerAd.OnAdLoaded += this.HandleOnAdLoaded;
// Called when an ad request failed to load.
this.bannerAd.OnAdFailedToLoad += this.HandleOnAdFailedToLoad;
// Called when an ad is clicked.
this.bannerAd.OnAdOpening += this.HandleOnAdOpened;
// Called when the user returned from the app after an ad click.
this.bannerAd.OnAdClosed += this.HandleOnAdClosed;
// Called when the ad click caused the user to leave the application.
this.bannerAd.OnAdLeavingApplication += this.HandleOnAdLeavingApplication;
}
else{
// Called when an ad request has successfully loaded.
this.bannerAd.OnAdLoaded -= this.HandleOnAdLoaded;
// Called when an ad request failed to load.
this.bannerAd.OnAdFailedToLoad -= this.HandleOnAdFailedToLoad;
// Called when an ad is clicked.
this.bannerAd.OnAdOpening -= this.HandleOnAdOpened;
// Called when the user returned from the app after an ad click.
this.bannerAd.OnAdClosed -= this.HandleOnAdClosed;
// Called when the ad click caused the user to leave the application.
this.bannerAd.OnAdLeavingApplication -= this.HandleOnAdLeavingApplication;
}
}
void OnEnable(){
HandleBannerADEvents(true);
}
void OnDisable(){
HandleBannerADEvents(false);
}
}
我在要运行插页式广告的地方调用DisplayInterstitialAd()。很少有人说我应该等待,但是已经过去了三天,我仍然在等待广告展示。我已将应用程序与Admob关联。 Unity的版本为2019.3.13f1。
找到了解决方案。我的Admob帐户未通过验证,因此未启用我的广告投放。通过验证后,广告就会开始展示。