Skip to content

Instantly share code, notes, and snippets.

@LiekeVanmulken
Last active November 27, 2019 10:03
Show Gist options
  • Select an option

  • Save LiekeVanmulken/3ebdf4437c67dad20d6d2dd1d13cb6e7 to your computer and use it in GitHub Desktop.

Select an option

Save LiekeVanmulken/3ebdf4437c67dad20d6d2dd1d13cb6e7 to your computer and use it in GitHub Desktop.

Revisions

  1. Wouter Vanmulken renamed this gist Nov 27, 2019. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. Wouter Vanmulken created this gist Nov 27, 2019.
    37 changes: 37 additions & 0 deletions MainThreadDisplatcher.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    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);
    }
    }
    }