addmob测试广告不再显示



我为android制作了一款带有unity的游戏,但我在admob上遇到了问题。当我第一次设置AdMob时,它运行得很好。但现在,它没有显示横幅,当我请求填隙时,应用程序会直接关闭。

当我在Unity中运行代码时,它运行得很好。我可以在控制台中看到以下内容:

已创建DummyClient虚拟创建横幅视图伪加载广告

当我为android编译代码时,我无法通过初始化。

移动广告。初始化(initStatus=>{}(;

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GoogleMobileAds.Api;
using UnityEngine.UI;
public class reklamlar : MonoBehaviour
{
private BannerView bannerView;
string adUnitId = "ca-app-pub-3940256099942544/6300978111";
void Start()
{
MobileAds.Initialize(initStatus => { });
this.RequestBanner();
ShowBannerAd();
}
public void RequestBanner()
{
this.bannerView = new BannerView(adUnitId, AdSize.SmartBanner, AdPosition.Top);
Debug.Log("banner Request");
}
public void ShowBannerAd()
{
AdRequest request = new AdRequest.Builder().Build();
this.bannerView.LoadAd(request);
Debug.Log("banner Show");
}
// Update is called once per frame
void Update()
{
}
}

我的工作步骤如下:

步骤1-在窗口中设置JAVA_HOME:
https://javatutorial.net/set-java-home-windows-10

步骤2-将您的项目切换到Android

步骤3-将AdMob appid添加到您的项目

资产>谷歌移动广告>设置>

(然后启用google admob并输入您可以在检查器窗口中看到的appid(

步骤4-然后强制解决项目资产>播放服务解析程序>安卓解析程序>强制解析

步骤5-编写脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GoogleMobileAds.Api;
using System;
public class AdScript : MonoBehaviour
{
string AppId = "(your App ID)";
string InterstitialAdID = "(test adid or real adid)";
string BannerAdId = "(test adid or real adid)";
private InterstitialAd interstitial;
private BannerView bannerView;
void Start()
{
MobileAds.Initialize(AppId);
if (Application.platform == RuntimePlatform.Android)
{
RequestBannerAd();
RequestInterstitial();
}
}
private void RequestInterstitial()
{
// Initialize an InterstitialAd.
this.interstitial = new InterstitialAd(InterstitialAdID);
// Called when an ad request has successfully loaded.
this.interstitial.OnAdLoaded += HandleOnAdLoaded;
// Called when an ad request failed to load.
this.interstitial.OnAdFailedToLoad += HandleOnAdFailedToLoad;
// Called when an ad is shown.
this.interstitial.OnAdOpening += HandleOnAdOpened;
// Called when the ad is closed.
this.interstitial.OnAdClosed += HandleOnAdClosed;
// Called when the ad click caused the user to leave the application.
this.interstitial.OnAdLeavingApplication += HandleOnAdLeavingApplication;
// Create an empty ad request.
AdRequest request = new AdRequest.Builder().Build();
// Load the interstitial with the request.
this.interstitial.LoadAd(request);
}
private void RequestBannerAd()
{
this.bannerView = new BannerView(BannerAdId, AdSize.Banner, AdPosition.Top);
// Create an empty ad request.
// Called when an ad request has successfully loaded.
this.bannerView.OnAdLoaded += this.HandleOnAdLoaded;
// Called when an ad request failed to load.
this.bannerView.OnAdFailedToLoad += this.HandleOnAdFailedToLoad;
// Called when an ad is clicked.
this.bannerView.OnAdOpening += this.HandleOnAdOpened;
// Called when the user returned from the app after an ad click.
this.bannerView.OnAdClosed += this.HandleOnAdClosed;
// Called when the ad click caused the user to leave the application.
this.bannerView.OnAdLeavingApplication += this.HandleOnAdLeavingApplication;

}

public void ShowinterstitialAds()
{
if (this.interstitial.IsLoaded())
{
this.interstitial.Show();
}
}
public void ShowbannerViewAds()
{
AdRequest request = new AdRequest.Builder().Build();
Load the banner with the request.
this.bannerView.LoadAd(request);
}

#region interstitial and banned handler
public void HandleOnAdLoaded(object sender, EventArgs args)
{
MonoBehaviour.print("HandleAdLoaded event received");
}
public void HandleOnAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
MonoBehaviour.print("HandleFailedToReceiveAd event received with message: "
+ args.Message);
}
public void HandleOnAdOpened(object sender, EventArgs args)
{
MonoBehaviour.print("HandleAdOpened event received");
}
public void HandleOnAdClosed(object sender, EventArgs args)
{
MonoBehaviour.print("HandleAdClosed event received");
RequestInterstitial();
}
public void HandleOnAdLeavingApplication(object sender, EventArgs args)
{
MonoBehaviour.print("HandleAdLeavingApplication event received");
}
#endregion
}

希望这能奏效;(

相关内容

  • 没有找到相关文章