Threading.cs 875 B

12345678910111213141516171819202122232425262728293031
  1. using System;
  2. using UnityEditor;
  3. using UnityEditorInternal;
  4. using UnityEngine;
  5. namespace Unity.Cloud.Collaborate.Utilities
  6. {
  7. internal static class Threading
  8. {
  9. /// <summary>
  10. /// Returns true if the current thread is the main thread, false otherwise.
  11. /// </summary>
  12. public static bool IsMainThread => InternalEditorUtility.CurrentThreadIsMainThread();
  13. /// <summary>
  14. /// Ensure that the provided action is executed on the UI/main thread.
  15. /// </summary>
  16. /// <param name="action">Action to perform on the UI/main thread.</param>
  17. public static void EnsureUiThread(Action action)
  18. {
  19. if (IsMainThread)
  20. {
  21. action();
  22. }
  23. else
  24. {
  25. EditorApplication.delayCall += () => action();
  26. }
  27. }
  28. }
  29. }