Skip to content

Commit

Permalink
feat: using Settings Manager for editor settings
Browse files Browse the repository at this point in the history
BREAKING CHANGE: old settings will be lost, please remove the old HeaderSettings scriptableobject
  • Loading branch information
BennyKok committed Feb 1, 2021
1 parent 2cad0b6 commit 71cfa9b
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 105 deletions.
27 changes: 16 additions & 11 deletions Editor/HeaderEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Header>();
foreach (var header in allHeader)
{
Expand All @@ -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:
Expand All @@ -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);
}
Expand All @@ -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;
Expand All @@ -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);
}
Expand Down
145 changes: 53 additions & 92 deletions Editor/HeaderSettings.cs
Original file line number Diff line number Diff line change
@@ -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<int> maxLength = new UserSetting<int>(Instance, "general.maxLength", 30);
[UserSetting] public static UserSetting<int> minPrefixLength = new UserSetting<int>(Instance, "general.minPrefixLength", 10);
[UserSetting] public static UserSetting<HeaderType> headerType = new UserSetting<HeaderType>(Instance, "general.headerType", HeaderType.Default);
[UserSetting] public static UserSetting<string> customPrefix = new UserSetting<string>(Instance, "general.customPrefix", null);
[UserSetting] public static UserSetting<HeaderAlignment> alignment = new UserSetting<HeaderAlignment>(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<HeaderSettings>(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<HeaderSettings>();
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<string>(new[] { "Header" })
};
[SettingsProvider]
static SettingsProvider CreateSettingsProvider()
{
var provider = new UserSettingsProvider(k_PreferencesPath, Instance,
new[] { typeof(HeaderSettings).Assembly }, SettingsScope.Project);

return provider;
}
Expand Down
3 changes: 2 additions & 1 deletion Editor/com.bennykok.hierarchy-header.editor.asmdef
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"name": "com.bennykok.hierarchy-header.editor",
"references": [
"GUID:c6e066e332a5d9748bc1ff41b6174db5"
"GUID:c6e066e332a5d9748bc1ff41b6174db5",
"GUID:49818357e697641afb75d2f8acaf1861"
],
"includePlatforms": [
"Editor"
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}

0 comments on commit 71cfa9b

Please sign in to comment.