我用unity做了一个安卓游戏,我遇到了admob的问题.当我第一次设置AdMob时,它工作得很好.但现在,它不显示banner,当我请求中间插播时,应用程序直接关闭。
当我在Unity中运行代码时,它工作正常。我可以在控制台中看到以下内容。
Created DummyClientDummy CreateBannerViewDummy LoadAd(加载广告)
当我编译android的代码时,我无法通过初始化。
MobileAds.Initialize(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()
{
}
}
我的是通过以下步骤实现的。
第一步 - 在windows中设置JAVA_HOME。 https:/javatutorial.netset-java-home-windows-10。
第2步--将你的项目切换到Android
步骤3 - 将AdMob appid添加到您的项目中去
Assets>谷歌移动广告>设置>
(然后启用google admob并输入appid,你可以在检查器窗口看到)
第4步--然后强制解决项目Assets>Play Services Resolver>Android Resolver>Force Resolve。
步骤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
}
希望这可以工作;)