Skip to content

Commit

Permalink
v1.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
BennyKok committed Sep 9, 2020
1 parent 2c4c4f6 commit 2cad0b6
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 94 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
127 changes: 37 additions & 90 deletions Editor/HeaderEditor.cs
Original file line number Diff line number Diff line change
@@ -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<Renderer>(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<Renderer>(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<Header>();

//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;
Expand All @@ -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;
Expand All @@ -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()
{
Expand All @@ -157,16 +103,18 @@ 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
if ((HeaderType)typeProperty.enumValueIndex != settings.type)
{
typeProperty.enumValueIndex = (int)settings.type;

UpdateHeader(header);
UpdateHeader(header, null, false);
}

EditorGUILayout.Space();
Expand All @@ -177,8 +125,7 @@ public override void OnInspectorGUI()
}
if (GUILayout.Button("Refresh"))
{
UpdateHeader(header);
HeaderUtils.UpdateAllHeader();
UpdateAllHeader();
}
if (GUILayout.Button("Create Empty"))
{
Expand Down
6 changes: 3 additions & 3 deletions Editor/HeaderSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
{
Expand Down Expand Up @@ -105,7 +105,7 @@ public static SettingsProvider CreateMyCustomSettingsProvider()

if (EditorGUI.EndChangeCheck())
{
HeaderUtils.UpdateAllHeader();
HeaderEditor.UpdateAllHeader();
}
},

Expand Down
75 changes: 75 additions & 0 deletions Editor/HeaderUtils.cs
Original file line number Diff line number Diff line change
@@ -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<Renderer>(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<Renderer>(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<Header>();

//Hide the transform
header.transform.hideFlags = HideFlags.NotEditable | HideFlags.HideInInspector;

//Update the header
HeaderEditor.UpdateHeader(header.GetComponent<Header>());

//Register undo
Undo.RegisterCreatedObjectUndo(header, "Create Header");

//Select the created header
Selection.activeGameObject = header;
}
}
}
11 changes: 11 additions & 0 deletions Editor/HeaderUtils.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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"
}

0 comments on commit 2cad0b6

Please sign in to comment.