Skip to content

Instantly share code, notes, and snippets.

@glitchersgames
Created August 14, 2020 09:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glitchersgames/f3f30c0566841d0b22d7bfbfc819ec46 to your computer and use it in GitHub Desktop.
Save glitchersgames/f3f30c0566841d0b22d7bfbfc819ec46 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
[InitializeOnLoad]
public class CustomHierarchyView
{
private static GUIContent toggledOnIcon;
private static GUIContent toggledOffIcon;
private static GUIStyle customButton;
private static GUIStyle customLabel;
static CustomHierarchyView()
{
EditorApplication.hierarchyWindowItemOnGUI += HierarchyWindowItemOnGUI;
}
~CustomHierarchyView()
{
EditorApplication.hierarchyWindowItemOnGUI -= HierarchyWindowItemOnGUI;
}
private static void Setup()
{
if (customButton != null)
{
return;
}
toggledOnIcon = EditorGUIUtility.IconContent("animationvisibilitytoggleon");
toggledOffIcon = EditorGUIUtility.IconContent("animationvisibilitytoggleoff");
customButton = new GUIStyle(GUI.skin.button);
customLabel = new GUIStyle(GUI.skin.label);
customButton.border.bottom = 0;
customButton.border.top = 0;
customButton.border.right = 0;
customButton.border.left = 0;
customButton.normal.background = null;
customButton.active.background = null;
customButton.hover.background = null;
customButton.focused.background = null;
customButton.alignment = TextAnchor.MiddleCenter;
customLabel.alignment = TextAnchor.MiddleRight;
}
private static void HierarchyWindowItemOnGUI(int instanceID, Rect selectionRect)
{
Setup();
var instanceObject = EditorUtility.InstanceIDToObject(instanceID);
if (instanceObject == null)
{
return;
}
if ((instanceObject is GameObject) == false)
{
return;
}
var gameObject = instanceObject as GameObject;
var transform = gameObject.transform;
if (transform.childCount == 0)
{
return;
}
var firstChild = transform.GetChild(0);
bool isHidden = ((int)firstChild.hideFlags & (int)HideFlags.HideInHierarchy) == (int)HideFlags.HideInHierarchy;
float width = 25f;
var rect = selectionRect;
rect.x = selectionRect.x + selectionRect.width - width;
rect.width = width;
if (isHidden)
{
if (GUI.Button(rect, toggledOffIcon, customButton))
{
ToggleHighlightInChildren(gameObject, false);
}
var labelRect = selectionRect;
labelRect.width = selectionRect.width - width;
GUI.Label(labelRect, transform.childCount.ToString("N0"), customLabel);
}
else
{
if (GUI.Button(rect, toggledOnIcon, customButton))
{
ToggleHighlightInChildren(gameObject, true);
}
}
}
private static void ToggleHighlightInChildren(GameObject gameObject, bool toggleOn)
{
foreach (Transform child in gameObject.transform)
{
HideFlags hideFlags = child.hideFlags;
if( toggleOn )
{
hideFlags = (HideFlags)((int)hideFlags | (int)HideFlags.HideInHierarchy);
}
else
{
hideFlags = (HideFlags)((int)hideFlags & (~(int)HideFlags.HideInHierarchy));
}
child.hideFlags = hideFlags;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment