using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; namespace Azerion.BlueStack.Common { public class AdsEventExecutor : MonoBehaviour { public static AdsEventExecutor instance = null; private static List adEventsQueue = new List(); private static volatile bool adEventsQueueEmpty = true; public static void Initialize() { if (IsActive()) { return; } GameObject gameObject = new GameObject("AdsMainThreadExecuter"); gameObject.hideFlags = HideFlags.HideAndDontSave; DontDestroyOnLoad(gameObject); instance = gameObject.AddComponent(); } public static bool IsActive() { return instance != null; } public void Awake() { DontDestroyOnLoad(gameObject); } public static void ExecuteInUpdate(Action action) { if (action == null) return; object obj = adEventsQueue; lock (obj) { adEventsQueue.Add(action); adEventsQueueEmpty = false; } } public static void InvokeInUpdate(UnityEvent eventParam) { ExecuteInUpdate(delegate { eventParam.Invoke(); }); } public void Update() { if (adEventsQueueEmpty) { return; } List list = new List(); object obj = adEventsQueue; lock (obj) { list.AddRange(adEventsQueue); adEventsQueue.Clear(); adEventsQueueEmpty = true; } foreach (Action current in list) { if (current.Target != null) { current(); } } } public void OnDisable() { instance = null; } } }