AufWindow.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System.Diagnostics;
  2. using UnityEditor;
  3. using UnityEngine;
  4. using Debug = UnityEngine.Debug;
  5. namespace Asset_Cleaner {
  6. class AufWindow : EditorWindow {
  7. [SerializeField] PersistentUndoRedoState _persistentUndo;
  8. [MenuItem("Window/- Asset Cleaner %L")]
  9. static void OpenActiveWindow() {
  10. GetWindow<AufWindow>();
  11. }
  12. // restore window state after recompilation
  13. void OnEnable() {
  14. var wd = Globals<WindowData>.Value = new WindowData();
  15. wd.Window = this;
  16. var firstTime = _persistentUndo == null;
  17. Globals<PersistentUndoRedoState>.Value = _persistentUndo ?? new PersistentUndoRedoState();
  18. Globals<BacklinkStore>.Value = new BacklinkStore();
  19. var config = Globals<Config>.Value = new Config();
  20. PersistenceUtils.Load(ref config);
  21. if (firstTime || !config.RebuildCacheOnDemand)
  22. Globals<BacklinkStore>.Value.Init();
  23. EditorApplication.update += Upd;
  24. EditorApplication.projectWindowItemOnGUI += ProjectViewGui.OnProjectWindowItemOnGui;
  25. AufCtx.TryInitWorld();
  26. // need to close window in case of Asset Cleaner uninstalled
  27. if (!CleanerStyleAsset.Style.TryFindSelf(out wd.Style))
  28. ForceClose();
  29. }
  30. void OnGUI() {
  31. var store = Globals<BacklinkStore>.Value;
  32. if (!store.Initialized) {
  33. // prevent further window GUI rendering
  34. if (!GUILayout.Button("Initialize Cache")) return;
  35. var stopwatch = new Stopwatch();
  36. stopwatch.Start();
  37. store.Init();
  38. stopwatch.Stop();
  39. Globals<Config>.Value.InitializationTime = $"Initialized in {stopwatch.Elapsed.TotalSeconds:N} s";
  40. AufCtx.World.NewEntityWith(out RequestRepaintEvt _);
  41. }
  42. AufCtx.OnGuiGroup.Run();
  43. }
  44. static void Upd() {
  45. if (AufCtx.World == null) {
  46. AufCtx.DestroyWorld();
  47. return;
  48. }
  49. if (!Globals<BacklinkStore>.Value.Initialized) return;
  50. AufCtx.UpdateGroup.Run();
  51. }
  52. bool _closing;
  53. void ForceClose() {
  54. if (_closing) return;
  55. _closing = true;
  56. Close();
  57. EditorWindow.DestroyImmediate(this);
  58. }
  59. void OnDisable() {
  60. if (AufCtx.Destroyed) return;
  61. _persistentUndo = Globals<PersistentUndoRedoState>.Value;
  62. AufCtx.UpdateGroup.Destroy();
  63. AufCtx.OnGuiGroup.Destroy();
  64. AufCtx.DestroyWorld();
  65. Globals<Config>.Value = default;
  66. Globals<PersistentUndoRedoState>.Value = default;
  67. Globals<WindowData>.Value = default;
  68. EditorApplication.update -= Upd;
  69. EditorApplication.projectWindowItemOnGUI -= ProjectViewGui.OnProjectWindowItemOnGui;
  70. // need to close window in case of Asset Cleaner uninstalled
  71. if (!CleanerStyleAsset.Style.TryFindSelf(out _))
  72. ForceClose();
  73. }
  74. }
  75. }