diff --git a/CHANGELOG.md b/CHANGELOG.md index b6a4347..0ea6ddf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this package will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). +## [1.0.2] - 2020-09-08 + +- bug fixes. + ## [1.0.1] - 2020-09-08 ### Add more styling options, alignment, length etc. diff --git a/Editor/HeaderEditor.cs b/Editor/HeaderEditor.cs index b4b4cae..f74c77c 100644 --- a/Editor/HeaderEditor.cs +++ b/Editor/HeaderEditor.cs @@ -1,74 +1,12 @@ -using UnityEditor; +using System.Text; +using UnityEditor; using UnityEngine; namespace BK.HierarchyHeader.Editor { - public static class HeaderUtils + [CustomEditor(typeof(Header))] + public class HeaderEditor : UnityEditor.Editor { - [MenuItem("GameObject/Group Selected %g")] - private static void GroupSelected() - { - if (!Selection.activeTransform) return; - var go = new GameObject(Selection.activeTransform.name + " Group"); - go.transform.SetSiblingIndex(Selection.activeTransform.GetSiblingIndex()); - - go.transform.position = FindCenterPoint(Selection.transforms); - - go.transform.SetParent(Selection.activeTransform.parent); - - Undo.RegisterCreatedObjectUndo(go, "Group Selected"); - - foreach (var transform in Selection.transforms) - { - Undo.SetTransformParent(transform, go.transform, "Group Selected"); - } - - Selection.activeGameObject = go; - } - - private static Vector3 FindCenterPoint(Transform[] objects) - { - if (objects.Length == 0) - return Vector3.zero; - - if (objects.Length == 1) - { - if (objects[0].TryGetComponent(out var ren)) - return ren.bounds.center; - else - return objects[0].transform.position; - } - - var bounds = new Bounds(objects[0].transform.position, Vector3.zero); - foreach (var transform in objects) - { - if (transform.TryGetComponent(out var ren) && ren.GetType() != typeof(ParticleSystemRenderer)) - bounds.Encapsulate(ren.bounds); - else - bounds.Encapsulate(transform.position); - } - return bounds.center; - } - - [MenuItem("GameObject/Create Header", false, 0)] - private static void CreateHeader() - { - var header = new GameObject(); - - //Mark as EditorOnly, so it will not included in final build - header.tag = "EditorOnly"; - header.AddComponent
(); - - //Hide the transform - header.transform.hideFlags = HideFlags.NotEditable | HideFlags.HideInInspector; - - //Register undo - Undo.RegisterCreatedObjectUndo(header, "Create Header"); - - //Select the created header - Selection.activeGameObject = header; - } - public static void UpdateAllHeader() { var targetType = HeaderSettings.Instance.type; @@ -79,18 +17,14 @@ public static void UpdateAllHeader() header.type = targetType; header.alignment = targetAlignment; - HeaderEditor.UpdateHeader(header); + HeaderEditor.UpdateHeader(header, null, true); } } - } - [CustomEditor(typeof(Header))] - public class HeaderEditor : UnityEditor.Editor - { - public static string GetSimpleTitle(char prefix, Header header) + public static string GetSimpleTitle(char prefix, string title) { var maxCharLength = HeaderSettings.Instance.maxLength; - var charLength = maxCharLength - header.title.Length; + var charLength = maxCharLength - title.Length; var leftSize = 0; var rightSize = 0; @@ -113,35 +47,47 @@ public static string GetSimpleTitle(char prefix, Header header) string left = leftSize > 0 ? new string(prefix, leftSize) : ""; string right = rightSize > 0 ? new string(prefix, rightSize) : ""; - return left + " " + header.title.ToUpper() + " " + right; + var builder = new StringBuilder(); + builder.Append(left); + builder.Append(" "); + builder.Append(title.ToUpper()); + builder.Append(" "); + builder.Append(right); + + return builder.ToString(); } - public static string GetFormattedTitle(Header header) + public static string GetFormattedTitle(string title) { switch (HeaderSettings.Instance.type) { case HeaderType.Dotted: - return GetSimpleTitle('-', header); + return GetSimpleTitle('-', title); case HeaderType.Custom: - return GetSimpleTitle(HeaderSettings.Instance.customPrefix, header); + return GetSimpleTitle(HeaderSettings.Instance.customPrefix, title); } - return GetSimpleTitle('━', header); + return GetSimpleTitle('━', title); } - public static void UpdateHeader(Header header) + public static void UpdateHeader(Header header, string title = null, bool markAsDirty = false) { - header.name = GetFormattedTitle(header); - EditorUtility.SetDirty(header); + var targetTitle = title == null ? header.title : title; + + header.name = GetFormattedTitle(targetTitle); + + if (markAsDirty) + EditorUtility.SetDirty(header); } private void OnEnable() => Undo.undoRedoPerformed += OnUndoRedo; private void OnDisable() => Undo.undoRedoPerformed -= OnUndoRedo; - public void OnUndoRedo() - { - UpdateHeader(target as Header); - } + public void OnUndoRedo() => UpdateHeader(target as Header, null, true); + + private bool titleChanged; + + private double lastChangedTime; public override void OnInspectorGUI() { @@ -157,8 +103,10 @@ public override void OnInspectorGUI() EditorGUILayout.PropertyField(titleProperty); if (EditorGUI.EndChangeCheck()) { - serializedObject.ApplyModifiedProperties(); - UpdateHeader(header); + UpdateHeader(header, titleProperty.stringValue, false); + + //Refresh the hierarchy to reflect the new name + EditorApplication.RepaintHierarchyWindow(); } //Sync current header with settings @@ -166,7 +114,7 @@ public override void OnInspectorGUI() { typeProperty.enumValueIndex = (int)settings.type; - UpdateHeader(header); + UpdateHeader(header, null, false); } EditorGUILayout.Space(); @@ -177,8 +125,7 @@ public override void OnInspectorGUI() } if (GUILayout.Button("Refresh")) { - UpdateHeader(header); - HeaderUtils.UpdateAllHeader(); + UpdateAllHeader(); } if (GUILayout.Button("Create Empty")) { diff --git a/Editor/HeaderSettings.cs b/Editor/HeaderSettings.cs index 61c7234..15a6859 100644 --- a/Editor/HeaderSettings.cs +++ b/Editor/HeaderSettings.cs @@ -60,12 +60,12 @@ public static SettingsProvider CreateMyCustomSettingsProvider() label = "Hierarchy Header", activateHandler = (_, element) => { - Undo.undoRedoPerformed += HeaderUtils.UpdateAllHeader; + Undo.undoRedoPerformed += HeaderEditor.UpdateAllHeader; settings = HeaderSettings.GetSerializedSettings(); }, deactivateHandler = () => { - Undo.undoRedoPerformed -= HeaderUtils.UpdateAllHeader; + Undo.undoRedoPerformed -= HeaderEditor.UpdateAllHeader; }, guiHandler = (searchContext) => { @@ -105,7 +105,7 @@ public static SettingsProvider CreateMyCustomSettingsProvider() if (EditorGUI.EndChangeCheck()) { - HeaderUtils.UpdateAllHeader(); + HeaderEditor.UpdateAllHeader(); } }, diff --git a/Editor/HeaderUtils.cs b/Editor/HeaderUtils.cs new file mode 100644 index 0000000..f71b0a3 --- /dev/null +++ b/Editor/HeaderUtils.cs @@ -0,0 +1,75 @@ +using UnityEditor; +using UnityEngine; + +namespace BK.HierarchyHeader.Editor +{ + public static class HeaderUtils + { + [MenuItem("GameObject/Group Selected %g")] + private static void GroupSelected() + { + if (!Selection.activeTransform) return; + var go = new GameObject(Selection.activeTransform.name + " Group"); + go.transform.SetSiblingIndex(Selection.activeTransform.GetSiblingIndex()); + + go.transform.position = FindCenterPoint(Selection.transforms); + + go.transform.SetParent(Selection.activeTransform.parent); + + Undo.RegisterCreatedObjectUndo(go, "Group Selected"); + + foreach (var transform in Selection.transforms) + { + Undo.SetTransformParent(transform, go.transform, "Group Selected"); + } + + Selection.activeGameObject = go; + } + + private static Vector3 FindCenterPoint(Transform[] objects) + { + if (objects.Length == 0) + return Vector3.zero; + + if (objects.Length == 1) + { + if (objects[0].TryGetComponent(out var ren)) + return ren.bounds.center; + else + return objects[0].transform.position; + } + + var bounds = new Bounds(objects[0].transform.position, Vector3.zero); + foreach (var transform in objects) + { + if (transform.TryGetComponent(out var ren) && ren.GetType() != typeof(ParticleSystemRenderer)) + bounds.Encapsulate(ren.bounds); + else + bounds.Encapsulate(transform.position); + } + return bounds.center; + } + + [MenuItem("GameObject/Create Header", false, 0)] + private static void CreateHeader() + { + var header = new GameObject(); + + //Mark as EditorOnly, so it will not included in final build + header.tag = "EditorOnly"; + header.AddComponent
(); + + //Hide the transform + header.transform.hideFlags = HideFlags.NotEditable | HideFlags.HideInInspector; + + //Update the header + HeaderEditor.UpdateHeader(header.GetComponent
()); + + //Register undo + Undo.RegisterCreatedObjectUndo(header, "Create Header"); + + //Select the created header + Selection.activeGameObject = header; + } + } +} \ No newline at end of file diff --git a/Editor/HeaderUtils.cs.meta b/Editor/HeaderUtils.cs.meta new file mode 100644 index 0000000..79ee4e3 --- /dev/null +++ b/Editor/HeaderUtils.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: b9e0d5581cf5459428fbaf3f69c80f4a +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/package.json b/package.json index d445f73..7d12bd0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "com.bennykok.hierarchy-header", "displayName": "Hierarchy Header", - "version": "1.0.1", + "version": "1.0.2", "description": "Text based simple hierarchy header, no fancy magic, just works" } \ No newline at end of file