From 144287ba1c4ea1c222eeae29e9dc2c02191cf943 Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Wed, 3 May 2023 20:38:55 -0500 Subject: [PATCH] Add `Unload all but this scene` option to scene context menu to unload all of the active scenes except for the selected one. --- Source/Editor/Modules/SceneModule.cs | 36 ++++++++++++++++++++ Source/Editor/SceneGraph/Actors/SceneNode.cs | 7 ++++ 2 files changed, 43 insertions(+) diff --git a/Source/Editor/Modules/SceneModule.cs b/Source/Editor/Modules/SceneModule.cs index 4d1aee93e..0a51038ac 100644 --- a/Source/Editor/Modules/SceneModule.cs +++ b/Source/Editor/Modules/SceneModule.cs @@ -315,6 +315,42 @@ namespace FlaxEditor.Modules Editor.StateMachine.ChangingScenesState.UnloadScene(Level.Scenes); } + /// + /// Closes all of the scenes except for the specified scene (async). + /// + /// The scene to not close. + public void CloseAllScenesExcept(Scene scene) + { + // Check if cannot change scene now + if (!Editor.StateMachine.CurrentState.CanChangeScene) + return; + + var scenes = new List(); + foreach (var s in Level.Scenes) + { + if (s == scene) + continue; + scenes.Add(s); + } + + // In play-mode Editor mocks the level streaming script + if (Editor.IsPlayMode) + { + foreach (var s in scenes) + { + Level.UnloadSceneAsync(s); + } + return; + } + + // Ensure to save all pending changes + if (CheckSaveBeforeClose()) + return; + + // Unload scenes + Editor.StateMachine.ChangingScenesState.UnloadScene(scenes); + } + /// /// Show save before scene load/unload action. /// diff --git a/Source/Editor/SceneGraph/Actors/SceneNode.cs b/Source/Editor/SceneGraph/Actors/SceneNode.cs index df7a11b1a..0405e4fdf 100644 --- a/Source/Editor/SceneGraph/Actors/SceneNode.cs +++ b/Source/Editor/SceneGraph/Actors/SceneNode.cs @@ -77,6 +77,8 @@ namespace FlaxEditor.SceneGraph.Actors } contextMenu.AddButton("Save scene", OnSave).LinkTooltip("Saves this scene.").Enabled = IsEdited && !Editor.IsPlayMode; contextMenu.AddButton("Unload scene", OnUnload).LinkTooltip("Unloads this scene.").Enabled = Editor.Instance.StateMachine.CurrentState.CanChangeScene; + if (Level.ScenesCount > 1) + contextMenu.AddButton("Unload all but this scene", OnUnloadAllButSelectedScene).LinkTooltip("Unloads all of the active scenes except for the selected scene.").Enabled = Editor.Instance.StateMachine.CurrentState.CanChangeScene; base.OnContextMenu(contextMenu); } @@ -95,5 +97,10 @@ namespace FlaxEditor.SceneGraph.Actors { Editor.Instance.Scene.CloseScene(Scene); } + + private void OnUnloadAllButSelectedScene() + { + Editor.Instance.Scene.CloseAllScenesExcept(Scene); + } } }