Scene picker: Added option to add groups of scenes to open
This commit is contained in:
parent
41604b98e7
commit
a86f76904e
28 changed files with 1052 additions and 225 deletions
|
|
@ -1,7 +1,4 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using JetBrains.Annotations;
|
||||
using UnityEditor;
|
||||
|
|
@ -18,134 +15,22 @@ namespace Module.NavigationTool.Editor.Toolbar
|
|||
public override bool Enabled => !EditorApplication.isPlaying && !EditorApplication.isPlayingOrWillChangePlaymode;
|
||||
public override EToolbarPlacement Placement => EToolbarPlacement.Right;
|
||||
public override int Priority => (int)EToolbarPriority.Medium;
|
||||
|
||||
private static readonly List<int> SELECTED_INDICES = new List<int>();
|
||||
private static readonly StringBuilder STRING_BUILDER = new StringBuilder();
|
||||
private static string SCENE_LABEL = string.Empty;
|
||||
private static string[] SCENE_NAMES = new string[0];
|
||||
private static string[] SCENE_SHORT_NAMES = new string[0];
|
||||
private static string[] SCENE_PATHS = new string[0];
|
||||
private static readonly GUIContent LABEL_SCENE_ADDITIVE = new GUIContent(string.Empty, "Additive scene loading (toogle to Single)");
|
||||
private static readonly GUIContent LABEL_SCENE_SINGLE = new GUIContent(string.Empty, "Single scene loading (toggle to Additive)");
|
||||
private static readonly GUIContent LABEL_SCENE_CREATE = new GUIContent(string.Empty, "Create a new scene");
|
||||
private const float BUTTON_WIDTH = 24.0f;
|
||||
|
||||
private static readonly StringBuilder STRING_BUILDER = new StringBuilder();
|
||||
private static readonly ScenePickerList SCENE_LIST = new ScenePickerList();
|
||||
private static string SCENE_LABEL = string.Empty;
|
||||
private static bool IS_DIRTY = true;
|
||||
|
||||
|
||||
private static void Initialize()
|
||||
{
|
||||
if (!IS_DIRTY)
|
||||
return;
|
||||
|
||||
SELECTED_INDICES.Clear();
|
||||
var listNames = new List<string>();
|
||||
var listShortNames = new List<string>();
|
||||
var listPaths = new List<string>();
|
||||
|
||||
InitializeBuildSettingsScenes(listNames, listShortNames, listPaths);
|
||||
InitializeRemainingScenes(listNames, listShortNames, listPaths);
|
||||
|
||||
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(index);
|
||||
}
|
||||
|
||||
SCENE_NAMES = listNames.ToArray();
|
||||
SCENE_SHORT_NAMES = listShortNames.ToArray();
|
||||
SCENE_PATHS = listPaths.ToArray();
|
||||
SCENE_LIST.Refresh();
|
||||
RefreshSceneLabel();
|
||||
IS_DIRTY = false;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static void InitializeBuildSettingsScenes(List<string> listNames, List<string> listShortNames, List<string> listPaths)
|
||||
{
|
||||
EditorBuildSettingsScene[] scenes = EditorBuildSettings.scenes;
|
||||
|
||||
for (int i = 0, added = 0; i < scenes.Length; i++)
|
||||
{
|
||||
string path = scenes[i].path;
|
||||
|
||||
if (string.IsNullOrEmpty(path))
|
||||
continue;
|
||||
if (AssetDatabase.LoadAssetAtPath<SceneAsset>(path) == null)
|
||||
continue;
|
||||
|
||||
listNames.Add(Path.GetFileNameWithoutExtension(path));
|
||||
listShortNames.Add(listNames[added]);
|
||||
listPaths.Add(path);
|
||||
added++;
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static void InitializeRemainingScenes(List<string> listNames, List<string> listShortNames, List<string> listPaths)
|
||||
{
|
||||
List<string> sortByAssetLabels = ToolbarScenePickerSettings.SceneAssetLabels;
|
||||
var assetLabelToScenes = new SceneSortElement[sortByAssetLabels.Count + 1];
|
||||
string[] guids = AssetDatabase.FindAssets("t:scene");
|
||||
|
||||
for (var i = 0; i < guids.Length; i++)
|
||||
{
|
||||
string path = AssetDatabase.GUIDToAssetPath(guids[i]);
|
||||
|
||||
if (listPaths.Contains(path) || !path.StartsWith("Assets"))
|
||||
continue;
|
||||
|
||||
var scene = AssetDatabase.LoadAssetAtPath<SceneAsset>(path);
|
||||
|
||||
if (scene == null)
|
||||
continue;
|
||||
|
||||
string sceneName = path.Substring(7, path.Length - 13)
|
||||
.Replace('/', '\\');
|
||||
|
||||
string[] assetLabels = AssetDatabase.GetLabels(scene);
|
||||
int index = -1;
|
||||
|
||||
for (var j = 0; j < assetLabels.Length; j++)
|
||||
{
|
||||
index = sortByAssetLabels.IndexOf(assetLabels[j]);
|
||||
|
||||
if (index != -1)
|
||||
break;
|
||||
}
|
||||
|
||||
if (index == -1)
|
||||
index = sortByAssetLabels.Count;
|
||||
if (assetLabelToScenes[index] == null)
|
||||
assetLabelToScenes[index] = new SceneSortElement();
|
||||
|
||||
assetLabelToScenes[index].names.Add(sceneName);
|
||||
assetLabelToScenes[index].shortNames.Add(Path.GetFileName(sceneName));
|
||||
assetLabelToScenes[index].paths.Add(path);
|
||||
}
|
||||
|
||||
for (var i = 0; i < assetLabelToScenes.Length; i++)
|
||||
{
|
||||
SceneSortElement e = assetLabelToScenes[i];
|
||||
|
||||
if (e == null)
|
||||
continue;
|
||||
|
||||
listNames.Add(string.Empty);
|
||||
listShortNames.Add(string.Empty);
|
||||
listPaths.Add(string.Empty);
|
||||
|
||||
listNames.AddRange(e.names);
|
||||
listShortNames.AddRange(e.shortNames);
|
||||
listPaths.AddRange(e.paths);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Update()
|
||||
{
|
||||
Initialize();
|
||||
|
|
@ -155,9 +40,9 @@ namespace Module.NavigationTool.Editor.Toolbar
|
|||
{
|
||||
Initialize();
|
||||
|
||||
var rect0 = new Rect(rect.x, rect.y, rect.width - BUTTON_WIDTH * 2.0f, rect.height);
|
||||
var rect1 = new Rect(rect0.xMax, rect.y, BUTTON_WIDTH, rect.height);
|
||||
var rect2 = new Rect(rect1.xMax, rect.y, BUTTON_WIDTH, rect.height);
|
||||
var rect0 = new Rect(rect.x, rect.y, rect.width - Styles.BUTTON_WIDTH * 2.0f, rect.height);
|
||||
var rect1 = new Rect(rect0.xMax, rect.y, Styles.BUTTON_WIDTH, rect.height);
|
||||
var rect2 = new Rect(rect1.xMax, rect.y, Styles.BUTTON_WIDTH, rect.height);
|
||||
DrawSceneDropDown(rect0);
|
||||
DrawSceneLoadToggle(rect1);
|
||||
DrawSceneAddButton(rect2);
|
||||
|
|
@ -177,12 +62,12 @@ namespace Module.NavigationTool.Editor.Toolbar
|
|||
if (tempIsAdditive)
|
||||
{
|
||||
GUI.Label(rect, styles.iconSceneAdditive, styles.labelCenter);
|
||||
GUI.Label(rect, LABEL_SCENE_ADDITIVE, styles.labelCenter);
|
||||
GUI.Label(rect, Styles.LABEL_SCENE_ADDITIVE, styles.labelCenter);
|
||||
}
|
||||
else
|
||||
{
|
||||
GUI.Label(rect, styles.iconSceneSingle, styles.labelCenter);
|
||||
GUI.Label(rect, LABEL_SCENE_SINGLE, styles.labelCenter);
|
||||
GUI.Label(rect, Styles.LABEL_SCENE_SINGLE, styles.labelCenter);
|
||||
}
|
||||
|
||||
if (tempIsAdditive != isScenePickerSetAsAdditive)
|
||||
|
|
@ -211,17 +96,17 @@ namespace Module.NavigationTool.Editor.Toolbar
|
|||
}
|
||||
}
|
||||
|
||||
GUI.Label(rect, LABEL_SCENE_CREATE, styles.labelCenter);
|
||||
GUI.Label(rect, Styles.LABEL_SCENE_CREATE, styles.labelCenter);
|
||||
}
|
||||
|
||||
private static void ShowDropDown(Rect rect)
|
||||
{
|
||||
var menu = new GenericMenu();
|
||||
|
||||
for (var i = 0; i < SCENE_NAMES.Length; i++)
|
||||
for (var i = 0; i < SCENE_LIST.labels.Length; i++)
|
||||
{
|
||||
int sceneIndex = i;
|
||||
menu.AddItem(new GUIContent(SCENE_NAMES[i]), SELECTED_INDICES.Contains(i), () => SelectScene(sceneIndex));
|
||||
menu.AddItem(SCENE_LIST.labels[i], SCENE_LIST.selected.Contains(i), () => SelectScene(sceneIndex));
|
||||
}
|
||||
|
||||
menu.DropDown(rect);
|
||||
|
|
@ -231,30 +116,59 @@ namespace Module.NavigationTool.Editor.Toolbar
|
|||
{
|
||||
try
|
||||
{
|
||||
string path = SCENE_PATHS[index];
|
||||
Scene scene = SceneManager.GetSceneByPath(path);
|
||||
ScenePickerList.SceneElement scenes = SCENE_LIST.scenes[index];
|
||||
bool isScenePickerSetAsAdditive = ToolbarScenePickerSettings.IsScenePickerSetAsAdditive;
|
||||
|
||||
if (scene.isLoaded)
|
||||
{
|
||||
if (SceneManager.sceneCount == 1)
|
||||
return;
|
||||
|
||||
EditorSceneManager.CloseScene(scene, true);
|
||||
SELECTED_INDICES.Remove(index);
|
||||
}
|
||||
else if (isScenePickerSetAsAdditive)
|
||||
for (var i = 0; i < scenes.paths.Count; i++)
|
||||
{
|
||||
EditorSceneManager.OpenScene(SCENE_PATHS[index], OpenSceneMode.Additive);
|
||||
SELECTED_INDICES.Add(index);
|
||||
string path = scenes.paths[i];
|
||||
Scene scene = SceneManager.GetSceneByPath(path);
|
||||
|
||||
if (scenes.isGroup)
|
||||
{
|
||||
if (isScenePickerSetAsAdditive || i != 0)
|
||||
{
|
||||
EditorSceneManager.OpenScene(path, OpenSceneMode.Additive);
|
||||
int sceneIndex = SCENE_LIST.IndexOfPath(path, false);
|
||||
|
||||
if (sceneIndex != -1)
|
||||
SCENE_LIST.selected.Add(sceneIndex);
|
||||
}
|
||||
else
|
||||
{
|
||||
EditorSceneManager.OpenScene(path, OpenSceneMode.Single);
|
||||
int sceneIndex = SCENE_LIST.IndexOfPath(path, false);
|
||||
|
||||
SCENE_LIST.selected.Clear();
|
||||
|
||||
if (sceneIndex != -1)
|
||||
SCENE_LIST.selected.Add(index);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (scene.isLoaded)
|
||||
{
|
||||
if (SceneManager.sceneCount == 1)
|
||||
return;
|
||||
|
||||
EditorSceneManager.CloseScene(scene, true);
|
||||
SCENE_LIST.selected.Remove(index);
|
||||
}
|
||||
else if (isScenePickerSetAsAdditive)
|
||||
{
|
||||
EditorSceneManager.OpenScene(path, OpenSceneMode.Additive);
|
||||
SCENE_LIST.selected.Add(index);
|
||||
}
|
||||
else
|
||||
{
|
||||
EditorSceneManager.OpenScene(path, OpenSceneMode.Single);
|
||||
SCENE_LIST.selected.Clear();
|
||||
SCENE_LIST.selected.Add(index);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
EditorSceneManager.OpenScene(SCENE_PATHS[index], OpenSceneMode.Single);
|
||||
SELECTED_INDICES.Clear();
|
||||
SELECTED_INDICES.Add(index);
|
||||
}
|
||||
|
||||
|
||||
RefreshSceneLabel();
|
||||
}
|
||||
catch (Exception e)
|
||||
|
|
@ -267,14 +181,14 @@ namespace Module.NavigationTool.Editor.Toolbar
|
|||
{
|
||||
STRING_BUILDER.Clear();
|
||||
|
||||
if (SELECTED_INDICES.Count != 0)
|
||||
if (SCENE_LIST.selected.Count != 0)
|
||||
{
|
||||
for (var i = 0; i < SELECTED_INDICES.Count; i++)
|
||||
for (var i = 0; i < SCENE_LIST.selected.Count; i++)
|
||||
{
|
||||
if (i > 0)
|
||||
STRING_BUILDER.Append(", ");
|
||||
|
||||
STRING_BUILDER.Append(SCENE_SHORT_NAMES[SELECTED_INDICES[i]]);
|
||||
STRING_BUILDER.Append(SCENE_LIST.scenes[SCENE_LIST.selected[i]].shortname);
|
||||
}
|
||||
}
|
||||
else
|
||||
|
|
@ -287,19 +201,20 @@ namespace Module.NavigationTool.Editor.Toolbar
|
|||
|
||||
public override float CalculateWidth()
|
||||
{
|
||||
return 100.0f + BUTTON_WIDTH * 2.0f;
|
||||
return 100.0f + Styles.BUTTON_WIDTH * 2.0f;
|
||||
}
|
||||
|
||||
public static void SetAsDirty()
|
||||
{
|
||||
IS_DIRTY = true;
|
||||
}
|
||||
|
||||
private sealed class SceneSortElement
|
||||
|
||||
private static class Styles
|
||||
{
|
||||
public readonly List<string> names = new List<string>();
|
||||
public readonly List<string> shortNames = new List<string>();
|
||||
public readonly List<string> paths = new List<string>();
|
||||
public static readonly GUIContent LABEL_SCENE_ADDITIVE = new GUIContent(string.Empty, "Additive scene loading (toogle to Single)");
|
||||
public static readonly GUIContent LABEL_SCENE_SINGLE = new GUIContent(string.Empty, "Single scene loading (toggle to Additive)");
|
||||
public static readonly GUIContent LABEL_SCENE_CREATE = new GUIContent(string.Empty, "Create a new scene");
|
||||
public const float BUTTON_WIDTH = 24.0f;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue