From 5e0634e0699acf4f11f3ee4f559c9e159f69daa1 Mon Sep 17 00:00:00 2001 From: Anders Ejlersen Date: Tue, 29 Aug 2023 15:16:19 +0200 Subject: [PATCH] Scene Picker: Fixed issue, where it would display deleted or unsaved scenes and selectable or no label entries --- Editor/Toolbar/Tools/ToolScenePicker.cs | 91 ++++++++++++++++--------- 1 file changed, 57 insertions(+), 34 deletions(-) diff --git a/Editor/Toolbar/Tools/ToolScenePicker.cs b/Editor/Toolbar/Tools/ToolScenePicker.cs index 49a84da..0782f37 100644 --- a/Editor/Toolbar/Tools/ToolScenePicker.cs +++ b/Editor/Toolbar/Tools/ToolScenePicker.cs @@ -48,10 +48,14 @@ namespace Module.NavigationTool.Editor.Toolbar for (var i = 0; i < SceneManager.sceneCount; i++) { Scene scene = SceneManager.GetSceneAt(i); + + if (string.IsNullOrEmpty(scene.path)) + continue; + int index = listPaths.IndexOf(scene.path); if (index != -1) - SELECTED_INDICES.Add(listPaths.IndexOf(scene.path)); + SELECTED_INDICES.Add(index); } SCENE_NAMES = listNames.ToArray(); @@ -66,14 +70,19 @@ namespace Module.NavigationTool.Editor.Toolbar { EditorBuildSettingsScene[] scenes = EditorBuildSettings.scenes; - for (var i = 0; i < scenes.Length; i++) + for (int i = 0, added = 0; i < scenes.Length; i++) { - if (string.IsNullOrEmpty(scenes[i].path)) + string path = scenes[i].path; + + if (string.IsNullOrEmpty(path)) continue; - - listNames.Add(Path.GetFileNameWithoutExtension(scenes[i].path)); - listShortNames.Add(listNames[i]); - listPaths.Add(scenes[i].path); + if (AssetDatabase.LoadAssetAtPath(path) == null) + continue; + + listNames.Add(Path.GetFileNameWithoutExtension(path)); + listShortNames.Add(listNames[added]); + listPaths.Add(path); + added++; } } @@ -220,45 +229,59 @@ namespace Module.NavigationTool.Editor.Toolbar private static void SelectScene(int index) { - string path = SCENE_PATHS[index]; - Scene scene = SceneManager.GetSceneByPath(path); - bool isScenePickerSetAsAdditive = ToolbarScenePickerSettings.IsScenePickerSetAsAdditive; + try + { + string path = SCENE_PATHS[index]; + Scene scene = SceneManager.GetSceneByPath(path); + bool isScenePickerSetAsAdditive = ToolbarScenePickerSettings.IsScenePickerSetAsAdditive; - if (scene.isLoaded) - { - if (SceneManager.sceneCount == 1) - return; + if (scene.isLoaded) + { + if (SceneManager.sceneCount == 1) + return; - EditorSceneManager.CloseScene(scene, true); - SELECTED_INDICES.Remove(index); - } - else if (isScenePickerSetAsAdditive) - { - EditorSceneManager.OpenScene(SCENE_PATHS[index], OpenSceneMode.Additive); - SELECTED_INDICES.Add(index); - } - else - { - EditorSceneManager.OpenScene(SCENE_PATHS[index], OpenSceneMode.Single); - SELECTED_INDICES.Clear(); - SELECTED_INDICES.Add(index); - } + EditorSceneManager.CloseScene(scene, true); + SELECTED_INDICES.Remove(index); + } + else if (isScenePickerSetAsAdditive) + { + EditorSceneManager.OpenScene(SCENE_PATHS[index], OpenSceneMode.Additive); + SELECTED_INDICES.Add(index); + } + else + { + EditorSceneManager.OpenScene(SCENE_PATHS[index], OpenSceneMode.Single); + SELECTED_INDICES.Clear(); + SELECTED_INDICES.Add(index); + } - RefreshSceneLabel(); + RefreshSceneLabel(); + } + catch (Exception e) + { + Debug.LogException(e); + } } private static void RefreshSceneLabel() { STRING_BUILDER.Clear(); - for (var i = 0; i < SELECTED_INDICES.Count; i++) + if (SELECTED_INDICES.Count != 0) { - if (i > 0) - STRING_BUILDER.Append(", "); + for (var i = 0; i < SELECTED_INDICES.Count; i++) + { + if (i > 0) + STRING_BUILDER.Append(", "); - STRING_BUILDER.Append(SCENE_SHORT_NAMES[SELECTED_INDICES[i]]); + STRING_BUILDER.Append(SCENE_SHORT_NAMES[SELECTED_INDICES[i]]); + } } - + else + { + STRING_BUILDER.Append("Unsaved scene(s)"); + } + SCENE_LABEL = STRING_BUILDER.ToString(); }