using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
[InitializeOnLoad]
public static class HierarchyFolder
{
// A dictionary to store the expanded state of each folder
private static Dictionary<Transform, bool> _folderStates = new Dictionary<Transform, bool>();
static HierarchyFolder()
{
EditorApplication.hierarchyWindowItemOnGUI += OnHierarchyGUI;
}
private static void OnHierarchyGUI(int instanceID, Rect selectionRect)
{
GameObject obj = EditorUtility.InstanceIDToObject(instanceID) as GameObject;
if (obj != null)
{
Rect rect = new Rect(selectionRect);
rect.x = selectionRect.xMax - 20;
// Draw a foldout for any objects marked as folders
if (obj.name.StartsWith("[Folder]"))
{
bool state;
if (!_folderStates.TryGetValue(obj.transform, out state))
{
state = true;
_folderStates[obj.transform] = state;
}
bool newState = EditorGUI.Foldout(rect, state, GUIContent.none);
// If state changes, toggle visibility of children
if (newState != state)
{
_folderStates[obj.transform] = newState;
SetChildrenVisibility(obj.transform, newState);
}
}
}
}
private static void SetChildrenVisibility(Transform parent, bool state)
{
foreach (Transform child in parent)
{
child.gameObject.hideFlags = state ? HideFlags.None : HideFlags.HideInHierarchy;
// Refresh the Hierarchy
EditorApplication.RepaintHierarchyWindow();
}
}
}
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
namespace DevKit.Utility.Editor
{
public class HierarchyFolder : MonoBehaviour
{
}
code
[InitializeOnLoad]
public static class HierarchyFolderExtension
{
// A dictionary to store the expanded state of each folder
private static readonly Dictionary<Transform, bool> _folderStates = new Dictionary<Transform, bool>();
static HierarchyFolderExtension()
{
EditorApplication.hierarchyWindowItemOnGUI += OnHierarchyGUI;
}
private static void OnHierarchyGUI(int instanceID, Rect selectionRect)
{
GameObject obj = EditorUtility.InstanceIDToObject(instanceID) as GameObject;
if (obj != null)
{
Rect rect = new Rect(selectionRect);
rect.x = selectionRect.xMax - 20;
// Draw a foldout for any objects marked as folders
if (obj.name.StartsWith("[Folder]"))
{
bool state;
if (!_folderStates.TryGetValue(obj.transform, out state))
{
state = true;
_folderStates[obj.transform] = state;
}
bool newState = EditorGUI.Foldout(rect, state, GUIContent.none);
// If state changes, toggle visibility of children
if (newState != state)
{
_folderStates[obj.transform] = newState;
_setChildrenVisibility(obj.transform, newState);
}
}
}
}
private static void _setChildrenVisibility(Transform parent, bool state)
{
foreach (Transform child in parent)
{
child.gameObject.hideFlags = state ? HideFlags.None : HideFlags.HideInHierarchy;
// Refresh the Hierarchy
EditorApplication.RepaintHierarchyWindow();
}
}
[MenuItem("DevKit/Utility/CreateHierarchyFolder")]
public static void Create()
{
}
}
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
namespace DevKit.Utility.Editor
{
public class HierarchyFolder : MonoBehaviour
{
}
[InitializeOnLoad]
public static class HierarchyFolderExtension
{
private static readonly Dictionary<Transform, bool> _folderStates = new Dictionary<Transform, bool>();
static HierarchyFolderExtension()
{
EditorApplication.hierarchyWindowItemOnGUI += OnHierarchyGUI;
}
private static void OnHierarchyGUI(int instanceID, Rect selectionRect)
{
GameObject obj = EditorUtility.InstanceIDToObject(instanceID) as GameObject;
if (obj != null)
{
Rect rect = new Rect(selectionRect);
rect.x = selectionRect.xMax - 20;
if (obj.name.StartsWith("[Folder]"))
{
bool state;
if (!_folderStates.TryGetValue(obj.transform, out state))
{
state = true;
_folderStates[obj.transform] = state;
}
bool newState = EditorGUI.Foldout(rect, state, GUIContent.none);
if (newState != state)
{
_folderStates[obj.transform] = newState;
_setChildrenVisibility(obj.transform, newState);
}
}
}
}
private static void _setChildrenVisibility(Transform parent, bool state)
{
foreach (Transform child in parent)
{
child.gameObject.hideFlags = state ? HideFlags.None : HideFlags.HideInHierarchy;
EditorApplication.RepaintHierarchyWindow();
}
}
[MenuItem("DevKit/Utility/Create Hierarchy Folder %#f")]
public static void Create()
{
GameObject folder = new GameObject("[Folder] New Folder");
Undo.RegisterCreatedObjectUndo(folder, "Create Hierarchy Folder");
// Optionally select the new folder in the hierarchy
Selection.activeGameObject = folder;
// Optionally, reset folder's transform if you want to position it at origin in the scene
folder.transform.position = Vector3.zero;
}
}
}
User
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
namespace DevKit.Utility.Editor
{
public class HierarchyFolder : MonoBehaviour
{
/// The "EditorOnly" tag string.
private const string _editorOnlyTag = "EditorOnly";
[CustomEditor(typeof(HierarchyFolder))]
class HierarchyFolderInspector : UnityEditor.Editor
{
} use custom inspector to override draw it's name in hierarchy
using UnityEngine;
using UnityEditor;
namespace DevKit.Utility.Editor
{
[CustomEditor(typeof(HierarchyFolder))]
class HierarchyFolderInspector : UnityEditor.Editor
{
public override void OnInspectorGUI()
{
HierarchyFolder folder = (HierarchyFolder)target;
folder.gameObject.name = EditorGUILayout.TextField("Folder Name", folder.gameObject.name.Substring(8));
// Call the base class method to draw other controls
base.OnInspectorGUI();
}
}
}
/// <summary>Reset is called when the user hits the Reset button in the Inspector's context menu or when adding the component the first time.</summary>
private void Reset()
{
EnsureEditorOnlyTagSet();
}
/// <summary>This function is called when the script is loaded or a value is changed in the Inspector.</summary>
private void OnValidate()
{
EnsureEditorOnlyTagSet();
}
/// <summary>Sets the Tag to <see cref="_editorOnlyTag" /> on the <see cref="GameObject" /> this <see cref="MonoBehaviour" /> is attached to.</summary>
private void EnsureEditorOnlyTagSet()
{
if (!CompareTag(_editorOnlyTag)) gameObject.tag = _editorOnlyTag;
}
}
[InitializeOnLoad]
public static class HierarchyFolderExtension
{
// A dictionary to store the expanded state of each folder
private static readonly Dictionary<Transform, bool> _folderStates = new Dictionary<Transform, bool>();
static HierarchyFolderExtension()
{
EditorApplication.hierarchyWindowItemOnGUI += OnHierarchyGUI;
}
private static void OnHierarchyGUI(int instanceID, Rect selectionRect)
{
GameObject obj = EditorUtility.InstanceIDToObject(instanceID) as GameObject;
if (obj != null)
{
Rect rect = new Rect(selectionRect);
rect.x = selectionRect.xMax - 20;
// Draw a foldout for any objects marked as folders
if (obj.name.StartsWith("[Folder]"))
{
bool state;
if (!_folderStates.TryGetValue(obj.transform, out state))
{
state = true;
_folderStates[obj.transform] = state;
}
bool newState = EditorGUI.Foldout(rect, state, GUIContent.none);
// If state changes, toggle visibility of children
if (newState != state)
{
_folderStates[obj.transform] = newState;
_setChildrenVisibility(obj.transform, newState);
}
}
}
}
private static void _setChildrenVisibility(Transform parent, bool state)
{
foreach (Transform child in parent)
{
child.gameObject.hideFlags = state ? HideFlags.None : HideFlags.HideInHierarchy;
// Refresh the Hierarchy
EditorApplication.RepaintHierarchyWindow();
}
}
[MenuItem("GameObject/DevKit/Utility/CreateHierarchyFolder")]
public static void CreateHierarchyFolder()
{
GameObject folder = new GameObject("[Folder] Folder Name");
folder.AddComponent<HierarchyFolder>();
Undo.RegisterCreatedObjectUndo(folder, "Create Hierarchy Folder");
// Optionally select the new folder in the hierarchy
Selection.activeGameObject = folder;
// Optionally, reset folder's transform if you want to position it at origin in the scene
folder.transform.position = Vector3.zero;
}
}