关闭

Unity3D expand hierachry by code

469人阅读 评论(0) 收藏 举报

 

  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

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:13444次
    • 积分:287
    • 等级:
    • 排名:千里之外
    • 原创:13篇
    • 转载:15篇
    • 译文:1篇
    • 评论:0条