[组件笔记]5.UnityEngine.Transform

public class Transform : Component, IEnumerable{}

在场景里描述GameObject的空间位置、旋转和缩放的组件以及携带节点关系数据。继承自UnityEngine.Component,并且实现了枚举器。

  • 此组件对于GameObject空间变换具有重要意义。
  • 由于实现了IEnumerable接口,可以使用foreach (Transform child in transform){}进行遍历,其只包含此对象子GameObject的Transform,而不包含子GameObject的子GameObject的Transform。
  • 在物理变换体系中,应使用物理变换体系相关api。
  • 此组件中保管有节点关系数据。
  • 此组件依赖于GameObject。
  • 当子父物体都有非等比缩放时,子发生旋转会出现倾斜拉伸等异常现象。

变量

childCount

public int childCount { get; }

此GameObject具有的子节点数。

  • 自身不在计数中

eulerAngles

public Vector3 eulerAngles { get; set; }

以欧拉角表示的旋转数据(以度为单位)。

  • 表示世界空间中的旋转。在属性面板中查看GameObject的旋转时,可能会看到与此属性中存储的值不同的角度值。这是因为属性面板显示本地旋转,如查看本地旋转角度使用Transform.localEulerAngles
  • 物体的旋转不是以欧拉角进行存储,而是以四元数作为存储
  • 通过变换this.eulerAngles的方式进行旋转,可能会造成问题,可以使用四元数乘积transform.rotation *= Quaternion.AngleAxis(angle* Time.deltaTime, Vector3.up);的方式。也可以不依赖于读取 Quanternion.eulerAngles 来增加旋转,而是定义变量Vector3 currentEulerAngles; 来设置它。所有旋转更改都在currentEulerAngles 变量中进行,这些更改后应用于四元数transform.eulerAngles = currentEulerAngles += Vector3.up * Time.deltaTime * 10f;

forward

public Vector3 forward { get; set; }

物体的前矢量

  • 标准化矢量,它表示物体世界空间中变换的蓝轴,也就是z轴。
  • Vector3.forward不同,transform.forward在移动GameObject的同时,还考虑其自身旋转角度,此向量的坐标体系为世界坐标。
  • 旋转游戏对象时,表示游戏对象的 Z 轴的蓝色箭头也会改变方向,此向量也会改变。
  • 要在沿Z轴移动物体时,忽略其自身旋转角度,请使用Vector3.forward
  • 如果获取物体的后矢量值,只需要-Vector3.forward即可。

right

public Vector3 forward { get; set; }

物体的右矢量

  • 标准化矢量,它表示物体世界空间中变换的红轴,也就是x轴。
  • Vector3.right不同,transform.right在移动GameObject的同时,还考虑其自身旋转角度,此向量的坐标体系为世界坐标。
  • 要在沿X轴移动物体时,忽略其自身旋转角度,请使用Vector3.right
  • 如果获取物体的左矢量值,只需要-Vector3.right即可。

up

public Vector3 up { get; set; }

物体的上矢量

  • 标准化矢量,它表示物体世界空间中变换的绿轴,也就是y轴。
  • Vector3.up 不同,transform.up 在移动GameObject的同时,还考虑其自身旋转角度,此向量的坐标体系为世界坐标。
  • 要在沿Y轴移动物体时,忽略其自身旋转角度,请使用Vector3.up
  • 如果获取物体的下矢量值,只需要-Vector3.up即可。

hasChanged

public bool hasChanged { get; set; }

自上次将标志设置为“false”以来,是否发生Transform更改?

  • 可以是任何能够导致重新计算其矩阵的操作:对其位置、旋转或缩放的任意调整都会设置为true
  • 不会实际检查旧值和新值是否不同,因此如果使用需要我们自己将其设置为false

hierarchyCapacity

public int hierarchyCapacity { get; set; }

Transform层级视图数据结构的Transform容量,包含自身。

  • 当其中的变换数量超过其容量时,将调整该数据结构的大小。
  • 将容量设置为略大于最大预期大小的值可减少内存使用量,并提高超大层级视图的 Transform.SetParent 和 Object.Destroy 的性能。

hierarchyCount

public int hierarchyCount { get; }

Transform的层级视图数据结构中Transform的数量,包含自身。

localEulerAngles

public Vector3 localEulerAngles { get; set; }

以欧拉角表示的相对于父物体变换旋转的旋转角度(以度为单位)。

  • 表示相对于父物体的相对空间中的旋转。在属性面板中查看GameObject的旋转时,会看到与此属性相同的角度值。如获取世界空间的角度值请使用Transform.eulerAngles
  • 物体的旋转不是以欧拉角进行存储,而是以四元数作为存储
  • 通过变换this.eulerAngles的方式进行旋转,可能会造成问题,可以使用四元数乘积transform.rotation *= Quaternion.AngleAxis(angle* Time.deltaTime, Vector3.up);的方式。也可以不依赖于读取 Quanternion.eulerAngles 来增加旋转,而是定义变量Vector3 currentEulerAngles; 来设置它。所有旋转更改都在currentEulerAngles 变量中进行,这些更改后应用于四元数transform.localEulerAngles = currentEulerAngles += Vector3.up * Time.deltaTime * 10f;

localPosition

public Vector3 localPosition { get; set; }

相对于父物体的位置。

  • 如果没有父物体,则其与transform.position 相同。
  • 父物体们的旋转和缩放将影响世界位置,但是不会影响localPosition数值,transform.position中的 1 个单位始终为 1 个单位,但transform.localPosition中的1个单位将按所有父物体的缩放进行缩放。如:子物体localPosition(1,0,0)时,父物体的localScale(10,1,1)时,子物体的localPosition仍为(1,0,0),而position(10,1,1)

position

public Vector3 position { get; set; }

世界空间中的物体位置。

  • 获得该值可在3D世界空间中定位此GameObject。
  • 父物体们的旋转和缩放将影响子物体世界位置,影响子物体的position值。

localRotation

public Quaternion localRotation { get; set; }

相对于父物体的旋转角度。

  • 用四元数进行存储。
  • 可以通过 Transform.Rotate进行旋转。
  • 可以通过Transform.localEulerAngles获取欧拉角度。

rotation

public Quaternion rotation { get; set; }

世界空间的旋转角度。

  • 用四元数进行存储。
  • 当父节点们选择发生变化,会影响此物体的rotation值。
  • transform.rotation 没有万向锁的困扰。
  • 当对此值Quaternion.Lerp()时,可能会因为角度差距过大而造成错误的角度差值。可以采用欧拉角进行Lerp。yAngle = Mathf.Lerp(yAngle ,0, 20* Time.deltaTime);然后在对其设置transform.rotation = Quaternion.Euler(0, Focus.eulerAngles.y + yAngle , 0);

localScale

public Vector3 localScale { get; set; }

相对于父物体的缩放比例。

  • 此值会影响子物体的世界位置transform.position
  • 如果想获取物体的世界缩放比例请使用transform.lossyScale

lossyScale

public Vector3 lossyScale { get; }

物体的全局缩放比例。

  • 如果有一个具有缩放的父变换和一个任意旋转的子项,则缩放将出现偏斜。 因此,无法使用3维矢量正确地表示缩放,而只能使用 3x3 矩阵进行表示。 但是,这种表示方式非常不便于使用。 lossyScale 是一个便捷属性,它尽可能地匹配实际的世界缩放。 如果您的对象未出现偏斜,则该值将完全正确; 即使其偏斜,大多数时候该值也不会出现大幅变动

parent

public Transform parent { get; set; }

获取或设置物体的父级

  • 更改父级将修改相对于父级的位置、缩放和旋转,保持与世界空间位置、旋转和缩放不变,但对象发生图形偏斜状态则会发生变化或出现偏斜现象。
  • 若要设置parent请使用transform.SetParent(parent)

root

public Transform root { get; }

返回物体的根对象。

  • 如果该变换没有父级,则返回自身。

localToWorldMatrix

public Matrix4x4 localToWorldMatrix { get; }

从本地空间到世界空间的矩阵数据

  • 此数据为4x4 矩阵
  • 不熟悉使用矩阵进行坐标变换,请改用Transform.TransformPoint
  • 若要设置shader矩阵参数,则必须使用用Renderer.localToWorldMatrix
  • 矩阵详情 [Unity数学及图形学数学基础]篇幅

worldToLocalMatrix

public Matrix4x4 worldToLocalMatrix { get; }

从世界空间到本地空间的矩阵数据

  • 此数据为4x4 矩阵
  • 不熟悉使用矩阵进行坐标变换,请改用Transform.InverseTransformPoint
  • 若要设置shader矩阵参数,则必须使用用Renderer.worldToLocalMatrix
  • 矩阵详情 [Unity数学及图形学数学基础]篇幅

公共函数

DetachChildren

public void DetachChildren();

清除所有子物体。

  • 等同于child.SetParent(null)效果。

Find

public Transform Find(string n);

查找子物体。

  • 如果未找到则返回null
  • 查找会向下递归查找。
  • 可以找到非活跃的子物体。
  • 如果n包含"/"字符,则会向路径名称那样查找这个物体。

GetChild

public Transform GetChild(int index);

通过索引位置获取子物体。

  • 如果没有子物体,或者index参数的值大于子物体数,则会发生错误。子项数可以通过childCount获取。

GetSiblingIndex

public int GetSiblingIndex();

获取当前的自身的同级索引。

  • 索引号从0开始。

IsChildOf

public bool IsChildOf([NotNull("ArgumentNullException")] Transform parent);

是否为parent的子物体。

  • 如果自身是parent的深层子物体,也会返回true

SetParent

public void SetParent(Transform p);
public void SetParent(Transform parent, bool worldPositionStays);

设置父物体为parent

  • worldPositionStays = true则修改相对于父级的位置、缩放和旋转,使对象保持与之前相同的世界空间位置、旋转和缩放。此为默认值。
  • worldPositionStays = false 会以本地相对数据作用于GameObject新的父级中。

SetAsFirstSibling

public void SetAsFirstSibling();

设置当前子物体到第一位

  • 等同于transform.SetSiblingIndex(0)效果。
  • 索引更改后,在Hierarchy窗口中的顺序也会改变。同时UGUI中的AutoLayout也会重新布局。

SetAsLastSibling

public void SetAsLastSibling();

设置当前子物体到最后一位

  • 等同于transform.SetSiblingIndex(parent.childCount-1)效果。
  • 索引更改后,在Hierarchy窗口中的顺序也会改变。同时UGUI中的AutoLayout相关组件也会重新布局。

SetSiblingIndex

public void SetSiblingIndex(int index);

设置当前的自身的同级索引。

  • 可以通过transform.GetSiblingIndex()获取当前索引。
  • 索引更改后,在Hierarchy窗口中的顺序也会改变。同时UGUI中的AutoLayout相关组件也会重新布局。

LookAt

public void LookAt(Transform target, [DefaultValue("Vector3.up")] Vector3 worldUp);
public void LookAt(Vector3 worldPosition, [DefaultValue("Vector3.up")] Vector3 worldUp);
public void LookAt(Vector3 worldPosition);
public void LookAt(Transform target);

使前矢量或使用worldUp指定矢量,指向target的当前位置。

Translate

public void Translate(float x, float y, float z);
public void Translate(float x, float y, float z, [DefaultValue("Space.Self")] Space relativeTo);
public void Translate(Vector3 translation);
public void Translate(Vector3 translation, [DefaultValue("Space.Self")] Space relativeTo);
public void Translate(float x, float y, float z, Transform relativeTo);
public void Translate(Vector3 translation, Transform relativeTo);

根据translation 向量的方向和距离或者x,y,z的向量轴移动变换。

  • relativeTo为 Space.World,则相对于世界坐标系应用移动变换。默认为Space.Self

Rotate

public void Rotate(float xAngle, float yAngle, float zAngle);
public void Rotate(Vector3 eulers, [DefaultValue("Space.Self")] Space relativeTo);
public void Rotate(Vector3 eulers);
public void Rotate(float xAngle, float yAngle, float zAngle, [DefaultValue("Space.Self")] Space relativeTo);
public void Rotate(Vector3 axis, float angle, [DefaultValue("Space.Self")] Space relativeTo);
public void Rotate(Vector3 axis, float angle);

使用欧拉角对物体进行旋转变换。

  • relativeTo为 Space.World,则相对于世界坐标系应用旋转变换。默认为Space.Self`。

RotateAround

public void RotateAround(Vector3 point, Vector3 axis, float angle);

围绕一个点进行旋转。

  • 旋转过程中会以开始时的朝向作为朝向,旋转并朝向目标点。

SetPositionAndRotation

public void SetPositionAndRotation(Vector3 position, Quaternion rotation);

设置位置和旋转值

  • 此设置为世界空间下,需要设置本地空间下,可以使用TransformDirectionTransformPoint进行转换。
  • 当你需要同时设置物体的transform.positiontransform.rotation时,强烈推荐此方法。

InverseTransformDirection

public Vector3 InverseTransformDirection(Vector3 direction);
Vector3 InverseTransformDirection(float x, float y, float z);

direction从世界空间变换到本地空间。与Transform.TransformDirection相反。

  • 如果表示空间中的位置而不是方向,则应使用Transform.InverseTransformPoint

TransformDirection

public Vector3 TransformDirection(float x, float y, float z);;
public Vector3 TransformDirection(Vector3 direction);;

direction从本地空间变换到世界空间。

  • 操作不受变换的缩放或位置的影响。 返回的向量与direction的长度相同。
  • 如果表示位置而不是方向,则应使用Transform.TransformPoint进行转换。

InverseTransformPoint

Vector3 InverseTransformPoint(float x, float y, float z);
Vector3 InverseTransformPoint(Vector3 position);;

position从世界空间变换到本地空间。

  • 该函数与Transform.TransformPoint相反。
  • 返回的位置受缩放影响。如果要处理向量而不是位置,请使用Transform.InverseTransformDirection

TransformPoint

public Vector3 TransformPoint(float x, float y, float z);;
public Vector3 TransformPoint(Vector3 position);;

position从本地空间变换到世界空间。

  • 返回的位置受缩放影响。如果要处理向量,请使用Transform.TransformDirection
  • 可以使用Transform.InverseTransformPoint从世界空间到本地空间的转换

InverseTransformVector

public Vector3 InverseTransformVector(Vector3 vector);;
public Vector3 InverseTransformVector(float x, float y, float z);;

vector向量从世界空间变换到本地空间。

  • Transform.TransformVector相反。
  • 该操作受缩放影响。

TransformVector

public Vector3 TransformVector(float x, float y, float z);
public Vector3 TransformVector(Vector3 vector);

vector向量从本地空间变换到世界空间。

  • 不受变换的位置影响,但受缩放影响。 返回的矢量可能具有与vector不同的长度。
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值