在Unity中使用AdMob实施非页内广告

问题描述

我正在使用Unity和C#为Android制作2D游戏。 我的横幅广告出现故障。我在这张图片中解释得更好。

Image

我在插页式广告完全不显示时遇到了麻烦。 我希望它在3次死亡后出现。 您可以在下面看到我的代码:

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

public class Ad3 : MonoBehaviour
{
    private BannerView bannerView;                              // Banner Ad
    public InterstitialAd interstitial;                         // Interstitial Ad
    static int loadCount = 0;                                   // Interstitial Ad

    public void Awake()
    {
        MobileAds.Initialize(initStatus => { });                // Initializing Ad SDK
    }

    public void Start()
    {
        this.RequestBanner();                                   // Show Banner AD in the Start
        if (loadCount % 3 == 0)                                 // Interstitial Ad - only show ad every third death - Got this line from another stackoverflow question - so,is loadCount universal?
        {
            this.ShowInterstitial();
        }
        loadCount++;
    }
    
    public void RequestBanner()
    {
        string adUnitId = "ca-app-pub-3940256099942544/6300978111";
        this.bannerView = new BannerView(adUnitId,AdSize.Banner,AdPosition.Top);

        AdRequest request = new AdRequest.Builder().Build();    // Create an empty ad request.
        this.bannerView.LoadAd(request);                        // Load the banner with the request.
    }
    
    public void RequestInterstitial()                                   // Interstitial Ad
    {
        string InterstitialAdID = "ca-app-pub-3940256099942544/1033173712";
        this.interstitial = new InterstitialAd(InterstitialAdID);       // Initialize an InterstitialAd.
        
        AdRequest request = new AdRequest.Builder().Build();            // Create an empty ad request.
        this.interstitial.LoadAd(request);                              // Load the interstitial with the request.
    }
    public void ShowInterstitial()                                   // Interstitial Ad
    {
        if (this.interstitial.IsLoaded())
        {
            this.interstitial.Show();
        }  
    }

}

感谢您的所有反馈。

谢谢:)

解决方法

问题在于您永远不会在任何地方调用“ RequestInterstitial()” 需要请求插页广告才能显示 注意:请检查是否在其他脚本中展示广告的时间以及该脚本将调用该函数以展示插页式广告:

SingletonTest.Instance.TheFunctionThatShowInsterstitial();

单例示例:

 public class SingletonTest : MonoBehaviour
 {
   public static SingletonTest Instance;

   private void Awake()
   {
     if (Instance == null)
     {
         Instance = this;
         
     }
     else if(Instance != this)
     {
         Destroy(gameObject);
         return;
     }
     DontDestroyOnLoad(gameObject);
   }
}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...