using System; using System.Collections.Generic; using UnityEngine; namespace Runtime.ADAggregator { public class ADManager : MonoBehaviour { private static ADManager _instance; private static bool mIsCreate = false; public static ADManager Instance { get { if (_instance == null && !mIsCreate) { var objs = FindObjectsOfType(); for (int i = 0; i < objs.Length; i++) { Destroy(objs[i]); } _instance = new GameObject().AddComponent(); _instance.name = "[GameUpdater] NoInit"; DontDestroyOnLoad(_instance); _instance.InitTimeSystem(); mIsCreate = true; } return _instance; } private set => _instance = value; } #if UNITY_ANDROID public AndroidJavaObject activityContext { get; private set; } #endif /// /// 用于记录广告位状态 0 未加载 、 1 加载中、 2 加载成功 /// private Dictionary AD_Dicts; private ADConfig _adConfig; private IAdController _adController; private AsyncAdPlayer _curAsyncPlayer; private string _userId; public string UserId => _userId; #pragma warning disable CS0414 private bool _isInit = false; #pragma warning restore CS0414 public void Init(Action onCallback , string userId, ADConfig adConfig , IAdController controller , params object[] args) { _isInit = true; _instance.name = "[GameUpdater] Init"; AD_Dicts = new Dictionary(); _userId = userId; #if UNITY_EDITOR onCallback?.Invoke(); #else controller.Init(adConfig , args); _adController = controller; _adConfig = adConfig; onCallback?.Invoke(); #endif } public bool IsRealy(AD_Type adType) { #if UNITY_EDITOR return true; #endif #pragma warning disable CS0162 if (AD_Dicts.ContainsKey(adType) == false) { AD_Dicts[adType] = PlayerCreate(adType); } return AD_Dicts[adType].IsReadly(); #pragma warning restore CS0162 } public void LoadAD(AD_Type adType) { #if UNITY_EDITOR return; #endif // Debug.LogError("准备加载广告" + adType); #pragma warning disable CS0162 if (!IsRealy(adType)) { AD_Dicts[adType].LoadAD(); } #pragma warning restore CS0162 } /// /// 检查网络状态 /// /// public bool CheckNetwork() { return Application.internetReachability != NetworkReachability.NotReachable; } /// /// 直接加载并播放广告 /// /// /// public void AsyncPlayAD(AD_Type adType, string adScene, Action callback) { _adController.EventLog("adScene" , adScene); try { _curAsyncPlayer?.Kill(); if (AD_Dicts.ContainsKey(adType) == false) { AD_Dicts[adType] = PlayerCreate(adType); } var player = AD_Dicts[adType]; _curAsyncPlayer = new AsyncAdPlayer(player, callback); } catch (Exception e) { Debug.LogError(e); _adController.EventLog("error" , "showAdError" , e.Message); callback?.Invoke(false); } } public ADPlayer PlayerCreate(AD_Type type) { #if UNITY_EDITOR return null; #endif #pragma warning disable CS0162 return _adController.CreateAdPlayer(type); #pragma warning restore CS0162 } public void CloseAd(AD_Type adType) { #if UNITY_EDITOR return; #endif #pragma warning disable CS0162 if (adType != AD_Type.AwardVideo) { if (AD_Dicts.ContainsKey(adType)) { AD_Dicts[adType]?.CloseAD(); } } #pragma warning restore CS0162 } #region TimeSystem private List _updateList; private const int DefaultLength = 1024; private List _timeHandlers; private void InitTimeSystem() { this._updateList = new List(DefaultLength); this._timeHandlers = new List(); } private void _InvokeUpdate(List list) { for (int i = list.Count - 1; i >= 0; i--) { //在遍历时可能会出现外部操作list导致Count改变 if (i >= list.Count) continue; if (list[i] == null) { list.RemoveAt(i); } else { list[i].Invoke(); } } for (int i = this._timeHandlers.Count - 1; i >= 0; i--) { var timeHandler = this._timeHandlers[i]; if (timeHandler == null || timeHandler.IsDone || timeHandler._isKill) { this._timeHandlers.RemoveAt(i); } else { timeHandler.Update(); } } } private void Update() => this._InvokeUpdate(this._updateList); public AdTimeHandler CreateTimer(float delay, Action callback, Action update = null) { if (delay <= 0) { callback.Invoke(); return null; } var timer = new AdTimeHandler(delay, callback, update); this._timeHandlers.Add(timer); return timer; } public AdTimeHandler CreateOneFrameTimer(Action callback, Action update = null) { return this.CreateTimer(0.001f, callback, update); } public AdTimeHandler CreateTimer(GameObject gameObj, float delay, Action callback, Action update = null) { if (delay <= 0) { callback.Invoke(); return null; } var timer = new AdTimeHandler(gameObj, delay, callback, update); this._timeHandlers.Add(timer); return timer; } public void AddUpdater(Action updater) { this._updateList.Add(updater); } public void RemoveUpdater(Action updater) { this._updateList.Remove(updater); } #endregion public void CloseMask() { _adController.SetMask(false); } public void OpenMask() { _adController.SetMask(true); } } }