Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  4. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Collapse All Folders in Project/All GameObjects in Hierarchy (Editor Script)

Discussion in 'Extensions & OnGUI' started by yasirkula, Oct 29, 2017.

  1. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    506
    Hi there,

    I'd like to share a simple editor script that allows you to collapse all folders in the Project view or collapse all GameObjects in Hierarchy view programmatically.

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using System.IO;
    3. using UnityEditor;
    4. using UnityEngine;
    5. using UnityEngine.SceneManagement;
    6.  
    7. public static class EditorCollapseAll
    8. {
    9.     private static int wait = 0;
    10.     private static int undoIndex;
    11.  
    12.     [MenuItem( "Assets/Collapse Folders", priority = 1000 )]
    13.     public static void CollapseFolders()
    14.     {
    15.         DirectoryInfo[] rootDirectories = new DirectoryInfo( Application.dataPath ).GetDirectories();
    16.         List<Object> rootDirectoriesList = new List<Object>( rootDirectories.Length );
    17.         for( int i = 0; i < rootDirectories.Length; i++ )
    18.         {
    19.             Object directoryObj = AssetDatabase.LoadAssetAtPath<Object>( "Assets/" + rootDirectories[i].Name );
    20.             if( directoryObj != null )
    21.                 rootDirectoriesList.Add( directoryObj );
    22.         }
    23.  
    24.         if( rootDirectoriesList.Count > 0 )
    25.         {
    26.             Undo.IncrementCurrentGroup();
    27.             Selection.objects = Selection.objects;
    28.             undoIndex = Undo.GetCurrentGroup();
    29.  
    30.             EditorUtility.FocusProjectWindow();
    31.  
    32.             Selection.objects = rootDirectoriesList.ToArray();
    33.             EditorApplication.update += CollapseHelper;
    34.         }
    35.     }
    36.  
    37.     [MenuItem( "GameObject/Collapse All", priority = 40 )]
    38.     public static void CollapseGameObjects()
    39.     {
    40.         List<GameObject> rootGameObjects = new List<GameObject>();
    41.         int sceneCount = SceneManager.sceneCount;
    42.         for( int i = 0; i < sceneCount; i++ )
    43.             rootGameObjects.AddRange( SceneManager.GetSceneAt( i ).GetRootGameObjects() );
    44.  
    45.         if( rootGameObjects.Count > 0 )
    46.         {
    47.             Undo.IncrementCurrentGroup();
    48.             Selection.objects = Selection.objects;
    49.             undoIndex = Undo.GetCurrentGroup();
    50.            
    51.             Selection.objects = rootGameObjects.ToArray();
    52.             EditorApplication.update += CollapseHelper;
    53.         }
    54.     }
    55.  
    56.     private static void CollapseHelper()
    57.     {
    58.         if( wait < 1 ) // Increase the number if script doesn't always work
    59.             wait++;
    60.         else
    61.         {
    62.             wait = 0;
    63.  
    64.             EditorWindow focusedWindow = EditorWindow.focusedWindow;
    65.             if( focusedWindow != null )
    66.                 focusedWindow.SendEvent( new Event { keyCode = KeyCode.LeftArrow, type = EventType.keyDown, alt = true } );
    67.  
    68.             EditorApplication.update -= CollapseHelper;
    69.             Undo.RevertAllDownToGroup( undoIndex );
    70.         }
    71.     }
    72. }
    Simply create an EditorCollapseAll.cs script in Assets/Editor folder of your project and paste the above code in it.

    Collapsing Hierarchy View
    To collapse all GameObjects in the currently active Hierarchy view, either use the GameObject menu or right click a GameObject and select "Collapse All".

    Collapsing Project View
    To collapse all folders in Project view, either use the Assets menu or right click an asset and select "Collapse Folders". Note that folders containing the selected assets will not be collapsed.

    Enjoy!
     
  2. morphinegames

    morphinegames

    Joined:
    Mar 13, 2015
    Posts:
    28
    Just wanted to say thank you - I was just looking for some way to do this and this works great.
     
    yasirkula likes this.
  3. StylishCoding

    StylishCoding

    Joined:
    Apr 21, 2018
    Posts:
    97
    Unity indeed supports this feature natively. Hold alt key to collapse/expand any item in Hierarchy or Project View, it collapse/expand all under this item.
     
  4. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    506
    The code actually simulates the same behaviour programmatically, only that it selects all the root folders and collapses them all at once, so that you won't have to alt-click them all one by one (can be useful when you have many root folders).