【问题标题】:Admob loads ads only onceAdmob 只加载一次广告
【发布时间】:2016-09-24 07:52:04
【问题描述】:

我的游戏中有一个事件在游戏中触发了很多次。我想在事件触发的每个奇数时间展示广告。 但是它只显示一次广告,然后似乎无限加载新广告

using GoogleMobileAds.Api;
using System;
using UnityEngine;

public class AdMobManager : MonoBehaviour
{
#if UNITY_ANDROID

    private static AdMobManager _instance;
    private bool isLoading;
    private bool show;
    private int isShown = -1;
#if UNITY_ANDROID
    string adUnitId = "ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxxx";
#elif UNITY_IPHONE
         string adUnitId = "ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxx";
#else
         string adUnitId = "unexpected_platform";
#endif

    public int IsShown
    {
        get
        {
            if (isShown < 0)
            {
                isShown = PlayerPrefs.GetInt("shown", 0);
            }
            return isShown;
        }
        set { isShown = value; }
    }


    public static AdMobManager Instance
    {
        get { return _instance; }
        set { _instance = value; }
    }

    InterstitialAd interstitial;

    void Awake()
    {
        if (_instance == null)
            _instance = this;

        IsShown = PlayerPrefs.GetInt("shown", 0);
        interstitial = new InterstitialAd(adUnitId);
    }

    void Start()
    {
        RequestAd();
    }

    public void Show()
    {
        Debug.Log("SHOW IS STARTED");
        if (IsShown == 0)
        {
            if (interstitial.IsLoaded())
            {
                IsShown = 1;
                Debug.Log("SHOW ADS");
                interstitial.Show();
            }
            else
            {
                show = true;
                Debug.Log("REQUESTING A NEW AD");
                RequestAd(true);
            }
        }
        else
        {
            IsShown = 0;
        }
    }

    private void RequestAd(bool show = false)
    {
        if (isLoading)
        {
            Debug.Log("RETURN FROM REQUEST AD");
            return;
        }
        isLoading = true;
        AdRequest request = new AdRequest.Builder().Build();
        // Load the banner with the request.
        interstitial.LoadAd(request);
        if (show)
            interstitial.OnAdLoaded += ShowAd;
        interstitial.OnAdFailedToLoad += FailedToLoad;
        interstitial.OnAdClosed += AdClosed;

    }

    private void AdClosed(object sender, EventArgs e)
    {
        RequestAd();
    }

    private void FailedToLoad(object sender, AdFailedToLoadEventArgs e)
    {
        isLoading = false;
        show = false;
        Debug.Log("FAILED TO LOAD: " + e.Message);
    }

    void ShowAd(object sender, System.EventArgs args)
    {
        Debug.Log("LOADED");
        if (show)
        {
            IsShown = 1;
            Debug.Log("SHOW ADS");
            interstitial.Show();
            show = false;
        }
        isLoading = false;
    }

    void OnApplicationPause(bool pause)
    {
        if (pause)
            PlayerPrefs.SetInt("shown", isShown);
    }
#endif
}

是我的代码错误还是 Admob 的失败?

【问题讨论】:

  • 我认为 IsShown 搞乱了我们的逻辑,尝试在各处注释掉并重新构建它。
  • 做到了,没有任何改变
  • 分享您的错误日志
  • 没有错误,只是开始加载新广告而已

标签: unity3d admob


【解决方案1】:

好的,我发现了错误,我得打电话了

interstitial.Destroy(); 

广告展示后

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-04
    • 1970-01-01
    • 1970-01-01
    • 2016-11-17
    • 1970-01-01
    • 2019-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多