Unity3D expand hierachry by code

 

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


        EditorApplication.ExecuteMenuItem("Window/Hierarchy");
        var window = EditorWindow.focusedWindow;


        methodInfo.Invoke(window, new object[] { go.GetInstanceID(), expand });
        //Selection.activeObject = go;
    }




    public static void Collapse(GameObject go, bool collapse)
    {
        // bail out immediately if the go doesn't have children
        if (go.transform.childCount == 0) return;


        // get a reference to the hierarchy window
        var hierarchy = GetFocusedWindow("Hierarchy");


        // select our go
        SelectObject(go);


        // create a new key event (RightArrow for collapsing, LeftArrow for folding)
        var key = new Event { keyCode = collapse ? KeyCode.RightArrow : KeyCode.LeftArrow, type = EventType.keyDown };


        // finally, send the window the event
        hierarchy.SendEvent(key);
    }


    public static void SelectObject(Object obj)
    {
        Selection.activeObject = obj;
    }


    public static EditorWindow GetFocusedWindow(string window)
    {
        FocusOnWindow(window);
        return EditorWindow.focusedWindow;
    }


    public static void FocusOnWindow(string window)
    {
        EditorApplication.ExecuteMenuItem("Window/" + window);
    }


from: http://answers.unity3d.com/questions/656869/foldunfold-gameobject-from-code.html

你可能感兴趣的:(Unity3D expand hierachry by code)