No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EditorDispatcher.cs 1.2KB

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