c# - How to stop auto fetching/loading of ads from UnityAds and Vungle? -
i'm using both unity ads , vungle game. installed them using .unitypackage.
i finished codes displaying ads them alternately. problem how stop them fetching/loading after closing 1st ads? or have option stop fetching while fetches.
it make game crashes due memory pressure since 2 ads in memory plus resources game.
i browsed source codes , can't find api stop fetching or unload waiting-to-be-displayed ads. looked on documentations there's no detail that.
here 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 ads 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 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 loading of ads. //xxx: test code setnextadengine (); } public void showad(){ switch(m_curadsengine){ case adsengine.vungle: { debug.log("*** vungle ads available. playing...\n"); vungle.playad(); vungle.setsoundenabled(false); }break; case adsengine.unity_ads: { debug.log("*** unity ads available. 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 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 }
unity ads , vungle handle ad cacheing them self , there not way stop it. done both networks show video ads , video ads take time buffer if not pre cached before displaying.
Comments
Post a Comment