Skip to content

Instantly share code, notes, and snippets.

@hzi-dev
Forked from yasirkula/EditorCollapseAll.cs
Created January 8, 2021 03:30
Show Gist options
  • Save hzi-dev/08acfd17eb315f93e4aaa0c8ee56eea3 to your computer and use it in GitHub Desktop.
Save hzi-dev/08acfd17eb315f93e4aaa0c8ee56eea3 to your computer and use it in GitHub Desktop.

Revisions

  1. @yasirkula yasirkula revised this gist Feb 7, 2020. 1 changed file with 12 additions and 3 deletions.
    15 changes: 12 additions & 3 deletions EditorCollapseAll.cs
    Original file line number Diff line number Diff line change
    @@ -57,9 +57,18 @@ private static void CollapseGameObjects( MenuCommand command )
    }

    List<GameObject> rootGameObjects = new List<GameObject>();
    int sceneCount = SceneManager.sceneCount;
    for( int i = 0; i < sceneCount; i++ )
    rootGameObjects.AddRange( SceneManager.GetSceneAt( i ).GetRootGameObjects() );
    #if UNITY_2018_3_OR_NEWER
    // Check if a prefab stage is currently open
    var prefabStage = UnityEditor.Experimental.SceneManagement.PrefabStageUtility.GetCurrentPrefabStage();
    if( prefabStage != null && prefabStage.stageHandle.IsValid() )
    rootGameObjects.Add( prefabStage.prefabContentsRoot );
    else
    #endif
    {
    int sceneCount = SceneManager.sceneCount;
    for( int i = 0; i < sceneCount; i++ )
    rootGameObjects.AddRange( SceneManager.GetSceneAt( i ).GetRootGameObjects() );
    }

    if( rootGameObjects.Count > 0 )
    {
  2. @yasirkula yasirkula revised this gist Jan 21, 2020. 1 changed file with 20 additions and 9 deletions.
    29 changes: 20 additions & 9 deletions EditorCollapseAll.cs
    Original file line number Diff line number Diff line change
    @@ -9,27 +9,27 @@ public static class EditorCollapseAll
    private static int wait = 0;
    private static int undoIndex;

    [MenuItem( "Assets/Collapse Folders", priority = 1000 )]
    [MenuItem( "Assets/Collapse All", priority = 1000 )]
    public static void CollapseFolders()
    {
    DirectoryInfo[] rootDirectories = new DirectoryInfo( Application.dataPath ).GetDirectories();
    List<Object> rootDirectoriesList = new List<Object>( rootDirectories.Length );
    for( int i = 0; i < rootDirectories.Length; i++ )
    FileSystemInfo[] rootItems = new DirectoryInfo( Application.dataPath ).GetFileSystemInfos();
    List<Object> rootItemsList = new List<Object>( rootItems.Length );
    for( int i = 0; i < rootItems.Length; i++ )
    {
    Object directoryObj = AssetDatabase.LoadAssetAtPath<Object>( "Assets/" + rootDirectories[i].Name );
    if( directoryObj != null )
    rootDirectoriesList.Add( directoryObj );
    Object asset = AssetDatabase.LoadAssetAtPath<Object>( "Assets/" + rootItems[i].Name );
    if( asset != null )
    rootItemsList.Add( asset );
    }

    if( rootDirectoriesList.Count > 0 )
    if( rootItemsList.Count > 0 )
    {
    Undo.IncrementCurrentGroup();
    Selection.objects = Selection.objects;
    undoIndex = Undo.GetCurrentGroup();

    EditorUtility.FocusProjectWindow();

    Selection.objects = rootDirectoriesList.ToArray();
    Selection.objects = rootItemsList.ToArray();

    EditorApplication.update -= CollapseHelper;
    EditorApplication.update += CollapseHelper;
    @@ -74,6 +74,17 @@ private static void CollapseGameObjects( MenuCommand command )
    }
    }

    [MenuItem( "CONTEXT/Component/Collapse All", priority = 1400 )]
    private static void CollapseComponents( MenuCommand command )
    {
    // Credit: https://forum.unity.com/threads/is-it-possible-to-fold-a-component-from-script-inspector-view.296333/#post-2353538
    ActiveEditorTracker tracker = ActiveEditorTracker.sharedTracker;
    for( int i = 0, length = tracker.activeEditors.Length; i < length; i++ )
    tracker.SetVisible( i, 0 );

    EditorWindow.focusedWindow.Repaint();
    }

    private static void CollapseHelper()
    {
    if( wait < 1 ) // Increase the number if script doesn't always work
  3. @yasirkula yasirkula revised this gist Oct 24, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion EditorCollapseAll.cs
    Original file line number Diff line number Diff line change
    @@ -85,7 +85,7 @@ private static void CollapseHelper()

    EditorWindow focusedWindow = EditorWindow.focusedWindow;
    if( focusedWindow != null )
    focusedWindow.SendEvent( new Event { keyCode = KeyCode.LeftArrow, type = EventType.keyDown, alt = true } );
    focusedWindow.SendEvent( new Event { keyCode = KeyCode.LeftArrow, type = EventType.KeyDown, alt = true } );

    Undo.RevertAllDownToGroup( undoIndex );
    }
  4. @yasirkula yasirkula revised this gist Oct 21, 2019. 1 changed file with 24 additions and 3 deletions.
    27 changes: 24 additions & 3 deletions EditorCollapseAll.cs
    Original file line number Diff line number Diff line change
    @@ -30,13 +30,32 @@ public static void CollapseFolders()
    EditorUtility.FocusProjectWindow();

    Selection.objects = rootDirectoriesList.ToArray();

    EditorApplication.update -= CollapseHelper;
    EditorApplication.update += CollapseHelper;
    }
    }

    [MenuItem( "GameObject/Collapse All", priority = 40 )]
    public static void CollapseGameObjects()
    {
    EditorApplication.update -= CollapseGameObjects;
    CollapseGameObjects( new MenuCommand( null ) );
    }

    [MenuItem( "GameObject/Collapse All", priority = 40 )]
    private static void CollapseGameObjects( MenuCommand command )
    {
    // This happens when this button is clicked via hierarchy's right click context menu
    // and is called once for each object in the selection. We don't want that, we want
    // the function to be called only once
    if( command.context )
    {
    EditorApplication.update -= CollapseGameObjects;
    EditorApplication.update += CollapseGameObjects;

    return;
    }

    List<GameObject> rootGameObjects = new List<GameObject>();
    int sceneCount = SceneManager.sceneCount;
    for( int i = 0; i < sceneCount; i++ )
    @@ -47,8 +66,10 @@ public static void CollapseGameObjects()
    Undo.IncrementCurrentGroup();
    Selection.objects = Selection.objects;
    undoIndex = Undo.GetCurrentGroup();

    Selection.objects = rootGameObjects.ToArray();

    EditorApplication.update -= CollapseHelper;
    EditorApplication.update += CollapseHelper;
    }
    }
    @@ -59,13 +80,13 @@ private static void CollapseHelper()
    wait++;
    else
    {
    EditorApplication.update -= CollapseHelper;
    wait = 0;

    EditorWindow focusedWindow = EditorWindow.focusedWindow;
    if( focusedWindow != null )
    focusedWindow.SendEvent( new Event { keyCode = KeyCode.LeftArrow, type = EventType.keyDown, alt = true } );

    EditorApplication.update -= CollapseHelper;
    Undo.RevertAllDownToGroup( undoIndex );
    }
    }
  5. @yasirkula yasirkula revised this gist Oct 29, 2017. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion EditorCollapseAll.cs
    Original file line number Diff line number Diff line change
    @@ -23,9 +23,12 @@ public static void CollapseFolders()

    if( rootDirectoriesList.Count > 0 )
    {
    Undo.IncrementCurrentGroup();
    Selection.objects = Selection.objects;
    undoIndex = Undo.GetCurrentGroup();

    EditorUtility.FocusProjectWindow();

    Selection.objects = rootDirectoriesList.ToArray();
    EditorApplication.update += CollapseHelper;
    }
    @@ -41,6 +44,8 @@ public static void CollapseGameObjects()

    if( rootGameObjects.Count > 0 )
    {
    Undo.IncrementCurrentGroup();
    Selection.objects = Selection.objects;
    undoIndex = Undo.GetCurrentGroup();

    Selection.objects = rootGameObjects.ToArray();
  6. @yasirkula yasirkula revised this gist Oct 29, 2017. No changes.
  7. @yasirkula yasirkula revised this gist Oct 29, 2017. No changes.
  8. @yasirkula yasirkula revised this gist Oct 29, 2017. No changes.
  9. @yasirkula yasirkula renamed this gist Oct 29, 2017. 1 changed file with 28 additions and 8 deletions.
    36 changes: 28 additions & 8 deletions EditorCollapseFolders.cs → EditorCollapseAll.cs
    Original file line number Diff line number Diff line change
    @@ -2,14 +2,15 @@
    using System.IO;
    using UnityEditor;
    using UnityEngine;
    using UnityEngine.SceneManagement;

    public static class EditorCollapseFolders
    public static class EditorCollapseAll
    {
    private static int wait = 0;
    private static Object[] prevSelection;
    private static int undoIndex;

    [MenuItem( "Assets/Collapse Folders", priority = 1000 )]
    public static void CollapseAll()
    public static void CollapseFolders()
    {
    DirectoryInfo[] rootDirectories = new DirectoryInfo( Application.dataPath ).GetDirectories();
    List<Object> rootDirectoriesList = new List<Object>( rootDirectories.Length );
    @@ -22,14 +23,31 @@ public static void CollapseAll()

    if( rootDirectoriesList.Count > 0 )
    {
    prevSelection = Selection.objects;
    undoIndex = Undo.GetCurrentGroup();
    EditorUtility.FocusProjectWindow();

    Selection.objects = rootDirectoriesList.ToArray();
    EditorApplication.update += CollapseHelper;
    }
    }

    [MenuItem( "GameObject/Collapse All", priority = 40 )]
    public static void CollapseGameObjects()
    {
    List<GameObject> rootGameObjects = new List<GameObject>();
    int sceneCount = SceneManager.sceneCount;
    for( int i = 0; i < sceneCount; i++ )
    rootGameObjects.AddRange( SceneManager.GetSceneAt( i ).GetRootGameObjects() );

    if( rootGameObjects.Count > 0 )
    {
    undoIndex = Undo.GetCurrentGroup();

    Selection.objects = rootGameObjects.ToArray();
    EditorApplication.update += CollapseHelper;
    }
    }

    private static void CollapseHelper()
    {
    if( wait < 1 ) // Increase the number if script doesn't always work
    @@ -38,10 +56,12 @@ private static void CollapseHelper()
    {
    wait = 0;

    EditorWindow.focusedWindow.SendEvent( new Event { keyCode = KeyCode.LeftArrow, type = EventType.keyDown, alt = true } );
    EditorApplication.update -= CollapseHelper;
    EditorWindow focusedWindow = EditorWindow.focusedWindow;
    if( focusedWindow != null )
    focusedWindow.SendEvent( new Event { keyCode = KeyCode.LeftArrow, type = EventType.keyDown, alt = true } );

    Selection.objects = prevSelection;
    EditorApplication.update -= CollapseHelper;
    Undo.RevertAllDownToGroup( undoIndex );
    }
    }
    }
  10. @yasirkula yasirkula revised this gist Oct 29, 2017. No changes.
  11. @yasirkula yasirkula created this gist Oct 29, 2017.
    47 changes: 47 additions & 0 deletions EditorCollapseFolders.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    using System.Collections.Generic;
    using System.IO;
    using UnityEditor;
    using UnityEngine;

    public static class EditorCollapseFolders
    {
    private static int wait = 0;
    private static Object[] prevSelection;

    [MenuItem( "Assets/Collapse Folders", priority = 1000 )]
    public static void CollapseAll()
    {
    DirectoryInfo[] rootDirectories = new DirectoryInfo( Application.dataPath ).GetDirectories();
    List<Object> rootDirectoriesList = new List<Object>( rootDirectories.Length );
    for( int i = 0; i < rootDirectories.Length; i++ )
    {
    Object directoryObj = AssetDatabase.LoadAssetAtPath<Object>( "Assets/" + rootDirectories[i].Name );
    if( directoryObj != null )
    rootDirectoriesList.Add( directoryObj );
    }

    if( rootDirectoriesList.Count > 0 )
    {
    prevSelection = Selection.objects;
    EditorUtility.FocusProjectWindow();

    Selection.objects = rootDirectoriesList.ToArray();
    EditorApplication.update += CollapseHelper;
    }
    }

    private static void CollapseHelper()
    {
    if( wait < 1 ) // Increase the number if script doesn't always work
    wait++;
    else
    {
    wait = 0;

    EditorWindow.focusedWindow.SendEvent( new Event { keyCode = KeyCode.LeftArrow, type = EventType.keyDown, alt = true } );
    EditorApplication.update -= CollapseHelper;

    Selection.objects = prevSelection;
    }
    }
    }