Scene Picker: Fixed issue, where it would display deleted or unsaved scenes and selectable or no label entries

This commit is contained in:
Anders Ejlersen 2023-08-29 15:16:19 +02:00
parent 959f012d80
commit 5e0634e069

View file

@ -48,10 +48,14 @@ namespace Module.NavigationTool.Editor.Toolbar
for (var i = 0; i < SceneManager.sceneCount; i++) for (var i = 0; i < SceneManager.sceneCount; i++)
{ {
Scene scene = SceneManager.GetSceneAt(i); Scene scene = SceneManager.GetSceneAt(i);
if (string.IsNullOrEmpty(scene.path))
continue;
int index = listPaths.IndexOf(scene.path); int index = listPaths.IndexOf(scene.path);
if (index != -1) if (index != -1)
SELECTED_INDICES.Add(listPaths.IndexOf(scene.path)); SELECTED_INDICES.Add(index);
} }
SCENE_NAMES = listNames.ToArray(); SCENE_NAMES = listNames.ToArray();
@ -66,14 +70,19 @@ namespace Module.NavigationTool.Editor.Toolbar
{ {
EditorBuildSettingsScene[] scenes = EditorBuildSettings.scenes; 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; continue;
if (AssetDatabase.LoadAssetAtPath<SceneAsset>(path) == null)
listNames.Add(Path.GetFileNameWithoutExtension(scenes[i].path)); continue;
listShortNames.Add(listNames[i]);
listPaths.Add(scenes[i].path); 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) private static void SelectScene(int index)
{ {
string path = SCENE_PATHS[index]; try
Scene scene = SceneManager.GetSceneByPath(path); {
bool isScenePickerSetAsAdditive = ToolbarScenePickerSettings.IsScenePickerSetAsAdditive; string path = SCENE_PATHS[index];
Scene scene = SceneManager.GetSceneByPath(path);
bool isScenePickerSetAsAdditive = ToolbarScenePickerSettings.IsScenePickerSetAsAdditive;
if (scene.isLoaded) if (scene.isLoaded)
{ {
if (SceneManager.sceneCount == 1) if (SceneManager.sceneCount == 1)
return; return;
EditorSceneManager.CloseScene(scene, true); EditorSceneManager.CloseScene(scene, true);
SELECTED_INDICES.Remove(index); SELECTED_INDICES.Remove(index);
} }
else if (isScenePickerSetAsAdditive) else if (isScenePickerSetAsAdditive)
{ {
EditorSceneManager.OpenScene(SCENE_PATHS[index], OpenSceneMode.Additive); EditorSceneManager.OpenScene(SCENE_PATHS[index], OpenSceneMode.Additive);
SELECTED_INDICES.Add(index); SELECTED_INDICES.Add(index);
} }
else else
{ {
EditorSceneManager.OpenScene(SCENE_PATHS[index], OpenSceneMode.Single); EditorSceneManager.OpenScene(SCENE_PATHS[index], OpenSceneMode.Single);
SELECTED_INDICES.Clear(); SELECTED_INDICES.Clear();
SELECTED_INDICES.Add(index); SELECTED_INDICES.Add(index);
} }
RefreshSceneLabel(); RefreshSceneLabel();
}
catch (Exception e)
{
Debug.LogException(e);
}
} }
private static void RefreshSceneLabel() private static void RefreshSceneLabel()
{ {
STRING_BUILDER.Clear(); STRING_BUILDER.Clear();
for (var i = 0; i < SELECTED_INDICES.Count; i++) if (SELECTED_INDICES.Count != 0)
{ {
if (i > 0) for (var i = 0; i < SELECTED_INDICES.Count; i++)
STRING_BUILDER.Append(", "); {
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(); SCENE_LABEL = STRING_BUILDER.ToString();
} }