Search code examples
unity-game-enginereturnadmobofflineinterstitial

How to fix "interstitialAd.IsLoaded() returns true in offline" (unity C#)


interstitialAd.IsLoaded() is always returning true even if device is offline. its works fine when app device is online.

public class InsAdBanner : MonoBehaviour
{

    public int SceneIndex;
    private InterstitialAd interstitialAd;


    void Start()
    {
        this.RequestInterstitial();
    }

    void RequestInterstitial()
    {
        string interstitial_ID = "ad_id";
        interstitialAd = new InterstitialAd(interstitial_ID);

        if (this.interstitialAd != null)
        {
            this.interstitialAd.Destroy();
        }

        this.interstitialAd.OnAdLoaded += HandleOnAdLoaded;
        this.interstitialAd.OnAdFailedToLoad += HandleOnAdFailedToLoad;
        this.interstitialAd.OnAdOpening += HandleOnAdOpened;
        this.interstitialAd.OnAdClosed += HandleOnAdClosed;
        this.interstitialAd.OnAdLeavingApplication += HandleOnAdLeavingApplication;


        AdRequest adRequest = new AdRequest.Builder().Build();

        interstitialAd.LoadAd(adRequest);
    }

    public void Display_InterstitialAD()
    {
    <!-- plz see this line below -->

        if (interstitialAd.IsLoaded())
        {
            Debug.Log("interstitialAd " + interstitialAd.IsLoaded());
            interstitialAd.Show();
        }
        else {
            getOut();
        }

    }

    public void getOut() {
        interstitialAd.Destroy();
        SceneManager.LoadScene(SceneIndex);
        restertAdCounter.restertsAdCouner += 1;
    }

    #region Interstitial callback handlers

    //Handle event

    public void HandleOnAdLoaded(object sender, EventArgs args)
    {
        MonoBehaviour.print("HandleAdLoaded event received");
    }

    public void HandleOnAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
    {
        RequestInterstitial();
    }

    public void HandleOnAdOpened(object sender, EventArgs args)
    {
        MonoBehaviour.print("HandleAdOpened event received");
    }

    public void HandleOnAdClosed(object sender, EventArgs args)
    {
        SceneManager.LoadScene(SceneIndex);
    }

    public void HandleOnAdLeavingApplication(object sender, EventArgs args)
    {
        SceneManager.LoadScene(SceneIndex);
    }

    #endregion

    void OnDisable()
    {
        this.interstitialAd.OnAdLoaded += HandleOnAdLoaded;
        this.interstitialAd.OnAdFailedToLoad += HandleOnAdFailedToLoad;
        this.interstitialAd.OnAdOpening += HandleOnAdOpened;
        this.interstitialAd.OnAdClosed += HandleOnAdClosed;
        this.interstitialAd.OnAdLeavingApplication += HandleOnAdLeavingApplication;
    }

        void OnDestroy()
    {
        interstitialAd.Destroy();
    }
}

I expecting it to return false in offline..it is working fine when app device is online .. but creates this problem when device is offline..plz help.. i cant find any solution in google.

Edit: Its not working on online too.. after adding real ad in place of test ad this problem is occurring.


Solution

  • Solved my problem by changing Minify->Relese->Proguard to Minify->Relese->None on player setting.(Although i dont know how actually its working)