如何停止从UnityAds和Vungle自动获取/加载广告?

时间:2015-04-29 04:52:51

标签: c# unity3d ads vungle-ads

我在我的游戏中使用Unity Ads和Vungle。我使用.unitypackage安装了它们。

我已经完成了交替显示广告的代码。我现在的问题是如何在关闭第一个广告后阻止他们获取/加载?或者可以选择在提取时停止提取。

由于内存压力导致我的游戏崩溃,因为2个广告已经在内存中加上我游戏的资源。

我浏览了源代码,无法找到任何API来停止提取或卸载等待显示的广告。我看了他们的文件,但没有详细说明。

这是我目前的代码:

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

}

1 个答案:

答案 0 :(得分:1)

Unity Ads和Vungle处理广告自我缓存,无法阻止它。这样做是因为两个网络都显示视频广告,如果没有预先缓存,则视频广告需要时间缓冲。