Heirachy functionality in editor - i clearly missed something

Hey all,

So, we all know we can enable all selected children of gameobjects using this code, and obviously also disable them with a small change.

    [UnityEditor.MenuItem("GameObject/Enable All Children")]    
    private static void EnableAllChildren()
    {
        foreach (var go in UnityEditor.Selection.gameObjects)
        {
            
            for (int i = 0; i < go.transform.childCount; i++)
            {
                go.transform.GetChild(i).gameObject.SetActive(true);
            }
        }
    }

However, what i want is the equivelent of pressing H (which toggles the scene visibility) but only on the children, so my question is, how can I change it so what Im looking for is

how can i programattically expand/unexpand a gameobject in the heirachy
how can I programattically show/hide a gameobject in the heirachy

eg I want to add that functionality to a script with the above.

I feel like i missed something obvious

Ignore me I found it

    [UnityEditor.MenuItem("GameObject/Toggle Scene Visibility of children")]
    private static void ToggleSceneVisibility()
    {
        foreach (var go in Selection.gameObjects)
        {
            if (go.transform.childCount > 0)
            {
                bool isVisible = !SceneVisibilityManager.instance.IsHidden(go.transform.GetChild(0).gameObject);
                for (int i = 0; i < go.transform.childCount; i++)
                {
                    if (isVisible)
                    {
                        SceneVisibilityManager.instance.Hide(go.transform.GetChild(i).gameObject, true);
                    }
                    else
                    {
                        SceneVisibilityManager.instance.Show(go.transform.GetChild(i).gameObject, true);
                    }
                }
            }
        }
    }

go copilot :smiley:

2 Likes