Exemplo n.º 1
0
    internal static void Rename(this GameObject go)
    {
        var hWindow = WindowX.Hierarchy;

        if (Event.current != null && Event.current.keyCode == KeyCode.Escape)
        {
            renameGO   = null;
            renameStep = 0;
            hWindow.Repaint();
            return;
        }

        if (renameGO != go)
        {
            renameGO   = go;
            renameStep = 2;
        }

        Debug.Log("Rename : " + go + ":" + renameStep);

        if (!IsRenaming())
        {
            //not yet in edit name mode, try to do it now
            Selection.activeGameObject = go;

#if UNITY_4_5 || UNITY_4_6 || UNITY_5
            var treeView = WindowX.Hierarchy.xGetField("m_TreeView");
            var data     = treeView.xGetProperty("data");
            var gui      = treeView.xGetProperty("gui");
            var item     = data.xInvoke("FindItem", null, null, go.GetInstanceID());

            if (item != null && gui != null)
            {
                gui.xInvoke("BeginRename", null, null, item, 0f);
            }
#else
            var property = new HierarchyProperty(HierarchyType.GameObjects);
            property.Find(go.GetInstanceID(), null);

            hWindow.xInvoke("BeginNameEditing", TypeX.BaseProjectWindowT, null, go.GetInstanceID());
            hWindow.xSetField("m_NameEditString", go.name, TypeX.BaseProjectWindowT);     //name will be missing without this line
            hWindow.Repaint();
#endif
        }
        else
        {
            if (Event.current == null)
            {
                renameStep = 2;
                //Debug.Log("How can Event.current be null ?");
                return;
            }

            if (Event.current.type == EventType.repaint && renameStep > 0)
            {
                renameStep--;
                //hWindow.Repaint();
            }

            if (Event.current.type != EventType.repaint && renameStep == 0)
            {
                renameGO = null;
            }
        }
        //}
    }