Skip to content

Instantly share code, notes, and snippets.

@LiekeVanmulken
Last active November 27, 2019 10:03
Show Gist options
  • Save LiekeVanmulken/3ebdf4437c67dad20d6d2dd1d13cb6e7 to your computer and use it in GitHub Desktop.
Save LiekeVanmulken/3ebdf4437c67dad20d6d2dd1d13cb6e7 to your computer and use it in GitHub Desktop.
Call the main thread in unity from the editor.
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