一、前言
由于某些原因,导致场景中有些物体预制体missing,需要从备份的项目中重新获取transform值。
二、使用步骤
将代码放入Editor文件夹内即可。
代码如下:
using UnityEditor;
using UnityEngine;
public class CopyTransform : Editor
{
[MenuItem("CONTEXT/Transform/粘贴信息")]
static void Paste()
{
GameObject[] obj = Selection.gameObjects;
string str = GUIUtility.systemCopyBuffer;
string[] b = str.Split('\n');
string[] a;
Quaternion rotation;
for (int i = 0; i < obj.Length; i++)
{
a = b[i].Split(',');
obj[i].transform.localPosition = new Vector3(float.Parse(a[0]), float.Parse(a[1]), float.Parse(a[2]));
rotation = Quaternion.Euler(float.Parse(a[3]), float.Parse(a[4]), float.Parse(a[5]));
obj[i].transform.localRotation = rotation;
obj[i].transform.localScale = new Vector3(float.Parse(a[6]), float.Parse(a[7]), float.Parse(a[8]));
}
}
[MenuItem("CONTEXT/Transform/拷贝信息")]
[System.Obsolete]
static void Copy()
{
string str = "";
Transform trans;
GameObject[] obj = Selection.gameObjects;
foreach (GameObject game in obj)
{
trans = game.transform;
str += trans.localPosition.x + "," + trans.localPosition.y + "," + trans.localPosition.z + "," +
trans.localEulerAngles.x + "," + trans.localEulerAngles.y + "," + trans.localEulerAngles.z + "," +
trans.localScale.x + "," + trans.localScale.y + "," + trans.localScale.z + '\n';
}
TextEditor te = new TextEditor();
te.content = new GUIContent(str);
te.SelectAll();
te.Copy();
}
}
注:复制时选中了几个物体,粘贴时也需要选中几个物体。
总结
菜鸡一个,欢迎各位大佬拿知识的大锤锤我。
|