Admob仅加载一次广告

时间:2016-09-24 07:52:05

标签: unity3d admob

我的游戏中有一个事件在游戏过程中会多次触发。我想在事件发生时每个奇怪的时间展示广告。 但它只展示一次广告,然后它似乎无限地加载新广告

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的失败?

1 个答案:

答案 0 :(得分:1)

好吧我弄清楚了这个错误,我必须打电话给

interstitial.Destroy(); 
广告显示后