Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

[REL] Collapse all GameObjects recursively in Hierarchy view

Discussion in 'Extensions & OnGUI' started by neonblitzer, Feb 18, 2015.

  1. neonblitzer

    neonblitzer

    Joined:
    Aug 5, 2013
    Posts:
    11
    Collapses all GameObjects in Hierarchy view recursively, with a single keyboard shortcut or a menu item.
    Drop the script in an Editor folder. Shortcut is Ctrl-Alt-Left on Windows, menu item is "Window/Collapse Hierarchy".

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using UnityEngine.SceneManagement;
    4.  
    5. public class HierarchyCollapser {
    6.     [MenuItem("Window/Collapse Hierarchy %&LEFT")]
    7.     public static void CollapseHierarchy() {
    8.         EditorApplication.ExecuteMenuItem("Window/Hierarchy");
    9.         var hierarchyWindow = EditorWindow.focusedWindow;
    10.         var expandMethodInfo = hierarchyWindow.GetType().GetMethod("SetExpandedRecursive");
    11.         foreach (GameObject root in SceneManager.GetActiveScene().GetRootGameObjects()) {
    12.             expandMethodInfo.Invoke(hierarchyWindow, new object[] { root.GetInstanceID(), false });
    13.         }
    14.     }
    15. }
    16.  
    This should outperform iterating all GameObjects and collapsing them individually. This code is available under the MIT License.

    Sources/more information:
    Menu Items and keyboard shortcuts: http://docs.unity3d.com/ScriptReference/MenuItem.html
    Fold recursively: http://answers.unity3d.com/questions/656869/#comment-858132
     
    Last edited: Oct 13, 2016
    Tinjaw, ahkow and pmurph03 like this.
  2. xCyborg

    xCyborg

    Joined:
    Oct 4, 2010
    Posts:
    497
    That's handy thanks alot