From cceef7bb7e0f4c3f18de8303a7d725219f227770 Mon Sep 17 00:00:00 2001 From: Anders Ejlersen Date: Tue, 26 Jul 2022 19:13:02 +0200 Subject: [PATCH] 1.7.0: Added option to sort scenes by asset labels --- Editor/Module.NavigationTool.Editor.asmdef | 2 +- Editor/Toolbar/Settings/IToolbarSettings.cs | 1 + .../Tools/Settings/ToolbarBuildSettings.cs | 6 +- .../Settings/ToolbarScenePickerSettings.cs | 73 ++++++++++++++++++- .../Tools/Settings/ToolbarTimeSettings.cs | 4 + .../Tools/Settings/ToolbarUiSettings.cs | 4 + Editor/Toolbar/Tools/ToolScenePicker.cs | 57 +++++++++++---- .../Utilities/ToolbarSettingsUtility.cs | 8 +- package.json | 2 +- 9 files changed, 136 insertions(+), 21 deletions(-) diff --git a/Editor/Module.NavigationTool.Editor.asmdef b/Editor/Module.NavigationTool.Editor.asmdef index 3e55a38..e2f2efc 100644 --- a/Editor/Module.NavigationTool.Editor.asmdef +++ b/Editor/Module.NavigationTool.Editor.asmdef @@ -11,7 +11,7 @@ "allowUnsafeCode": false, "overrideReferences": false, "precompiledReferences": [], - "autoReferenced": false, + "autoReferenced": true, "defineConstraints": [], "versionDefines": [], "noEngineReferences": false diff --git a/Editor/Toolbar/Settings/IToolbarSettings.cs b/Editor/Toolbar/Settings/IToolbarSettings.cs index 5e6c240..840d0d3 100644 --- a/Editor/Toolbar/Settings/IToolbarSettings.cs +++ b/Editor/Toolbar/Settings/IToolbarSettings.cs @@ -4,6 +4,7 @@ { string Title { get; } + void Initialize(); void Draw(); } } \ No newline at end of file diff --git a/Editor/Toolbar/Tools/Settings/ToolbarBuildSettings.cs b/Editor/Toolbar/Tools/Settings/ToolbarBuildSettings.cs index b5c5901..4f16090 100644 --- a/Editor/Toolbar/Tools/Settings/ToolbarBuildSettings.cs +++ b/Editor/Toolbar/Tools/Settings/ToolbarBuildSettings.cs @@ -20,7 +20,11 @@ namespace Module.NavigationTool.Editor.Toolbar get => EditorPrefs.GetBool(PREF_IS_BUILD_AND_RUN_ENABLED, true); set => EditorPrefs.SetBool(PREF_IS_BUILD_AND_RUN_ENABLED, value); } - + + public void Initialize() + { + } + public void Draw() { IsBuildEnabled = EditorGUILayout.Toggle("Enable Build Target picker", IsBuildEnabled); diff --git a/Editor/Toolbar/Tools/Settings/ToolbarScenePickerSettings.cs b/Editor/Toolbar/Tools/Settings/ToolbarScenePickerSettings.cs index a4dd75d..9e54574 100644 --- a/Editor/Toolbar/Tools/Settings/ToolbarScenePickerSettings.cs +++ b/Editor/Toolbar/Tools/Settings/ToolbarScenePickerSettings.cs @@ -1,4 +1,9 @@ -using UnityEditor; +using System; +using System.Collections.Generic; +using System.Linq; +using UnityEditor; +using UnityEditorInternal; +using UnityEngine; namespace Module.NavigationTool.Editor.Toolbar { @@ -7,16 +12,80 @@ namespace Module.NavigationTool.Editor.Toolbar public string Title => "Scene"; private const string PREF_IS_SCENE_ENABLED = "ToolbarSettings_IsSceneEnabled"; + private const string PREF_SCENE_ASSET_LABELS = "ToolbarSettings_SceneAssetLabels"; public static bool IsSceneEnabled { get => EditorPrefs.GetBool(PREF_IS_SCENE_ENABLED, true); - set => EditorPrefs.SetBool(PREF_IS_SCENE_ENABLED, value); + private set => EditorPrefs.SetBool(PREF_IS_SCENE_ENABLED, value); + } + + public static List SceneAssetLabels + { + get => EditorPrefs.GetString(PREF_SCENE_ASSET_LABELS).Split(',').ToList(); + private set => EditorPrefs.SetString(PREF_SCENE_ASSET_LABELS, string.Join(',', value)); + } + + private ReorderableList assetLabelList; + private List assetLabels; + + public void Initialize() + { + assetLabels = SceneAssetLabels.ToList(); + assetLabelList = new ReorderableList(assetLabels, typeof(string), true, true, true, true); + assetLabelList.drawElementCallback += OnAssetLabelListDrawElement; + assetLabelList.drawHeaderCallback += OnAssetLabelListDrawHeader; + assetLabelList.onAddCallback += OnAssetLabelListAddedElement; + assetLabelList.onRemoveCallback += OnAssetLabelListRemovedElement; + assetLabelList.onReorderCallback += OnAssetLabelListReorder; } public void Draw() { IsSceneEnabled = EditorGUILayout.Toggle("Enable Scene picker", IsSceneEnabled); + + if (assetLabelList == null) + return; + + assetLabelList.DoLayoutList(); + SceneAssetLabels = assetLabels; + } + + private static void OnAssetLabelListDrawHeader(Rect rect) + { + EditorGUI.LabelField(rect, "Scene sorting by Asset label"); + } + + private void OnAssetLabelListDrawElement(Rect rect, int index, bool isActive, bool isFocused) + { + rect.height -= 2; + rect.y += 1; + + string text = assetLabels[index]; + string temp = EditorGUI.TextField(rect, text); + + if (string.Equals(text, temp, StringComparison.Ordinal)) + return; + + assetLabels[index] = temp; + ToolScenePicker.SetAsDirty(); + } + + private void OnAssetLabelListAddedElement(ReorderableList list) + { + assetLabels.Add(string.Empty); + ToolScenePicker.SetAsDirty(); + } + + private void OnAssetLabelListRemovedElement(ReorderableList list) + { + assetLabels.RemoveAt(list.index); + ToolScenePicker.SetAsDirty(); + } + + private void OnAssetLabelListReorder(ReorderableList list) + { + ToolScenePicker.SetAsDirty(); } } } \ No newline at end of file diff --git a/Editor/Toolbar/Tools/Settings/ToolbarTimeSettings.cs b/Editor/Toolbar/Tools/Settings/ToolbarTimeSettings.cs index a0c9ab5..63cfc5b 100644 --- a/Editor/Toolbar/Tools/Settings/ToolbarTimeSettings.cs +++ b/Editor/Toolbar/Tools/Settings/ToolbarTimeSettings.cs @@ -29,6 +29,10 @@ namespace Module.NavigationTool.Editor.Toolbar set => EditorPrefs.SetFloat(PREF_TIME_SCALE_MAX, value); } + public void Initialize() + { + } + public void Draw() { IsTimeScaleEnabled = EditorGUILayout.Toggle("Enable Time Scale slider", IsTimeScaleEnabled); diff --git a/Editor/Toolbar/Tools/Settings/ToolbarUiSettings.cs b/Editor/Toolbar/Tools/Settings/ToolbarUiSettings.cs index 79e726d..7fb062b 100644 --- a/Editor/Toolbar/Tools/Settings/ToolbarUiSettings.cs +++ b/Editor/Toolbar/Tools/Settings/ToolbarUiSettings.cs @@ -21,6 +21,10 @@ namespace Module.NavigationTool.Editor.Toolbar set => EditorPrefs.SetBool(PREF_IS_UI_LAYER_ENABLED, value); } + public void Initialize() + { + } + public void Draw() { IsUiEnabled = EditorGUILayout.Toggle("Enable Canvas picker", IsUiEnabled); diff --git a/Editor/Toolbar/Tools/ToolScenePicker.cs b/Editor/Toolbar/Tools/ToolScenePicker.cs index 6c617bf..489eab4 100644 --- a/Editor/Toolbar/Tools/ToolScenePicker.cs +++ b/Editor/Toolbar/Tools/ToolScenePicker.cs @@ -57,18 +57,17 @@ namespace Module.NavigationTool.Editor.Toolbar private static void InitializeRemainingScenes(List listNames, List listPaths) { + List sortByAssetLabels = ToolbarScenePickerSettings.SceneAssetLabels; + var assetLabelToScenes = new SceneSortElement[sortByAssetLabels.Count + 1]; string[] guids = AssetDatabase.FindAssets("t:scene"); - int startIndex = listNames.Count; - + for (var i = 0; i < guids.Length; i++) { string path = AssetDatabase.GUIDToAssetPath(guids[i]); - if (listPaths.Contains(path)) + if (listPaths.Contains(path) || !path.StartsWith("Assets")) continue; - if (!path.StartsWith("Assets")) - continue; - + var scene = AssetDatabase.LoadAssetAtPath(path); if (scene == null) @@ -76,16 +75,40 @@ namespace Module.NavigationTool.Editor.Toolbar string sceneName = path.Substring(7, path.Length - 13) .Replace('/', '\\'); - - listNames.Add(sceneName); - listPaths.Add(path); + + 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].paths.Add(path); } - if (startIndex >= listNames.Count) - return; - - listNames.Insert(startIndex, string.Empty); - listPaths.Insert(startIndex, string.Empty); + for (var i = 0; i < assetLabelToScenes.Length; i++) + { + SceneSortElement e = assetLabelToScenes[i]; + + if (e == null) + continue; + + listNames.Add(string.Empty); + listPaths.Add(string.Empty); + + listNames.AddRange(e.names); + listPaths.AddRange(e.paths); + } } public override void Update() @@ -125,5 +148,11 @@ namespace Module.NavigationTool.Editor.Toolbar { IS_DIRTY = true; } + + private sealed class SceneSortElement + { + public readonly List names = new List(); + public readonly List paths = new List(); + } } } \ No newline at end of file diff --git a/Editor/Toolbar/Utilities/ToolbarSettingsUtility.cs b/Editor/Toolbar/Utilities/ToolbarSettingsUtility.cs index ceb96e7..788a0fe 100644 --- a/Editor/Toolbar/Utilities/ToolbarSettingsUtility.cs +++ b/Editor/Toolbar/Utilities/ToolbarSettingsUtility.cs @@ -25,8 +25,12 @@ namespace Module.NavigationTool.Editor.Toolbar { Type type = types[j]; - if (!type.IsInterface && !type.IsAbstract && iType.IsAssignableFrom(type)) - list.Add((IToolbarSettings)FormatterServices.GetUninitializedObject(type)); + if (type.IsInterface || type.IsAbstract || !iType.IsAssignableFrom(type)) + continue; + + var toolbar = (IToolbarSettings)FormatterServices.GetUninitializedObject(type); + toolbar.Initialize(); + list.Add(toolbar); } } diff --git a/package.json b/package.json index 0391a32..10f1fdb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "com.module.navigationtool", - "version": "1.6.0", + "version": "1.7.0", "displayName": "Module.NavigationTool", "description": "Support for navigation tools, like favorites, history and toolbars", "unity": "2019.2",