using System.Collections; using System.Collections.Generic; using UnityEngine; public class VungleAdsManager : UnitySingleton { private string appID = ""; private string placementID = ""; private bool adInited = false; private System.Action finishCallback = null; Dictionary placements = new Dictionary(); public void Init(System.Action finishCallback) { #if UNITY_IPHONE appID = "597a18ac6f7cda15620033e1"; placementID = "DEFAULT19439"; placements = new Dictionary { { placementID, false } }; #elif UNITY_ANDROID appID = "597a181605724327620031ed"; placementID = "DEFAULT72579"; placements = new Dictionary { { placementID, false } }; #endif string[] array = new string[placements.Keys.Count]; placements.Keys.CopyTo(array, 0); Vungle.init(appID, array); Debug.Log("Vungle Inited. appID is "+appID); this.finishCallback = finishCallback; InitializeEventHandlers(); } public void LoadAd(){ Vungle.loadAd(placementID); Debug.Log("Vungle load ad "); } public bool CheckAd(){ Debug.Log("Vungle isAdvertAvailable is "+Vungle.isAdvertAvailable(placementID)); return Vungle.isAdvertAvailable(placementID); } public void PlayAd(){ Vungle.playAd(placementID); Debug.Log("Vungle play ad"); } private void InitializeEventHandlers() { Vungle.onAdStartedEvent += (placementID) => { Debug.Log ("Ad " + placementID + " is starting! Pause your game animation or sound here."); }; Vungle.onAdFinishedEvent += (placementID, args) => { Debug.Log ("Ad finished - placementID " + placementID + ", was call to action clicked:" + args.WasCallToActionClicked + ", is completed view:" + args.IsCompletedView); if(args.IsCompletedView && finishCallback != null) finishCallback(); }; Vungle.adPlayableEvent += (placementID, adPlayable) => { Debug.Log ("Ad's playable state has been changed! placementID " + placementID + ". Now: " + adPlayable); }; Vungle.onLogEvent += (log) => { Debug.Log ("Log: " + log); }; Vungle.onInitializeEvent += () => { adInited = true; Debug.Log ("SDK initialized"); //自动加载广告 LoadAd(); }; } public bool IsAdInited(){ return adInited; } void OnApplicationPause(bool pauseStatus) { if (pauseStatus) { Vungle.onPause(); } else { Vungle.onResume(); } } }