Skip to content

Commit

Permalink
Update docs; fix displaying hierarchy property labels after full-rect…
Browse files Browse the repository at this point in the history
… labels; update default settings; minor refactor changes
  • Loading branch information
arimger committed Aug 18, 2024
1 parent 491a1ed commit 9dda253
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 41 deletions.
6 changes: 5 additions & 1 deletion Assets/Editor Toolbox/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
## 0.12.13 [WIP]
## 0.12.13 [19.08.2024]

### Changed:
- Hierarchy: Added Tree List renderer, which improves visual identification of parent and child gameobjects
- Fix SceneView settings change events firing when they shouldn't
- Fix issue when trying to find private fields/properties from parent classes (e.g. while using conditional attributes)

### Added:
- DisableInEditModeAttribute

## 0.12.12 [17.06.2024]

Expand Down
69 changes: 40 additions & 29 deletions Assets/Editor Toolbox/Editor/Hierarchy/HierarchyPropertyLabel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Toolbox.Editor.Hierarchy
{
//TODO: refactor
//TODO: refactor: replace labels with drawers (similar approach to the Inspector), possibility to define drawers and implement them using a dedicated base class

/// <summary>
/// Base class for all custom, Hierarchy-related labels based on targeted <see cref="GameObject"/>.
Expand All @@ -16,16 +16,6 @@ public abstract class HierarchyPropertyLabel
{
protected GameObject target;

/// <summary>
/// Does this label draw over the whole item?
/// </summary>
public virtual bool UsesWholeItemRect { get; } = false;

/// <summary>
/// Should this label draw for headers too?
/// </summary>
public virtual bool DrawForHeaders { get; } = false;

public virtual bool Prepare(GameObject target, Rect availableRect)
{
return this.target = target;
Expand All @@ -52,7 +42,6 @@ public virtual float GetWidth()

public abstract void OnGui(Rect rect);


/// <summary>
/// Returns built-in label class associated to provided <see cref="HierarchyItemDataType"/>.
/// </summary>
Expand All @@ -77,31 +66,44 @@ public static HierarchyPropertyLabel GetPropertyLabel(HierarchyItemDataType data
return null;
}

/// <summary>
/// Does this label draw over the whole item?
/// </summary>
public virtual bool UsesWholeItemRect { get; } = false;

/// <summary>
/// Should this label draw for headers too?
/// </summary>
public virtual bool DrawForHeaders { get; } = false;

#region Classes: Internal

private class HierarchyIconLabel : HierarchyPropertyLabel
{
public override void OnGui(Rect rect)
{
var content = EditorGuiUtility.GetObjectContent(target, typeof(GameObject));
if (content.image)
if (content.image == null)
{
GUI.Label(rect, content.image);
return;
}

GUI.Label(rect, content.image);
}
}

private class HierarchyToggleLabel : HierarchyPropertyLabel
{
private readonly GUIContent label = new GUIContent(string.Empty, "Enable/disable GameObject");

public override void OnGui(Rect rect)
{
var content = new GUIContent(string.Empty, "Enable/disable GameObject");
//NOTE: using EditorGUI.Toggle will cause bug and deselect all hierarchy toggles when you will pick a multi-selected property in the Inspector
var result = GUI.Toggle(new Rect(rect.x + EditorGUIUtility.standardVerticalSpacing,
rect.y,
rect.width,
rect.height),
target.activeSelf, content);
target.activeSelf, label);

if (rect.Contains(Event.current.mousePosition))
{
Expand All @@ -116,14 +118,16 @@ public override void OnGui(Rect rect)

private class HierarchyTagLabel : HierarchyPropertyLabel
{
private const string untaggedTag = "Untagged";

public override float GetWidth()
{
return Style.maxWidth;
}

public override void OnGui(Rect rect)
{
var content = new GUIContent(target.CompareTag("Untagged") ? string.Empty : target.tag, target.tag);
var content = new GUIContent(target.CompareTag(untaggedTag) ? string.Empty : target.tag, target.tag);
EditorGUI.LabelField(rect, content, Style.defaultAlignTextStyle);
}
}
Expand Down Expand Up @@ -165,18 +169,19 @@ private class HierarchyScriptLabel : HierarchyPropertyLabel
/// </summary>
private List<Component> cachedComponents;


private void CacheComponents(GameObject target)
{
var components = target.GetComponents<Component>();
cachedComponents = new List<Component>(components.Length);
//cache only valid (non-null) components
foreach (var component in components)
{
if (component)
if (component == null)
{
cachedComponents.Add(component);
continue;
}

cachedComponents.Add(component);
}
}

Expand Down Expand Up @@ -212,7 +217,6 @@ private GUIContent GetContent(Component component)
return content;
}


public override bool Prepare(GameObject target, Rect availableRect)
{
var isValid = base.Prepare(target, availableRect);
Expand Down Expand Up @@ -302,27 +306,27 @@ private class HierarchyTreeLinesLabel : HierarchyPropertyLabel, IDisposable
private const float columnSize = 14.0f;

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

private int itemRenderCount = 0;

public HierarchyTreeLinesLabel()
{
EditorApplication.update += ResetItemRenderCount;
EditorApplication.update += ResetItemRenderCount;
}

public void Dispose()
{
EditorApplication.update -= ResetItemRenderCount;
}

public override sealed void OnGui(Rect rect)
public sealed override void OnGui(Rect rect)
{
if (Event.current.type != EventType.Repaint)
{
return;
}

var levels = (int)((rect.x + firstElementXOffset) / columnSize);

if (levels <= 0)
{
return;
Expand Down Expand Up @@ -354,18 +358,18 @@ public override sealed void OnGui(Rect rect)

x--;

Transform transformBuffer = targetTransform;
var transformBuffer = targetTransform;
for (; x >= startIndex; x--)
{
levelRenderers[x].Initialize(transformBuffer);
transformBuffer = transformBuffer.parent;
}
}

Color colorCache = GUI.color;
var colorCache = GUI.color;
GUI.color = Color.gray;

int i = 0;
var i = 0;
for (; i < (levels - 1); i++)
{
levelRenderers[i].OnGUI(rect, target, siblingIndex, false);
Expand All @@ -382,9 +386,9 @@ private void ResetItemRenderCount()
itemRenderCount = 0;
}

public override sealed bool UsesWholeItemRect => true;
public sealed override bool UsesWholeItemRect => true;

public override sealed bool DrawForHeaders => true;
public sealed override bool DrawForHeaders => true;

private bool IsFirstRenderedElement => itemRenderCount == 0;

Expand Down Expand Up @@ -463,6 +467,8 @@ protected static class Style
internal static readonly GUIContent elementCross;
internal static readonly GUIContent elementPass;

internal static readonly Color characterColor;

static Style()
{
elementLast = new GUIContent("└");
Expand Down Expand Up @@ -501,6 +507,11 @@ static Style()
{
fontSize = 18,
};

if (!EditorGUIUtility.isProSkin)
{
centreAlignTreeLineStyle.normal.textColor = Color.white;
}
}
}
}
Expand Down
16 changes: 7 additions & 9 deletions Assets/Editor Toolbox/Editor/ToolboxEditorHierarchy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,11 @@ static ToolboxEditorHierarchy()
EditorApplication.hierarchyWindowItemOnGUI += OnItemCallback;
}


/// <summary>
/// All valid and prepared label drawers for each item.
/// </summary>
private static readonly List<HierarchyPropertyLabel> propertyLabels = new List<HierarchyPropertyLabel>();


/// <summary>
/// Tries to display item label in the Hierarchy Window.
/// </summary>
Expand Down Expand Up @@ -72,7 +70,6 @@ private static void OnItemCallback(int instanceId, Rect rect)
}
}


/// <summary>
/// Creates optional information about selected objects using the internal <see cref="Selection"/> class.
/// </summary>
Expand Down Expand Up @@ -159,9 +156,13 @@ private static void DrawDefaultItemLabel(Rect rect, GameObject gameObject, strin
{
//each property label has to be created in validated (adjusted) area
//depending on previously occupied rect we have to adjust current rect
contentRect = AppendPropertyLabel(propertyLabels[i], gameObject, availableRect);
availableRect.xMax -= contentRect.width;

var propertyLabel = propertyLabels[i];
contentRect = AppendPropertyLabel(propertyLabel, gameObject, availableRect);
if (!propertyLabel.UsesWholeItemRect)
{
availableRect.xMax -= contentRect.width;
}

EditorGUI.DrawRect(new Rect(contentRect.xMin, rect.y, Style.lineWidth, rect.height), Style.lineColor);
}

Expand All @@ -181,7 +182,6 @@ private static void DrawDefaultItemLabel(Rect rect, GameObject gameObject, strin
}
}


private static Rect AppendPropertyLabel(HierarchyPropertyLabel propertyLabel, GameObject target, Rect availableRect)
{
if (propertyLabel.UsesWholeItemRect)
Expand Down Expand Up @@ -315,7 +315,6 @@ internal static void RemoveAllowedHierarchyContentCallbacks()

internal static void RepaintHierarchyOverlay() => EditorApplication.RepaintHierarchyWindow();


/// <summary>
/// Determines if <see cref="ToolboxEditorHierarchy"/> can create an additional overlay on the Hierarchy Window.
/// </summary>
Expand All @@ -325,7 +324,6 @@ internal static void RemoveAllowedHierarchyContentCallbacks()
internal static bool ShowSelectionsCount { get; set; } = true;
internal static bool DrawSeparationLines { get; set; } = true;


private static class Style
{
internal static readonly float lineWidth = 1.0f;
Expand Down
2 changes: 1 addition & 1 deletion Assets/Editor Toolbox/EditorSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ MonoBehaviour:
useToolboxHierarchy: 1
drawHorizontalLines: 1
showSelectionsCount: 1
rowDataTypes: 0000000001000000
rowDataTypes: 000000000100000005000000
useToolboxFolders: 1
largeIconScale: 0.8
smallIconScale: 0.7
Expand Down
2 changes: 2 additions & 0 deletions Assets/Editor Toolbox/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,7 @@ Each row can contain:
- Tag
- Toggle to enable/disable GameObject
- Icon
- Tree Lines

![inspector](https://github.com/arimger/Unity-Editor-Toolbox/blob/develop/Docs/hierarchy.png)
Expand Down Expand Up @@ -928,4 +929,5 @@ Assets/Create/Editor Toolbox/ScriptableObject Creation Wizard

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

![inspector](https://github.com/arimger/Unity-Editor-Toolbox/blob/develop/Docs/sceneview.png)
![inspector](https://github.com/arimger/Unity-Editor-Toolbox/blob/develop/Docs/sceneview.png)
2 changes: 1 addition & 1 deletion Assets/Editor Toolbox/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.browar.editor-toolbox",
"displayName": "Editor Toolbox",
"version": "0.12.12",
"version": "0.12.13",
"unity": "2018.1",
"description": "Tools, custom attributes, drawers, hierarchy overlay, and other extensions for the Unity Editor.",
"keywords": [
Expand Down
Binary file modified Docs/hierarchy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,7 @@ Each row can contain:
- Toggle to enable/disable GameObject
- Icon
- Tree Lines

![inspector](https://github.com/arimger/Unity-Editor-Toolbox/blob/develop/Docs/hierarchy.png)
### Project <a name="project"></a>
Expand Down

0 comments on commit 9dda253

Please sign in to comment.