美文网首页
Unity 复制粘贴Transform

Unity 复制粘贴Transform

作者: 114105lijia | 来源:发表于2022-04-28 09:20 被阅读0次

    在开发过程中,有时需要复制其他物体的Transform,目前只能一个个的复制粘贴,颇为麻烦,所以写了一个帮助工具。

    使用时,先选中物体,然后依次选择【Tools】-【复制&粘贴 Transform】-【复制 Transform】点击复制: image.png 接着再选中需要粘贴Transform的物体,点击菜单中的【粘贴】即可。

    脚本如下:

    //EditorWindow类无法打包,主意添加这个判断
    #if UNITY_EDITOR
    
    using UnityEngine;
    using System;
    using UnityEditor;
    
    
    public class CopyTransformTool : EditorWindow
    {
        [MenuItem("Tools/复制&粘贴Transform")]
    
        public static void Open()
        {
            EditorWindow.GetWindow(typeof(CopyTransformTool));
        }
        void OnGUI()
        {
            if (GUILayout.Button("复制 Transform"))
            {
                CopyObjTransform();
            }
    
            if (GUILayout.Button("粘贴 Transform"))
            {
                PasteObjTransform();
            }
    
            if (GUILayout.Button("复制 Position"))
            {
                CopyObjPosition();
            }
    
            if (GUILayout.Button("粘贴 Position"))
            {
                PasteObjPosition();
            }
    
            if (GUILayout.Button("复制 Rotation"))
            {
                CopyObjRotation();
            }
    
            if (GUILayout.Button("粘贴 Rotation"))
            {
                PasteObjRotation();
            }
        }
    
        /*--------------------- 复制/粘贴坐标 -------------------------*/
        static void CopyObjPosition()
        {
            GameObject obj = UnityEditor.Selection.activeGameObject;
            if (obj != null)
            {
                string ret = obj.transform.localPosition.x + "," +
                            obj.transform.localPosition.y + "," +
                            obj.transform.localPosition.z;
    
                GUIUtility.systemCopyBuffer = ret;
    
                Debug.Log("Copy Position Success : " + ret);
            }
        }
    
        static void PasteObjPosition()
        {
            GameObject obj = UnityEditor.Selection.activeGameObject;
            if (obj != null)
            {
                string ret = GUIUtility.systemCopyBuffer;
                string[] array = ret.Split(",");
                if (array.Length == 3)
                {
                    Vector3 position = new Vector3(Convert.ToSingle(array[0]),
                                                    Convert.ToSingle(array[1]),
                                                    Convert.ToSingle(array[2]));
    
                    obj.transform.localPosition = position;
    
                    Debug.Log("Paste Position Success : " + position);
                }
                else {
                    Debug.Log("Paste Position Error");
                }
            }
        }
    
        static void CopyObjRotation()
        {
            GameObject obj = UnityEditor.Selection.activeGameObject;
            if (obj != null)
            {
                string ret = obj.transform.localEulerAngles.x + "," +
                             obj.transform.localEulerAngles.y + "," +
                             obj.transform.localEulerAngles.z;
    
                GUIUtility.systemCopyBuffer = ret;
    
                Debug.Log("Copy Rotation Success : " + ret);
            }
        }
    
        static void PasteObjRotation()
        {
            GameObject obj = UnityEditor.Selection.activeGameObject;
            if (obj != null)
            {
                string ret = GUIUtility.systemCopyBuffer;
                string[] array = ret.Split(",");
                if (array.Length == 3)
                {
                    Vector3 rotation = new Vector3(Convert.ToSingle(array[0]),
                                                    Convert.ToSingle(array[1]),
                                                    Convert.ToSingle(array[2]));
    
                    obj.transform.localEulerAngles = rotation;
    
                    Debug.Log("Paste Rotation Success : " + rotation);
                }
                else
                {
                    Debug.Log("Paste Rotation Error");
                }
            }
        }
    
        static void CopyObjTransform()
        {
            GameObject obj = UnityEditor.Selection.activeGameObject;
            if (obj != null)
            {
                string ret = obj.transform.localPosition.x + "," +
                             obj.transform.localPosition.y + "," +
                             obj.transform.localPosition.z + "," +
                             obj.transform.localEulerAngles.x + "," +
                             obj.transform.localEulerAngles.y + "," +
                             obj.transform.localEulerAngles.z + "," +
                             obj.transform.localScale.x + "," +
                             obj.transform.localScale.y + "," +
                             obj.transform.localScale.z;
    
                GUIUtility.systemCopyBuffer = ret;
    
                Debug.Log("Copy Transform Success : " + ret);
            }
        }
    
        static void PasteObjTransform()
        {
            GameObject obj = UnityEditor.Selection.activeGameObject;
            if (obj != null)
            {
                string ret = GUIUtility.systemCopyBuffer;
                string[] array = ret.Split(",");
                if (array.Length == 9)
                {
                    Vector3 position = new Vector3(Convert.ToSingle(array[0]),
                                                    Convert.ToSingle(array[1]),
                                                    Convert.ToSingle(array[2]));
    
                    Vector3 rotation = new Vector3(Convert.ToSingle(array[3]),
                                                    Convert.ToSingle(array[4]),
                                                    Convert.ToSingle(array[5]));
    
                    Vector3 scale = new Vector3(Convert.ToSingle(array[6]),
                                                    Convert.ToSingle(array[7]),
                                                    Convert.ToSingle(array[8]));
    
                    obj.transform.localPosition = position;
    
                    Debug.Log("Paste Transform Success : " + position);
                }
                else
                {
                    Debug.Log("Paste Transform Error");
                }
            }
        }
    }
    
    #endif
    

    相关文章

      网友评论

          本文标题:Unity 复制粘贴Transform

          本文链接:https://www.haomeiwen.com/subject/ctiryrtx.html