复制游戏对象的所有变换值?

有没有办法在脚本中复制游戏对象的变换值(位置、旋转、缩放)。我不想单独复制每个转换值,如下面的脚本所示。


public GameObject Obj02;

public GameObject Obj04;

private Vector3 positionOfObj02;

private Vector3 rotationOfObj02;

private Vector3 positionOfObj04;

private Vector3 rotationOfObj04;


void Start()

{

    positionOfObj02 = Obj02.transform.localPosition;

    rotationOfObj02 = Obj02.transform.localEulerAngles;


    positionOfObj04 = Obj04.transform.localPosition;

    rotationOfObj04 = Obj04.transform.localEulerAngles;

}


Cats萌萌
浏览 110回答 2
2回答

墨色风雨

不,不是内置的,但我会推荐一个合适的类型[Serializable]public class TransformData{    public Vector3 LocalPosition = Vector3.zero;    public Vector3 LocalEulerRotation = Vector3.zero;    public Vector3 LocalScale = Vector3.one;    // Unity requires a default constructor for serialization    public TransformData() { }    public TransformData(Transform transform)    {        LocalPosition = transform.localPosition;        LocalEulerRotation = transform.localEulerAngles;        LocalScale = transform.localScale;    }    public void ApplyTo(Transform transform)    {        transform.localPosition = LocalPosition;        transform.localEulerAngles = LocalEulerRotation ;        transform.localScale = LocalScale;    }}并像使用它public GameObject Obj02;public GameObject Obj04;private TransformData transformDataOfObj02;private TransformData transformDataOfObj04;void Start(){    transformDataOfObj02 = new TransformData(Obj02.transform);    transformDataOfObj04 = new TransformData(Obj04.transform);}以后也许transformDataOfObj02.ApplyTo(anotherObject.transform);

Smart猫小萌

我只是这样做了: from.transform.rotation = transform.rotation; 这移动了整个旋转
打开App,查看更多内容
随时随地看视频慕课网APP