Is there a way to create an editor script that folds out a scene in the editor's hierarchy tab?

Is there a way to fold out the hierarchy in an editor script (or certain hierarchies of particular game objects?).

1 Like

Updated below…

Also might be nice to have a preference to have them all unfolded by default. Right next to the preferences to enable alphanumeric sorting.

1 Like

Updated below…

1 Like

I have to update this. They changed Unity in 2018 to Window/General/Hierarchy instead of Window/Hierarchy.

Basically all you’re doing is selecting the hierarchy window through the scripting using EditorApplication.ExecuteMenuItem:

public static void SetExpandedRecursive(GameObject go, bool expand)
    {
        Type type                               = typeof(EditorWindow).Assembly.GetType("UnityEditor.SceneHierarchyWindow");
        MethodInfo methodInfo                   = type.GetMethod("SetExpandedRecursive");

        EditorApplication.ExecuteMenuItem("Window/General/Hierarchy");

        EditorWindow editorWindow               = EditorWindow.focusedWindow;

        methodInfo.Invoke(editorWindow, new object[] { go.GetInstanceID(), expand });
    }

In my class for a script that’s in every single one of my scenes, I use ExecuteInEditMode:

[ExecuteInEditMode]
public class S_Scene_Editor : MonoBehaviour
{

private void Start() {
//Code goes here!
}

}
4 Likes