Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

[REL] Collapse all GameObjects recursively in Hierarchy view

Discussion in 'Immediate Mode GUI (IMGUI)' started by neonblitzer, Feb 18, 2015.

  1. neonblitzer

    neonblitzer

    Joined:
    Aug 5, 2013
    Posts:
    13
    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
    viridisamor, xCyborg, Tinjaw and 2 others like this.
  2. xCyborg

    xCyborg

    Joined:
    Oct 4, 2010
    Posts:
    633
    That's handy thanks alot
     
  3. giantkilleroverunity3d

    giantkilleroverunity3d

    Joined:
    Feb 28, 2014
    Posts:
    383
    I like this. I am going to add 'No expand if item is disabled.'