user2686299
user2686299

Reputation: 427

Admob loads ads only once

I have an event in my game that fires many times during the game. And I want to show ads each odd time when the event fires. But it shows ads only once, and then it seems that it is loading new ads infinitely

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
}

Is my code wrong or it's Admob's failure?

Upvotes: 1

Views: 1193

Answers (1)

user2686299
user2686299

Reputation: 427

ok i figured out the mistake, i have to call

interstitial.Destroy(); 

after ad was shown

Upvotes: 1

Related Questions