using System; using System.Collections.Generic; using UnityEngine; namespace GoogleMobileAds.Common { public class MobileAdsEventExecutor : MonoBehaviour { public static MobileAdsEventExecutor instance = null; private static List adEventsQueue = new List(); private static volatile bool adEventsQueueEmpty = true; public static void Initialize() { if (MobileAdsEventExecutor.IsActive()) { return; } GameObject gameObject = new GameObject("MobileAdsMainThreadExecuter"); gameObject.hideFlags = HideFlags.HideAndDontSave; UnityEngine.Object.DontDestroyOnLoad(gameObject); MobileAdsEventExecutor.instance = gameObject.AddComponent(); } public static bool IsActive() { return MobileAdsEventExecutor.instance != null; } public void Awake() { UnityEngine.Object.DontDestroyOnLoad(base.gameObject); } public static void ExecuteInUpdate(Action action) { List obj = MobileAdsEventExecutor.adEventsQueue; lock (obj) { MobileAdsEventExecutor.adEventsQueue.Add(action); MobileAdsEventExecutor.adEventsQueueEmpty = false; } } public void Update() { if (MobileAdsEventExecutor.adEventsQueueEmpty) { return; } List list = new List(); List obj = MobileAdsEventExecutor.adEventsQueue; lock (obj) { list.AddRange(MobileAdsEventExecutor.adEventsQueue); MobileAdsEventExecutor.adEventsQueue.Clear(); MobileAdsEventExecutor.adEventsQueueEmpty = true; } foreach (Action action in list) { action(); } } public void OnDisable() { MobileAdsEventExecutor.instance = null; } } }