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++)
{
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;
if (AssetDatabase.LoadAssetAtPath<SceneAsset>(path) == null)
continue;
listNames.Add(Path.GetFileNameWithoutExtension(scenes[i].path));
listShortNames.Add(listNames[i]);
listPaths.Add(scenes[i].path);
listNames.Add(Path.GetFileNameWithoutExtension(path));
listShortNames.Add(listNames[added]);
listPaths.Add(path);
added++;
}
}
@ -220,43 +229,57 @@ 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;
if (scene.isLoaded)
try
{
if (SceneManager.sceneCount == 1)
return;
string path = SCENE_PATHS[index];
Scene scene = SceneManager.GetSceneByPath(path);
bool isScenePickerSetAsAdditive = ToolbarScenePickerSettings.IsScenePickerSetAsAdditive;
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);
}
if (scene.isLoaded)
{
if (SceneManager.sceneCount == 1)
return;
RefreshSceneLabel();
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();
}
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();