Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Dec 8, 2022, between 7 am & 7 pm EST, in the DOTS Dev Blitz Day 2022 - Q&A forum, Discord, and Unity3D Subreddit to learn more about DOTS directly from the Unity Developers.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    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:
    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
    viridisamor, xCyborg, Tinjaw and 2 others like this.
  2. xCyborg

    xCyborg

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

    giantkilleroverunity3d

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