SysSceneCleanup.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using Leopotam.Ecs;
  2. using UnityEditor;
  3. using UnityEditor.SceneManagement;
  4. using UnityEngine.SceneManagement;
  5. namespace Asset_Cleaner {
  6. class SceneToClose : IEcsAutoReset {
  7. public Scene Scene;
  8. public int SelectionId;
  9. public bool ForceClose;
  10. public void Reset() {
  11. ForceClose = default;
  12. Scene = default;
  13. SelectionId = default;
  14. }
  15. }
  16. class SysSceneCleanup : IEcsRunSystem, IEcsDestroySystem {
  17. EcsFilter<SceneToClose> ScenesToClose = default;
  18. public void Run() {
  19. if (ScenesToClose.IsEmpty()) return;
  20. var selectionId = Globals<PersistentUndoRedoState>.Value.Id;
  21. foreach (var i in ScenesToClose.Out(out var g1, out var entities)) {
  22. var s = g1[i].Scene;
  23. if (g1[i].SelectionId == selectionId && !g1[i].ForceClose) continue;
  24. if (Selection.activeGameObject && Selection.activeGameObject.scene == s) continue;
  25. if (s.isLoaded) EditorSceneManager.CloseScene(s, removeScene: true);
  26. entities[i].Destroy();
  27. }
  28. }
  29. // close scenes on window close
  30. public void Destroy() {
  31. foreach (var i in ScenesToClose.Out(out var g1, out _)) {
  32. var s = g1[i].Scene;
  33. if (s.isLoaded) EditorSceneManager.CloseScene(s, removeScene: true);
  34. }
  35. }
  36. }
  37. }