EditorStatusWatcher.cs 955 B

1234567891011121314151617181920212223242526272829303132
  1. using HybridCLR.Editor;
  2. using System;
  3. using UnityEditor;
  4. using UnityEditorInternal;
  5. namespace HybridCLR.Editor.Settings
  6. {
  7. /// <summary>
  8. /// 监听编辑器状态,当编辑器重新 focus 时,重新加载实例,避免某些情景下 svn 、git 等外部修改了数据却无法同步的异常。
  9. /// </summary>
  10. [InitializeOnLoad]
  11. public static class EditorStatusWatcher
  12. {
  13. public static Action OnEditorFocused;
  14. static bool isFocused;
  15. static EditorStatusWatcher() => EditorApplication.update += Update;
  16. static void Update()
  17. {
  18. if (isFocused != InternalEditorUtility.isApplicationActive)
  19. {
  20. isFocused = InternalEditorUtility.isApplicationActive;
  21. if (isFocused)
  22. {
  23. HybridCLRSettings.LoadOrCreate();
  24. OnEditorFocused?.Invoke();
  25. }
  26. }
  27. }
  28. }
  29. }