2

I'm using both Unity Ads and Vungle to my game. I installed them using their .unitypackage.

I already finished the codes for displaying ads from them alternately. My problem now is how to stop them from fetching/loading after closing their 1st ads? Or have the option to stop the fetching while it fetches.

It make my game crashes due to memory pressure since 2 ads are already in the memory plus the resources for my game.

I browsed the source codes and can't find any API to stop fetching or unload a waiting-to-be-displayed ads. I looked on their documentations but there's no detail to do that.

Here is my current code:

using UnityEngine;
using System.Collections;
using UnityEngine.Advertisements;  


public class AdsMngr : MonoManager<AdsMngr> {
    enum AdsEngine{

        VUNGLE,
        UNITY_ADS,

        MAXNUM
    };

    enum AdsStatus{
        IDLE,
        WAITING,
        SHOWING,

        MAXNUM
    }

    AdsEngine m_curAdsEngine = AdsEngine.UNITY_ADS;
    AdsStatus m_curAdsStatus = AdsStatus.IDLE;

    #if UNITY_IPHONE || UNITY_ANDROID


    protected override void Awake(){
        base.Awake ();

        m_curAdsEngine = AdsEngine.UNITY_ADS; //VUNGLE;

        SetupAdsCallback ();

    }

    protected override void OnDestroy(){

        base.OnDestroy();

        UnsetupAdsCallback();

    }

    void SetNextAdEngine (){
        m_curAdsEngine = (AdsEngine)((int)(m_curAdsEngine + 1) % (int)AdsEngine.MAXNUM);

    }

    void Update(){

        if (m_curAdsStatus == AdsStatus.WAITING) {
            Debug.Log ("Waiting for ads to load: "+m_curAdsEngine+"\n");

            bool bHasReadyAds = false;

            switch(m_curAdsEngine){
            case AdsEngine.VUNGLE:    if( Vungle.isAdvertAvailable()){bHasReadyAds = true;} break;
            case AdsEngine.UNITY_ADS: if( Advertisement.isReady()   ){bHasReadyAds = true;} break;
            }

            if (bHasReadyAds) {
                LoadingOverlay.Instance.Close();
                PromptOverlay.Instance.Close();
                ShowAd ();
            }
        }

    }

    public void StartAd(){

        LoadingOverlay.Instance.Open();

        //---start refetching/recaching the ads here
        switch(m_curAdsEngine){
            case AdsEngine.VUNGLE: {
                Vungle.init( "mygame_forAndroid", "mygame_forIOS" );
            }break;
            case AdsEngine.UNITY_ADS: {
                if (Advertisement.isSupported) {
                    //Advertisement.allowPrecache = true;
                    Advertisement.Initialize ("12345", true);

                    Debug.Log("Advertisement Initialized.\n");
                } else {
                    Debug.Log("Platform not supported\n");
                }

            }break;
        }

        m_curAdsStatus = AdsStatus.WAITING;

    }

    public void StopWaitingAds(){
        m_curAdsStatus = AdsStatus.IDLE;
        //TODO: cancel the loading of ads.

        //xxx: test code only
        SetNextAdEngine ();


    }

    public void ShowAd(){
        switch(m_curAdsEngine){
            case AdsEngine.VUNGLE: {
                Debug.Log("*** Vungle Ads is available. Now playing...\n");
                Vungle.playAd();
                Vungle.setSoundEnabled(false);
            }break;
            case AdsEngine.UNITY_ADS: {
                    Debug.Log("*** Unity Ads is available. Now playing...\n");
                    Advertisement.Show(null, new ShowOptions {
                        pause = true,
                        resultCallback = result => {
                            Debug.Log(result.ToString());
                            onAdEndedEvent();
                        }
                    });
            }break;
        }

        m_curAdsStatus = AdsStatus.SHOWING;
    }

    #region Optional: Example of Subscribing to All Events

    void SetupAdsCallback()
    {
        Vungle.onAdStartedEvent += onAdStartedEvent;
        Vungle.onAdEndedEvent += onAdEndedEvent;
        Vungle.onAdViewedEvent += onAdViewedEvent;
        Vungle.onCachedAdAvailableEvent += onCachedAdAvailableEvent;
    }


    void UnsetupAdsCallback()
    {
        Vungle.onAdStartedEvent -= onAdStartedEvent;
        Vungle.onAdEndedEvent -= onAdEndedEvent;
        Vungle.onAdViewedEvent -= onAdViewedEvent;
        Vungle.onCachedAdAvailableEvent -= onCachedAdAvailableEvent;
    }



    void onAdStartedEvent()
    {
        Debug.Log( "onAdStartedEvent" );
    }


    void onAdEndedEvent()
    {
        Debug.Log( "onAdEndedEvent" );

        EndGameRewardOverlay.Instance.SetMultiplier(2);
        EndGameRewardOverlay.Instance.Open();

        if (m_curAdsEngine == AdsEngine.VUNGLE) {
            Vungle.setSoundEnabled(true);       
        }

        m_curAdsStatus = AdsStatus.IDLE;
        SetNextAdEngine();

    }


    void onAdViewedEvent( double watched, double length )
    {
        Debug.Log( "onAdViewedEvent. watched: " + watched + ", length: " + length );
    }


    void onCachedAdAvailableEvent()
    {
        Debug.Log( "onCachedAdAvailableEvent" );
    }

    #endregion

    #endif

}
acegs
  • 2,621
  • 1
  • 22
  • 31

1 Answers1

1

Unity Ads and Vungle handle Ad Cacheing them self and there is not way to stop it. It is done as both networks show video Ads and video Ads take time to buffer if not pre cached before displaying.

Anas iqbal
  • 1,036
  • 8
  • 23