为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C#
Cats萌萌 2023-05-13 15:42:49
有没有办法在脚本中复制游戏对象的变换值(位置、旋转、缩放)。我不想单独复制每个转换值,如下面的脚本所示。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;}
查看完整描述

2 回答

?
墨色风雨

TA贡献1853条经验 获得超6个赞

不,不是内置的,但我会推荐一个合适的类型


[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);


查看完整回答
反对 回复 2023-05-13
?
Smart猫小萌

TA贡献1911条经验 获得超7个赞

我只是这样做了: from.transform.rotation = transform.rotation; 这移动了整个旋转



查看完整回答
反对 回复 2023-05-13
  • 2 回答
  • 0 关注
  • 109 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信