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;
if (AssetDatabase.LoadAssetAtPath<SceneAsset>(path) == null)
continue; continue;
listNames.Add(Path.GetFileNameWithoutExtension(scenes[i].path)); listNames.Add(Path.GetFileNameWithoutExtension(path));
listShortNames.Add(listNames[i]); listShortNames.Add(listNames[added]);
listPaths.Add(scenes[i].path); listPaths.Add(path);
added++;
} }
} }
@ -219,6 +228,8 @@ namespace Module.NavigationTool.Editor.Toolbar
} }
private static void SelectScene(int index) private static void SelectScene(int index)
{
try
{ {
string path = SCENE_PATHS[index]; string path = SCENE_PATHS[index];
Scene scene = SceneManager.GetSceneByPath(path); Scene scene = SceneManager.GetSceneByPath(path);
@ -246,11 +257,18 @@ namespace Module.NavigationTool.Editor.Toolbar
RefreshSceneLabel(); RefreshSceneLabel();
} }
catch (Exception e)
{
Debug.LogException(e);
}
}
private static void RefreshSceneLabel() private static void RefreshSceneLabel()
{ {
STRING_BUILDER.Clear(); STRING_BUILDER.Clear();
if (SELECTED_INDICES.Count != 0)
{
for (var i = 0; i < SELECTED_INDICES.Count; i++) for (var i = 0; i < SELECTED_INDICES.Count; i++)
{ {
if (i > 0) if (i > 0)
@ -258,6 +276,11 @@ namespace Module.NavigationTool.Editor.Toolbar
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();
} }