Transform類繼承自Component類,並實現了IEnumberable接口。Transform是GameObject必須擁有的一個組件,用來管理所在GameObject對象的坐標位置、旋轉角度和大小縮放。由於Transform實現了Ienumberable接口,於是可以在程序中使用foreach()方法快速遍歷子物體的Transform結構。即:
void Start()
{
foreach (Transform item in transform)
{
}
}
Transform類實例屬性
eulerAngles屬性:歐拉角
public Vector3 eulerAngles{get; set;}
此功能用於返回和設置GameObject對象的歐拉角。
forward屬性:z軸單位向量
public Vector3 forward{get; set;}
此屬性用於返回或設置transform自身坐標系中z軸方向的單位向量對應的世界坐標系中的單位向量,transform.forward即為transform.TransformDirection(new Vector3(0.0f, 0.0f, 0.0f))的簡化方式。
hasChanged屬性:transform組件是否被修改
public bool hasChanged{get; set;}
此屬性用於判斷GameObject對象上次將此屬性設為false以來,其transform組件的此屬性是否被修改過,默認值為true。
localPosition屬性:局部坐標系為值
public Vector3 localPosition{get; set;}
此屬性用於設置或返回GameObject對象在局部坐標系的位置,若無父級對象則和屬性Transform.positon相同。transform.localPosition的值受父級對象lossyScale的影響。
localToWorldMatrix屬性:轉換矩陣
public Matrix4x4 localToWorldMatrix{get;}
此屬性用於返回從transofrm局部坐標系向世界坐標系轉換的Matrix4x4矩陣。例設A為Vector3,B為Transform實例,且B的x、y、z軸方向與世界坐標系方向一致。
Vector3 vec3 = B.localToWorldMatrix.MultiplyPoint3x4(A);
則分量值vec3.x = B.Position.x + A.x * B.lossyScale.x。
一般情況可用TransformPoint(positon : Vector3)來實現Vector3實例從transform局部坐標系向世界坐標系的轉換。
parent屬性:父物體Transform實例
public Transform parent{get; set;}
此屬性用於返回父物體的Transform實例。若要返回transform的最頂層的父物體,可以使用transform.root屬性。
worldToLocalMatrix屬性:轉換矩陣
public Matrix4x4 worldToLocalMatrix{get;}
此屬性用於返回物體從世界坐標系向transform自身坐標系轉換的Matrix4x4矩陣。
一般可用方法InverseTransformPoint(position : Vector3)來實現Vector3實例從世界坐標系向transform自身坐標系轉換。
Transform類實例方法
DetachChildren方法:分離物體層級關系
public void DetachChidren();
此方法的功能是使GameObject對象的所有子物體和自身分離層級關系。
GetChild方法:獲取GameObject對象子類
public Transform GetChild(int index);
參數為子物體索引值。
此方法用於返回transform的索引值為index的子類Transfrom實例。參數index的值要小於transform的childCount的值。
InverseTransformDirection方法:坐標系轉換
public Vector3 InverseTransformDirection(Vector3 direction);
public Vector3 InverseTransformDirection(float x, float y, float z);
此方法用於將參數direction從世界坐標系轉換到GameObject對象的局部坐標系。
InverseTransfromPoint方法:點的相對坐標向量
public Vector3 InverseTransformPoint(Vector3 position);
public Vector3 InverseTransformPoint(float x, float y ,float z);
此方法用於返回參數position向量相對於GameObject對象局部坐標系的差向量,返回值受transform.lossyScale和transform.rotation影響。
IsChildOf方法:是否為子物體
public bool IsChildOf(Transform parent);
參數parent為父物體的Transform實例。此方法用於判斷transform對應的GameObject對象是否為參數parent的子物體。
LookAt方法:物體朝向
public void LookAt(Transform target);
public void LookAt(Vector3 worldPosition);
public void LookAt(Transform target, Vector3 worldUp);
public void LookAt(Vector3 worldPosition, Vector3 worldUp);
其中參數target為transform自身坐標系指向的目標,參數worldUp為transform自身坐標系中y軸最大限度指向的方向。
此方法的功能為使GameObject對象的z軸指向target,若自定義worldUp的方向,則GameObject對象的forword方向一直指向target,然后transform繞着自身坐標系的z軸即forward方向旋轉到一個使得自身的y軸方向最接近worldUp的地方。
Rotate方法:繞坐標軸或某個向量旋轉
public void Rotate(Vector3 eulerAngles);
public void Rotate(Vector3 eulerAngles, Space relativeTo);
public void Rotate(float xAngle, float yAngle, float zAngle);
public void Rotate(float xAngle, float yAngle, float zAngle, Space relativeTo);
public void Rotate(Vector3 axis, float angle);
public void Rotate(Vector3 axis, float angle, Space relativeTo);
此方法的前四個重載是使得transform實例在相對參數relativeTo的坐標系中旋轉歐拉角eulerAngles,后兩個重載是使得GameObject對象在relativeTo坐標系中繞軸向量axis旋轉angle度。
、
RotateAround方法:繞軸點旋轉
public void RotateAround(Vector3 axis, float angle);
public void RotateAround(Vector3 point, Vector3 axis, float angle);
其中參數point為參考點坐標,參數axis為旋轉軸方向,參數angle為旋轉角度。
TransfromDirection方法:坐標系轉換
public Vector3 TransformDirection(Vector3 direction);
public Vector3 TransformDirection(float x, float y, float z);
此方法用於將向量direction從transform局部坐標系轉換到世界坐標系。
TransformPoint方法:點的世界坐標位置
public Vector3 TransformPoint(Vector3 position);
public Vector3 TransformPoint(float x, float y, float z);
此方法用於返回GameObject對象局部坐標系中position在世界坐標系中的位置。
Translate方法:相對坐標系移動或相對其他物體移動
public void Translate(Vector3 translation);
public void Translate(Vector3 translation, Space relativeTo);
public void Translate(float x, float y, float z);
public void Translate(float x, float y, float z, Space relativeTo);
public void Translate(Vector3 translation, Transform relativeTo);
public void Translate(float x, float y, float z, Transform relativeTo);
此方法前四個重載的功能是使得GameObject對象在relativeTo的坐標系空間中移動參數translation指定的向量,后兩個重載的功能是使得GameObject對象在相對relativeTo的坐標系中移動向量translation。
關於localScale和lossyScale的注解:
當GameObject對象A為GameObject對象B的父物體時,父物體A的各個分量放縮值x、y、z的大小應該保持1:1:1的比例,否則當子物體B的Rotation值比例不為1:1:1時,B物體將會發生變形。
設GameObject對象A為B的父物體,當A物體各個分量的放縮值保持1:1:1的比例時,子物體B的lossyScale返回值即為B物體相對世界坐標系的放縮值,關系為B.localScale = B.lossyScale/A.localScale。
