1.7.0: Added option to sort scenes by asset labels

This commit is contained in:
Anders Ejlersen 2022-07-26 19:13:02 +02:00
parent d9e62f6589
commit cceef7bb7e
9 changed files with 136 additions and 21 deletions

View file

@ -11,7 +11,7 @@
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": false,
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false

View file

@ -4,6 +4,7 @@
{
string Title { get; }
void Initialize();
void Draw();
}
}

View file

@ -21,6 +21,10 @@ namespace Module.NavigationTool.Editor.Toolbar
set => EditorPrefs.SetBool(PREF_IS_BUILD_AND_RUN_ENABLED, value);
}
public void Initialize()
{
}
public void Draw()
{
IsBuildEnabled = EditorGUILayout.Toggle("Enable Build Target picker", IsBuildEnabled);

View file

@ -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<string> 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<string> 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();
}
}
}

View file

@ -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);

View file

@ -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);

View file

@ -57,16 +57,15 @@ namespace Module.NavigationTool.Editor.Toolbar
private static void InitializeRemainingScenes(List<string> listNames, List<string> listPaths)
{
List<string> 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))
continue;
if (!path.StartsWith("Assets"))
if (listPaths.Contains(path) || !path.StartsWith("Assets"))
continue;
var scene = AssetDatabase.LoadAssetAtPath<SceneAsset>(path);
@ -77,15 +76,39 @@ 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 (startIndex >= listNames.Count)
return;
if (index == -1)
index = sortByAssetLabels.Count;
if (assetLabelToScenes[index] == null)
assetLabelToScenes[index] = new SceneSortElement();
listNames.Insert(startIndex, string.Empty);
listPaths.Insert(startIndex, string.Empty);
assetLabelToScenes[index].names.Add(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);
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<string> names = new List<string>();
public readonly List<string> paths = new List<string>();
}
}
}

View file

@ -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);
}
}

View file

@ -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",