EditorDispatcher.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using UnityEditor;
  5. namespace Unity.PlasticSCM.Editor.UI
  6. {
  7. [InitializeOnLoad]
  8. internal static class EditorDispatcher
  9. {
  10. static EditorDispatcher()
  11. {
  12. mMainThread = Thread.CurrentThread;
  13. }
  14. internal static bool IsOnMainThread
  15. {
  16. get { return Thread.CurrentThread == mMainThread; }
  17. }
  18. internal static void Dispatch(Action task)
  19. {
  20. lock (mDispatchQueue)
  21. {
  22. if (mDispatchQueue.Count == 0)
  23. EditorApplication.update += Update;
  24. mDispatchQueue.Enqueue(task);
  25. }
  26. }
  27. internal static void Update()
  28. {
  29. Action[] actions;
  30. lock (mDispatchQueue)
  31. {
  32. if (mDispatchQueue.Count == 0)
  33. return;
  34. actions = mDispatchQueue.ToArray();
  35. mDispatchQueue.Clear();
  36. EditorApplication.update -= Update;
  37. }
  38. foreach (Action action in actions)
  39. action();
  40. }
  41. static readonly Queue<Action> mDispatchQueue = new Queue<Action>();
  42. static Thread mMainThread;
  43. }
  44. }