繁体   English   中英

Admob非页内广告Unity 3D的问题

[英]Issue with Admob Interstitial Unity 3D

我已经按照Google Admob的分步教程学习了如何在3D Unity游戏中实现插页式广告,但是在设备上运行游戏时却没有显示插页式广告。

您知道我的代码有什么问题,以及如何解决它吗?

广告脚本

    private InterstitialAd interstitial;

    static int loadCount = 0;

    bool GameHasEnded = false;
    float RestartDelay = 1.5f;


    private void Start()
    {

        #if UNITY_ANDROID
           string appId = "ca-app-pub-3940256099942544/1033173712";
        #else
           string appId = "unexpected_platform";
        #endif

        // Initialize the Google Mobile Ads SDK.
        MobileAds.Initialize(appId);

        this.RequestInterstitial();

        if ((loadCount % 3) == 0)  // only show ad every third time
        {
            if (this.interstitial.IsLoaded())
            {
                this.interstitial.Show();
            }
        }
    }

    void Restart()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().path);
        loadCount = loadCount + 1;
    }

    private void RequestInterstitial()
    {
       #if UNITY_ANDROID
           string adUnitId = "ca-app-pub-3940256099942544/1033173712";
       #else
           string adUnitId = "unexpected_platform";
       #endif

        // Initialize an InterstitialAd.
        this.interstitial = new InterstitialAd(adUnitId);

        // Create an empty ad request.
        AdRequest request = new AdRequest.Builder().Build();

        // Load the interstitial with the request.
        this.interstitial.LoadAd(request);
    }

对我来说,问题似乎是由于插页式广告加载需要一段时间(有时最多3秒)而发生的。 您试图调用它的速度太快,并且没有加载,所以什么也没发生。 我所做的就是不断尝试在无效更新中展示插页式广告,直到它显示为止(使用名为show的布尔值):

void Update () {
     if (shown == false) {
         ShowInterstitial();
     }
}
public void ShowInterstitial()
{
     if (interstitial.IsLoaded())
     {
         interstitial.Show();
         shown = true;
     } else    {
         Debug.Log ("Interstitial is not ready yet.");
     }    
}

您也可以在关卡的开头加载广告,然后仅在回合结束时调用它。

您正在尝试启动admob并在每次重新启动时请求新的插页式广告。 您必须创建一个AdmobController实例或其他实例并使用它。

public class AdManager{

   public static AdManager instance;

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


   private void Start()
   {


       #if UNITY_ANDROID
          string appId = "ca-app-pub-3940256099942544/1033173712";
       #else
          string appId = "unexpected_platform";
       #endif

       // Initialize the Google Mobile Ads SDK.
       MobileAds.Initialize(appId);

       this.RequestInterstitial();

   }

    public void showInterstitial(){
      if ((loadCount % 3) == 0)  // only show ad every third time
      {
        if (this.interstitial.IsLoaded())
        {
            this.interstitial.Show();
        }
     }

}

那么当您想展示插页式广告时

AdController.instance.showInterstitial();

它可能有语法错误或其他内容,但是您已经明白了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM