Skip to content

Commit 71168a5

Browse files
committed
Replace GUIContent-based tree lines with rect rextures; update default settings
1 parent 4d53280 commit 71168a5

File tree

5 files changed

+136
-41
lines changed

5 files changed

+136
-41
lines changed

Assets/Editor Toolbox/Editor/Hierarchy/HierarchyPropertyLabel.cs

Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,11 @@ public override void OnGui(Rect rect)
302302

303303
private class HierarchyTreeLinesLabel : HierarchyPropertyLabel, IDisposable
304304
{
305+
//TODO: properties to expose when switching to SerializedReference-based implementation:
306+
// - color
307+
// - isDashed
308+
// - tickness
309+
305310
private const float firstElementWidthOffset = 4.0f;
306311

307312
#if UNITY_2019_1_OR_NEWER
@@ -312,6 +317,7 @@ private class HierarchyTreeLinesLabel : HierarchyPropertyLabel, IDisposable
312317
private const float startXPosition = 0.0f;
313318
#endif
314319
private const float columnSize = 14.0f;
320+
private const bool isDashed = true;
315321

316322
private readonly List<TreeLineLevelRenderer> levelRenderers = new List<TreeLineLevelRenderer>();
317323

@@ -348,15 +354,14 @@ public sealed override void OnGui(Rect rect)
348354
itemRenderCount++;
349355

350356
rect.x = startXPosition;
351-
//we need 2x column size for full-line cases when object has no children and there is no foldout
352-
rect.width = 2 * columnSize + firstElementWidthOffset;
357+
rect.width = columnSize + firstElementWidthOffset;
353358

354359
var targetTransform = target.transform;
355360
var siblingIndex = targetTransform.GetSiblingIndex();
356361

357362
if (levels > levelRenderers.Count)
358363
{
359-
//initialize missing tree line level render
364+
//initialize missing tree line level renderer
360365
var startIndex = levelRenderers.Count;
361366
int x;
362367
for (x = startIndex; x < levels; x++)
@@ -413,34 +418,33 @@ public void Initialize(Transform transform)
413418

414419
public void OnGUI(Rect rect, GameObject target, int siblingIndex, bool isCurrentLevel)
415420
{
416-
//NOTE: currently we are using labels and predefined chars to display tree lines, this is not optimal solution
417-
// since we can't really control width, tickiness and other potential useful properties. Using few chars allow us
418-
// to display dashed lines very easily but replacing it with standard line would a bit harder.
419-
// For now this is ok solution but probably should be replaced with drawing lines using the EditorGUI.DrawRect API,
420-
// in the same way we draw lines in the Inspector
421+
var offset = new Vector2()
422+
{
423+
x = EditorGUIUtility.standardVerticalSpacing
424+
};
421425

422426
if (isCurrentLevel)
423427
{
424-
var hasChildren = target.transform.childCount > 0;
425-
GUIContent label;
428+
//NOTE: extend if there is no foldout button
429+
var isLineExtended = target.transform.childCount == 0;
430+
var horizontalSizeOffset = isLineExtended ? rect.width / 2 : 0.0f;
426431
if (GetParentChildCount(target) == (siblingIndex + 1))
427432
{
428433
renderedLastLevelGameobject = true;
429-
label = hasChildren ? Style.treeElementLastHalf : Style.treeElementLast;
434+
HierarchyTreeUtility.DrawCornerLine(rect, isDashed, Style.treeLineTickness, Style.treeLineColor, offset, horizontalSizeOffset);
430435
}
431436
else
432437
{
433438
renderedLastLevelGameobject = false;
434-
label = hasChildren ? Style.treeElementCrossHalf : Style.treeElementCross;
439+
HierarchyTreeUtility.DrawCrossLine(rect, isDashed, Style.treeLineTickness, Style.treeLineColor, offset, horizontalSizeOffset);
435440
}
436441

437-
EditorGUI.LabelField(rect, label, Style.treeElementStyle);
438442
return;
439443
}
440444

441445
if (!renderedLastLevelGameobject)
442446
{
443-
EditorGUI.LabelField(rect, Style.treeElementPass, Style.treeElementStyle);
447+
HierarchyTreeUtility.DrawPassingLine(rect, isDashed, Style.treeLineTickness, Style.treeLineColor, offset);
444448
}
445449
}
446450

@@ -462,34 +466,22 @@ private int GetParentChildCount(Transform transform)
462466
}
463467
}
464468
}
465-
#endregion
469+
#endregion
466470

467471
protected static class Style
468472
{
469473
internal static readonly float minWidth = 17.0f;
470474
internal static readonly float maxWidth = 60.0f;
475+
internal static readonly float treeLineTickness = 0.75f;
471476

472477
internal static readonly GUIStyle defaultAlignTextStyle;
473478
internal static readonly GUIStyle centreAlignTextStyle;
474479
internal static readonly GUIStyle rightAlignTextStyle;
475-
internal static readonly GUIStyle treeElementStyle;
476-
477-
internal static readonly GUIContent treeElementLast;
478-
internal static readonly GUIContent treeElementLastHalf;
479-
internal static readonly GUIContent treeElementCross;
480-
internal static readonly GUIContent treeElementCrossHalf;
481-
internal static readonly GUIContent treeElementPass;
482480

483-
internal static readonly Color characterColor;
481+
internal static readonly Color treeLineColor = Color.white;
484482

485483
static Style()
486484
{
487-
treeElementLast = new GUIContent("└--");
488-
treeElementLastHalf = new GUIContent("└-");
489-
treeElementCross = new GUIContent("├--");
490-
treeElementCrossHalf = new GUIContent("├-");
491-
treeElementPass = new GUIContent("│");
492-
493485
defaultAlignTextStyle = new GUIStyle(EditorStyles.miniLabel)
494486
{
495487
#if UNITY_2019_3_OR_NEWER
@@ -518,16 +510,6 @@ static Style()
518510
alignment = TextAnchor.UpperRight
519511
#endif
520512
};
521-
treeElementStyle = new GUIStyle(EditorStyles.label)
522-
{
523-
padding = new RectOffset(4, 0, 0, 0),
524-
fontSize = 12,
525-
};
526-
527-
if (!EditorGUIUtility.isProSkin)
528-
{
529-
treeElementStyle.normal.textColor = Color.white;
530-
}
531513
}
532514
}
533515
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
using UnityEditor;
2+
using UnityEngine;
3+
4+
namespace Toolbox.Editor.Hierarchy
5+
{
6+
internal static class HierarchyTreeUtility
7+
{
8+
private const float dashLength = 4.0f;
9+
private const float spaceLength = 1.0f;
10+
11+
private static Rect GetLineRect(Rect rect, float thickness, float padding, bool horizontal)
12+
{
13+
return horizontal
14+
? new Rect(rect.x, rect.y + padding / 2, rect.width, thickness)
15+
: new Rect(rect.x + padding / 2, rect.y, thickness, rect.height);
16+
}
17+
18+
private static void DrawVerticalLine(Rect rect, bool isDashed, float tickness, Color color)
19+
{
20+
DrawVerticalLine(rect, isDashed, tickness, color, 0.0f);
21+
}
22+
23+
private static void DrawVerticalLine(Rect rect, bool isDashed, float tickness, Color color, float paddingOffset)
24+
{
25+
rect = GetLineRect(rect, tickness, rect.width - paddingOffset, false);
26+
if (!isDashed)
27+
{
28+
EditorGUI.DrawRect(rect, color);
29+
return;
30+
}
31+
32+
var dashesCount = rect.height / (dashLength + spaceLength);
33+
var maxY = rect.yMax;
34+
35+
rect.yMax = rect.yMin + dashLength;
36+
for (var i = 0; i < dashesCount; i++)
37+
{
38+
EditorGUI.DrawRect(rect, color);
39+
rect.y += dashLength + spaceLength;
40+
if (rect.yMax > maxY)
41+
{
42+
rect.yMax = maxY;
43+
}
44+
}
45+
}
46+
47+
private static void DrawHorizontalLine(Rect rect, bool isDashed, float tickness, Color color)
48+
{
49+
DrawHorizontalLine(rect, isDashed, tickness, color, 0.0f);
50+
}
51+
52+
private static void DrawHorizontalLine(Rect rect, bool isDashed, float tickness, Color color, float paddingOffset)
53+
{
54+
rect = GetLineRect(rect, tickness, rect.height - paddingOffset, true);
55+
if (!isDashed)
56+
{
57+
EditorGUI.DrawRect(rect, color);
58+
return;
59+
}
60+
61+
var dashesCount = rect.width / (dashLength + spaceLength);
62+
var maxX = rect.xMax;
63+
64+
rect.xMax = rect.xMin + dashLength;
65+
for (var i = 0; i < dashesCount; i++)
66+
{
67+
EditorGUI.DrawRect(rect, color);
68+
rect.x += dashLength + spaceLength;
69+
if (rect.xMax > maxX)
70+
{
71+
rect.xMax = maxX;
72+
}
73+
}
74+
}
75+
76+
public static void DrawPassingLine(Rect rect, bool isDashed, float tickness, Color color, Vector2 paddingOffset)
77+
{
78+
DrawVerticalLine(rect, isDashed, tickness, color, paddingOffset.x);
79+
}
80+
81+
public static void DrawCornerLine(Rect rect, bool isDashed, float tickness, Color color, Vector2 paddingOffset, float horizontalSizeOffset)
82+
{
83+
//NOTE: -1 as a offset for halfs in corners
84+
var verticalRect = rect;
85+
verticalRect.yMax -= verticalRect.height / 2 - 1;
86+
DrawVerticalLine(verticalRect, isDashed, tickness, color, paddingOffset.x);
87+
88+
var horizontalRect = rect;
89+
horizontalRect.xMin += horizontalRect.width / 2;
90+
horizontalRect.xMax += horizontalSizeOffset;
91+
DrawHorizontalLine(horizontalRect, isDashed, tickness, color, paddingOffset.y);
92+
}
93+
94+
public static void DrawCrossLine(Rect rect, bool isDashed, float tickness, Color color, Vector2 paddingOffset, float horizontalSizeOffset)
95+
{
96+
DrawVerticalLine(rect, isDashed, tickness, color, paddingOffset.x);
97+
98+
rect.xMin += rect.width / 2;
99+
rect.xMax += horizontalSizeOffset;
100+
DrawHorizontalLine(rect, isDashed, tickness, color, paddingOffset.y);
101+
}
102+
}
103+
}

Assets/Editor Toolbox/Editor/Hierarchy/HierarchyTreeUtility.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Editor Toolbox/EditorSettings.asset

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ MonoBehaviour:
1515
useToolboxHierarchy: 1
1616
drawHorizontalLines: 1
1717
showSelectionsCount: 1
18-
rowDataTypes: 050000000000000001000000020000000300000004000000
18+
rowDataTypes: 05000000000000000100000004000000
1919
useToolboxFolders: 1
2020
largeIconScale: 0.8
2121
smallIconScale: 0.7

Assets/Editor Toolbox/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -929,5 +929,4 @@ Assets/Create/Editor Toolbox/ScriptableObject Creation Wizard
929929

930930
Select a specific object that is under the cursor (default key: tab).
931931

932-
![inspector](https://github.com/arimger/Unity-Editor-Toolbox/blob/develop/Docs/sceneview.png)
933932
![inspector](https://github.com/arimger/Unity-Editor-Toolbox/blob/develop/Docs/sceneview.png)

0 commit comments

Comments
 (0)