您的位置:首页 > 移动开发 > Unity3D

Unity3D expand hierachry by code

2015-07-07 11:33 405 查看
 

  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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: