Last active
November 27, 2019 10:03
-
-
Save LiekeVanmulken/3ebdf4437c67dad20d6d2dd1d13cb6e7 to your computer and use it in GitHub Desktop.
Call the main thread in unity from the editor.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using UnityEditor; | |
/// <summary> | |
/// Call actions on the MainThread from a different thread. | |
/// </summary> | |
[InitializeOnLoad] | |
public class MainThreadDispatcher | |
{ | |
private static readonly Queue<Action> _executionQueue = new Queue<Action>(); | |
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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment