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 Lights
下一篇Unity3D Editor在右键弹出菜单中添加 item
想对作者说点什么? 我来说一句

悬浮Expand

2014年06月27日 43KB 下载

Expand函数

2015年04月24日 681B 下载

没有更多推荐了,返回首页

关闭
关闭