From 71cfa9bd321729457f2b4a862e44bf5a5a295a3e Mon Sep 17 00:00:00 2001 From: BennyKok Date: Mon, 1 Feb 2021 21:54:33 +0800 Subject: [PATCH] feat: using Settings Manager for editor settings BREAKING CHANGE: old settings will be lost, please remove the old HeaderSettings scriptableobject --- Editor/HeaderEditor.cs | 27 ++-- Editor/HeaderSettings.cs | 145 +++++++----------- ...om.bennykok.hierarchy-header.editor.asmdef | 3 +- package.json | 5 +- 4 files changed, 75 insertions(+), 105 deletions(-) diff --git a/Editor/HeaderEditor.cs b/Editor/HeaderEditor.cs index f74c77c..71583b9 100644 --- a/Editor/HeaderEditor.cs +++ b/Editor/HeaderEditor.cs @@ -9,8 +9,8 @@ public class HeaderEditor : UnityEditor.Editor { public static void UpdateAllHeader() { - var targetType = HeaderSettings.Instance.type; - var targetAlignment = HeaderSettings.Instance.alignment; + var targetType = HeaderSettings.headerType; + var targetAlignment = HeaderSettings.alignment; var allHeader = GameObject.FindObjectsOfType
(); foreach (var header in allHeader) { @@ -21,21 +21,27 @@ public static void UpdateAllHeader() } } + public static string GetSimpleTitle(string prefix, string title) + { + if (prefix == null || prefix.Length <= 0) return title; + return GetSimpleTitle(prefix[0], title); + } + public static string GetSimpleTitle(char prefix, string title) { - var maxCharLength = HeaderSettings.Instance.maxLength; + var maxCharLength = HeaderSettings.maxLength; var charLength = maxCharLength - title.Length; var leftSize = 0; var rightSize = 0; - switch (HeaderSettings.Instance.alignment) + switch (HeaderSettings.alignment.value) { case HeaderAlignment.Start: - leftSize = HeaderSettings.Instance.minPrefixLength; + leftSize = HeaderSettings.minPrefixLength; rightSize = charLength - leftSize; break; case HeaderAlignment.End: - rightSize = HeaderSettings.Instance.minPrefixLength; + rightSize = HeaderSettings.minPrefixLength; leftSize = charLength - rightSize; break; case HeaderAlignment.Center: @@ -59,12 +65,12 @@ public static string GetSimpleTitle(char prefix, string title) public static string GetFormattedTitle(string title) { - switch (HeaderSettings.Instance.type) + switch (HeaderSettings.headerType.value) { case HeaderType.Dotted: return GetSimpleTitle('-', title); case HeaderType.Custom: - return GetSimpleTitle(HeaderSettings.Instance.customPrefix, title); + return GetSimpleTitle(HeaderSettings.customPrefix, title); } return GetSimpleTitle('━', title); } @@ -91,7 +97,6 @@ public static void UpdateHeader(Header header, string title = null, bool markAsD public override void OnInspectorGUI() { - var settings = HeaderSettings.GetOrCreateSettings(); var typeProperty = serializedObject.FindProperty("type"); var header = target as Header; @@ -110,9 +115,9 @@ public override void OnInspectorGUI() } //Sync current header with settings - if ((HeaderType)typeProperty.enumValueIndex != settings.type) + if ((HeaderType)typeProperty.enumValueIndex != HeaderSettings.headerType.value) { - typeProperty.enumValueIndex = (int)settings.type; + typeProperty.enumValueIndex = (int)HeaderSettings.headerType.value; UpdateHeader(header, null, false); } diff --git a/Editor/HeaderSettings.cs b/Editor/HeaderSettings.cs index 15a6859..c0fa8e0 100644 --- a/Editor/HeaderSettings.cs +++ b/Editor/HeaderSettings.cs @@ -1,117 +1,78 @@ -using System; -using System.Collections.Generic; using UnityEditor; +using UnityEditor.SettingsManagement; using UnityEngine; namespace BK.HierarchyHeader.Editor { - public class HeaderSettings : ScriptableObject + public static class HeaderSettings { - [Range(10, 60)] - public int maxLength = 30; + [UserSetting] public static UserSetting maxLength = new UserSetting(Instance, "general.maxLength", 30); + [UserSetting] public static UserSetting minPrefixLength = new UserSetting(Instance, "general.minPrefixLength", 10); + [UserSetting] public static UserSetting headerType = new UserSetting(Instance, "general.headerType", HeaderType.Default); + [UserSetting] public static UserSetting customPrefix = new UserSetting(Instance, "general.customPrefix", null); + [UserSetting] public static UserSetting alignment = new UserSetting(Instance, "general.alignment", HeaderAlignment.Center); - [Range(0, 10)] - public int minPrefixLength = 2; - public HeaderType type; - public char customPrefix; - public HeaderAlignment alignment; - - private static HeaderSettings current; - - public static HeaderSettings Instance { get => GetOrCreateSettings(); } - - public static HeaderSettings GetOrCreateSettings() + [UserSettingBlock("General")] + static void CustomSettingsGUI(string searchContext) { - if (current != null) return current; - - //Locate our header settings - var ids = AssetDatabase.FindAssets("t:HeaderSettings"); - if (ids.Length > 0) - current = AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(ids[0])); - - if (current == null) + using (var scope = new EditorGUI.ChangeCheckScope()) { - var path = EditorUtility.SaveFilePanelInProject("Save HeaderSettings as...", "HierarchyHeaderSettings", "asset", ""); - current = ScriptableObject.CreateInstance(); - current.type = HeaderType.Default; - current.alignment = HeaderAlignment.Center; - AssetDatabase.CreateAsset(current, path); - AssetDatabase.SaveAssets(); - } - return current; - } + maxLength.value = (int)SettingsGUILayout.SettingsSlider("Max Length", maxLength, 10, 60, searchContext); - internal static SerializedObject GetSerializedSettings() - { - return new SerializedObject(GetOrCreateSettings()); - } - } - - static class SettingsRegester - { - private static SerializedObject settings; + headerType.value = (HeaderType)EditorGUILayout.EnumPopup("Header Type", headerType.value); + SettingsGUILayout.DoResetContextMenuForLastRect(headerType); - [SettingsProvider] - public static SettingsProvider CreateMyCustomSettingsProvider() - { - var provider = new SettingsProvider("Project/BK/Hierarchy Header", SettingsScope.Project) - { - label = "Hierarchy Header", - activateHandler = (_, element) => + if (headerType.value == HeaderType.Custom) { - Undo.undoRedoPerformed += HeaderEditor.UpdateAllHeader; - settings = HeaderSettings.GetSerializedSettings(); - }, - deactivateHandler = () => - { - Undo.undoRedoPerformed -= HeaderEditor.UpdateAllHeader; - }, - guiHandler = (searchContext) => - { - settings.Update(); - - GUILayout.Space(8); - - EditorGUILayout.BeginHorizontal(); - GUILayout.Space(12); + string v = SettingsGUILayout.SettingsTextField("Custom Prefix", customPrefix, searchContext); + if (v?.Length <= 1) + customPrefix.value = v; + } - EditorGUI.BeginChangeCheck(); + alignment.value = (HeaderAlignment)EditorGUILayout.EnumPopup("Header Alignment", alignment.value); + SettingsGUILayout.DoResetContextMenuForLastRect(alignment); - EditorGUILayout.BeginVertical(); - EditorGUILayout.PropertyField(settings.FindProperty("maxLength")); + if (alignment.value == HeaderAlignment.Start || alignment.value == HeaderAlignment.End) + { + minPrefixLength.value = (int)SettingsGUILayout.SettingsSlider("Min Prefix Length", minPrefixLength, 0, 10, searchContext); + } - var type = settings.FindProperty("type"); - EditorGUILayout.PropertyField(type); + if (scope.changed) + { + Instance.Save(); + HeaderEditor.UpdateAllHeader(); + } - if (type.enumValueIndex == 2) - { - var customPrefix = settings.FindProperty("customPrefix"); - EditorGUILayout.PropertyField(customPrefix); - } + EditorGUILayout.Space(); + if (GUILayout.Button("Update Headers")) + { + HeaderEditor.UpdateAllHeader(); + } + } + } - EditorGUILayout.PropertyField(settings.FindProperty("alignment")); - var alignment = settings.FindProperty("alignment"); - if (alignment.enumValueIndex == 0 || alignment.enumValueIndex == 2) - { - EditorGUILayout.PropertyField(settings.FindProperty("minPrefixLength")); - } - EditorGUILayout.EndVertical(); + internal const string k_PackageName = "com.bennykok.hierarchy-header"; + internal const string k_PreferencesPath = "Project/BK/Hierarchy Header"; - GUILayout.Space(12); - EditorGUILayout.EndHorizontal(); + static Settings s_Instance; - settings.ApplyModifiedProperties(); + internal static Settings Instance + { + get + { + if (s_Instance == null) + s_Instance = new Settings(k_PackageName); - if (EditorGUI.EndChangeCheck()) - { - HeaderEditor.UpdateAllHeader(); - } - }, + return s_Instance; + } + } - // Populate the search keywords to enable smart search filtering and label highlighting: - keywords = new HashSet(new[] { "Header" }) - }; + [SettingsProvider] + static SettingsProvider CreateSettingsProvider() + { + var provider = new UserSettingsProvider(k_PreferencesPath, Instance, + new[] { typeof(HeaderSettings).Assembly }, SettingsScope.Project); return provider; } diff --git a/Editor/com.bennykok.hierarchy-header.editor.asmdef b/Editor/com.bennykok.hierarchy-header.editor.asmdef index 5481899..ac4d006 100644 --- a/Editor/com.bennykok.hierarchy-header.editor.asmdef +++ b/Editor/com.bennykok.hierarchy-header.editor.asmdef @@ -1,7 +1,8 @@ { "name": "com.bennykok.hierarchy-header.editor", "references": [ - "GUID:c6e066e332a5d9748bc1ff41b6174db5" + "GUID:c6e066e332a5d9748bc1ff41b6174db5", + "GUID:49818357e697641afb75d2f8acaf1861" ], "includePlatforms": [ "Editor" diff --git a/package.json b/package.json index 7d12bd0..5aa32eb 100644 --- a/package.json +++ b/package.json @@ -2,5 +2,8 @@ "name": "com.bennykok.hierarchy-header", "displayName": "Hierarchy Header", "version": "1.0.2", - "description": "Text based simple hierarchy header, no fancy magic, just works" + "description": "Text based simple hierarchy header, no fancy magic, just works", + "dependencies": { + "com.unity.settings-manager": "1.0.3" + } } \ No newline at end of file