using System; using System.Collections.Generic; using System.Threading; using JetBrains.Annotations; using UnityEditor; namespace NugetForUnity.Helper { /// /// Helper class to dispatch actions on the Unity main thread. /// internal static class UnityMainThreadDispatcher { private static readonly List PreInitializedActionQueue = new List(); [CanBeNull] private static SynchronizationContext synchronizationContext = SynchronizationContext.Current; /// /// Add a action to be executed on the Unity Main thread. /// /// The action that is executed on the main thread. public static void Dispatch([NotNull] Action action) { if (action is null) { throw new ArgumentNullException(nameof(action)); } if (SynchronizationContext.Current != null) { // we are on main thread -> just execute action(); return; } if (synchronizationContext == null) { PreInitializedActionQueue.Add(action); return; } synchronizationContext.Post(_ => action(), null); } [InitializeOnLoadMethod] private static void Initialize() { if (synchronizationContext == null) { synchronizationContext = SynchronizationContext.Current; } if (synchronizationContext == null) { throw new InvalidOperationException("SynchronizationContext.Current is null in a InitializeOnLoadMethod."); } foreach (var action in PreInitializedActionQueue) { action(); } PreInitializedActionQueue.Clear(); } } }