首页 > 解决方案 > addmob 测试广告不再显示

问题描述

我为 android 制作了一个统一的游戏,但我在使用 admob 时遇到了问题。当我第一次设置 AdMob 时它工作正常。但现在,它没有显示横幅,当我请求插页式广告时,应用程序直接关闭。

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

创建的 DummyClient 虚拟 CreateBannerView 虚拟 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()
{

}
}

标签: c#androidunity3dmobileadmob

解决方案


我的工作步骤如下:

第 1 步 - 在 Windows 中设置 JAVA_HOME:
https ://javatutorial.net/set-java-home-windows-10

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

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

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

(然后启用google admob并输入您可以在inspector窗口中看到的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

}

希望这会奏效;)


推荐阅读