Skip to content

Commit

Permalink
Add limit to visible components in the Hierarchy
Browse files Browse the repository at this point in the history
  • Loading branch information
arimger committed Aug 25, 2024
1 parent 6075ff7 commit 0b53887
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 6 deletions.
39 changes: 33 additions & 6 deletions Assets/Editor Toolbox/Editor/Hierarchy/HierarchyPropertyLabel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,14 @@ public override float GetWidth()

private class HierarchyScriptLabel : HierarchyPropertyLabel
{
//TODO: properties to expose when switching to SerializedReference-based implementation:
// - max components
// - how to render standard scripts without custom icons

private const int maxComponents = 5;

private static readonly GUIContent moreLabelContent = new GUIContent("...");

private static Texture componentIcon;
private static Texture transformIcon;
private static Texture warningIcon;
Expand Down Expand Up @@ -248,8 +256,15 @@ public override bool Prepare(GameObject target, Rect availableRect)
baseWidth = Style.minWidth;
components = target.GetComponents<Component>();
var componentsCount = components.Length;
//NOTE: ignore transform for >1 components
var componentsToDisplay = componentsCount - 1;
if (componentsToDisplay > maxComponents)
{
componentsToDisplay = maxComponents + 1;
}

summWidth = componentsCount > 1
? (componentsCount - 1) * baseWidth
? componentsToDisplay * baseWidth
: baseWidth;

isHighlighted = availableRect.Contains(Event.current.mousePosition);
Expand All @@ -275,20 +290,32 @@ public override void OnGui(Rect rect)
return;
}

rect.xMin -= baseWidth * (componentsCount - 2);
//NOTE: we are already in appropriate position for the first component, let's move it depending on the count
var componentsToDisplay = componentsCount - 1;
if (componentsToDisplay > maxComponents)
{
//NOTE: one additional place for "more"
componentsToDisplay = maxComponents + 1;
}

rect.xMin -= baseWidth * (componentsToDisplay - 1);

var iconRect = rect;
iconRect.xMin = rect.xMin;
iconRect.xMax = rect.xMin + baseWidth;

//draw all icons associated to cached components (except transform)
for (var i = 1; i < components.Length; i++)
//NOTE: draw all icons associated to cached components (except transform)
for (var i = 1; i < componentsCount; i++)
{
if (i > maxComponents)
{
GUI.Label(iconRect, moreLabelContent);
break;
}

var component = components[i];
var content = GetContent(component);
//draw icon for the current component
GUI.Label(iconRect, content);
//adjust rect for the next script icon
iconRect.x += baseWidth;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace Toolbox.Editor.Hierarchy
/// </summary>
internal static class HierarchyTreeUtility
{
//NOTE: consider caching pre-created texturess for dashed lines and use them to optimize each call
private const float dashLength = 4.0f;
private const float spaceLength = 1.0f;

Expand Down
73 changes: 73 additions & 0 deletions Assets/Examples/Scenes/SampleScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,12 @@ GameObject:
- component: {fileID: 580334320}
- component: {fileID: 580334319}
- component: {fileID: 580334318}
- component: {fileID: 580334325}
- component: {fileID: 580334317}
- component: {fileID: 580334324}
- component: {fileID: 580334323}
- component: {fileID: 580334322}
- component: {fileID: 580334321}
m_Layer: 0
m_Name: GameObject (8)
m_TagString: Untagged
Expand Down Expand Up @@ -552,6 +557,74 @@ Transform:
m_Father: {fileID: 0}
m_RootOrder: 22
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!65 &580334321
BoxCollider:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 580334316}
m_Material: {fileID: 0}
m_IsTrigger: 0
m_Enabled: 1
serializedVersion: 2
m_Size: {x: 1, y: 1, z: 1}
m_Center: {x: 0, y: 0, z: 0}
--- !u!65 &580334322
BoxCollider:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 580334316}
m_Material: {fileID: 0}
m_IsTrigger: 0
m_Enabled: 1
serializedVersion: 2
m_Size: {x: 1, y: 1, z: 1}
m_Center: {x: 0, y: 0, z: 0}
--- !u!65 &580334323
BoxCollider:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 580334316}
m_Material: {fileID: 0}
m_IsTrigger: 0
m_Enabled: 1
serializedVersion: 2
m_Size: {x: 1, y: 1, z: 1}
m_Center: {x: 0, y: 0, z: 0}
--- !u!65 &580334324
BoxCollider:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 580334316}
m_Material: {fileID: 0}
m_IsTrigger: 0
m_Enabled: 1
serializedVersion: 2
m_Size: {x: 1, y: 1, z: 1}
m_Center: {x: 0, y: 0, z: 0}
--- !u!54 &580334325
Rigidbody:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 580334316}
serializedVersion: 2
m_Mass: 1
m_Drag: 0
m_AngularDrag: 0.05
m_UseGravity: 1
m_IsKinematic: 0
m_Interpolate: 0
m_Constraints: 0
m_CollisionDetection: 0
--- !u!1 &661896457
GameObject:
m_ObjectHideFlags: 0
Expand Down

0 comments on commit 0b53887

Please sign in to comment.