using System; using System.Collections.Generic; using UnityEditor; /// /// Call actions on the MainThread from a different thread. /// [InitializeOnLoad] public class MainThreadDispatcher { private static readonly Queue _executionQueue = new Queue(); static MainThreadDispatcher() { EditorApplication.update += Update; } static void Update() { lock (_executionQueue) { while (_executionQueue.Count > 0) { _executionQueue.Dequeue().Invoke(); } } } public static void Enqueue(Action action) { lock (_executionQueue) { _executionQueue.Enqueue(action); } } }