變換(Transform)組件肯定場景中每一個對象的「位置(Position)」、「旋轉(Rotation)」和「縮放(Scale)」。數據結構
每個遊戲對象(GameObject)都有一個變換(Transform)組件。函數
位置:變換組件在X、Y、Z軸上的位置(後面將解釋爲何不說是物體的軸上的位置信息)性能
旋轉:變換組件繞X、Y、Z軸所旋轉的度數spa
縮放:變換組件沿X、Y、Z軸上的縮放,值爲1時則爲原始大小(被導入或建立時物體的大小)code
注意:變換的位置、旋轉以及縮放是相對於該物體的(變換組件的)父級測量的。當沒有父級時則在世界座標下顯示其屬性。orm
(注:代碼塊上方是屬性或方法的做用域、數據類型和訪問權限(get/set),代碼塊中是具體的使用方法)對象
public int childCount { get; }索引
transform.childCount
物體的(變換組件的)子對象的數目(不包括自身)遊戲
public Vector3 eulerAngles { get; set; }ci
transform.eulerAngles
物體旋轉屬性的以度爲單位的歐拉角(即與變換組件中旋轉的三個量相對應)
public Vector3 forward { get; set; }
transform.forward
世界空間中,物體的(變換組件的)z軸
應當注意當物體旋轉角度後,其自身的座標軸也隨之變換,使用「transform.forward」能夠獲取物體當前自身座標系(Local而非Global)的z軸
public bool hasChanged { get; set; }
transform.hasChanged
自從上一次"hasChanged"被設爲false後,變換組件是否發生了變化?
應注意其默認值爲true。所以需手動設置爲false後進行檢測。當其改變爲true後,其真值不會自身改變爲false
public int hierarchyCapacity { get; set; }
transform.hierarchyCapacity
變換在層級中數據結構的變換能力
即目前該物體在Hierarchy窗口中對應的樹的可容納最大結點數
應注意其數值會隨着結點數的增長而擴容,所以對於須要頻繁增長子物體的物體,能夠事先設置該值比預計的最大值高一點,以免反覆修改此值形成的性能下降
public int hierarchyCount { get; }
transform.hierarchyCount
變化在層級中數據結構的所包含的變換組件數目
即物體及其子物體所包含的變換組件的總數目
public Vector3 localEulerAngles { get; set; }
transform.localEulerAngles
物體的相對於父級所旋轉的歐拉角
如需設置請注意使用正值且不要超過360度
public Vector3 localPosition { get; set; }
transform.localPosition
物體相對於父級的位置,沒有父級則與世界座標下一致
public Quaternion localRotation { get; set; }
transform.localRotation
物體相對於父級的旋轉屬性
public Vector3 localScale { get; set; }
transform.localScale
物體相對於父級的縮放屬性
public Matrix4x4 localToWorldMatrix { get; }
transform.localToWorldMatrix
將點從局部空間轉換爲世界空間的矩陣
public Vector3 lossyScale { get; }
transform.lossyScale
世界座標系下物體的縮放屬性
注意當物體的父級進行了縮放而且該物體旋轉後,該值再也不準確
public Transform parent { get; set; }
transform.parent
物體的父級的變換屬性
public Vector3 position { get; set; }
transform.position
物體在世界座標系下的位置屬性
public Vector3 right { get; set; }
transform.right
世界空間中,物體的(變換組件的)x軸
應當注意當物體旋轉角度後,其自身的座標軸也隨之變換,使用「transform.right」能夠獲取物體當前自身座標系(Local而非Global)的x軸
public Transform root { get; }
transform.root
返回物體所在層級結構的根的變換屬性,沒有父級則爲自身
public Quaternion rotation { get; set; }
transform.rotation
物體在世界座標系下的變換屬性,以四元數形式存儲
注意它並不與變換組件中對應的旋轉的三個屬性對應(可使用transform.eulerAngles進行修改)
它的值小於180度
public Vector3 up { get; set; }
transform.up
世界空間中,物體的(變換組件的)y軸
應當注意當物體旋轉角度後,其自身的座標軸也隨之變換,使用「transform.right」能夠獲取物體當前自身座標系(Local而非Global)的y軸
public Matrix4x4 worldToLocalMatrix { get; }
transform.worldToLocalMatrix
將點從世界空間轉換爲局部空間的矩陣
public void DetachChildren();
transform.DetachChildren()
將物體的全部子對象都與其取消父子關係
public Transform Find(string n);
參數
string n:要找到的子物體的名字
返回值
若是找到了該孩子,則返回其變換組件,不然返回null
transform.Find("ChildName"); transform.Find("ChildName/GrandChildName");
經過名字n找到物體的子對象的變換組件
注意以上用法只適用於子物體而不適用與孫物體或更多層級下的物體查找
如需查找更低層級的物體,能夠像路徑名同樣進行查找,如第二行所示
public Transform GetChild(int index);
參數
int index:子物體對應的索引
返回值
索引對應的子物體的變換組件
transform.GetChild(0);//找到第一個子物體變換組件 transform.GetChild(0).GetChild(0);//找到第一個子物體的第一個子物體的變換組件
子物體的索引在層級窗口從上至下由0開始遞增
public int GetSiblingIndex();
返回值
子物體相對於父物體的索引值
transform.GetSiblingIndex();
獲取物體在層級窗口下的索引,便是其父物體的從上之下的第幾個物體(從0開始)
注意無父物體時,則會在層級窗口最外層從上至下由0開始排列
public Vector3 InverseTransformDirection(Vector3 direction);
參數
Vector3 direction:表示某個方向的三維向量
返回值
物體自身座標系下direction的方向向量
transform.InverseTransformDirection(Vector3.forward);//將世界座標系下的z軸方向轉換爲物體自身座標系下的方向
將direction由世界座標系轉換爲物體的自身座標系下的向量表示
public Vector3 InverseTransformDirection(float x, float y, float z);
參數
float x, float y, float z:表示方向的三個軸上的參數
返回值
物體自身座標系下(x,y,z)的方向向量
transform.InverseTransformDirection(0, 0, 1);
將(x,y,z)由世界座標系轉換爲物體的自身座標系下的向量表示
public Vector3 InverseTransformPoint(Vector3 position);
參數
Vector3 position:表示某個點(位置)的三維向量
返回值
物體自身座標系下position的位置向量
transform.InverseTransformPoint(Camera.main.transform.position);//將世界座標系下的主相機的位置轉換爲物體自身座標系下的位置(即與相機的相對位置)
將position由世界座標系轉換爲物體的自身座標系下的向量表示
public Vector3 InverseTransformPoint(float x, float y, float z);
參數
float x, float y, float z:表示某個點(位置)的三個軸上的參數
返回值
物體自身座標系下(x,y,z)的位置向量
transform.InverseTransformPoint((0,0,0));
將(x,y,z)由世界座標系轉換爲物體的自身座標系下的向量表示
注意這個函數會受到縮放的影響
public Vector3 InverseTransformVector(Vector3 vector);
參數
Vector3 vector:某個向量
返回值
物體自身座標系下某個向量的表示
transform.InverseTransformVector(Vector3.forward);//將世界座標系下的z軸方向轉換爲物體自身座標系下的方向
將vector由世界座標系轉換爲物體的自身座標系下的向量
public Vector3 InverseTransformVector(float x, float y, float z);
參數
float x, float y, float z:表示某個向量的三個軸上的參數
返回值
物體自身座標系下(x,y,z)的轉換後的向量
transform.InverseTransformVector((0,0,0));
將(x,y,z)由世界座標系轉換爲物體的自身座標系下的向量
注意這個函數會受到縮放的影響
public bool IsChildOf([NotNull] Transform parent);
參數
[NotNull] Transform parent:父對象的變換屬性,空值時會提示錯誤
返回值
若該對象是parent的子對象或更深層次的對象,則返回true;不然返回false
transform.IsChildOf(parent.transform);
判斷一個物體是不是parent的子物體或更深層的物體
public void LookAt(Transform target, [DefaultValue("Vector3.up")] Vector3 worldUp);
參數
Transform target:物體所看向的變換屬性
[DefaultValue("Vector3.up")] Vector3 worldUp:所指定的向上的向量,默認值爲Vector3.up,即(0,1,0)
transform.LookAt(target.transform); transform.LookAt(target.transform,Vector3.down);
旋轉變換組件,使得指向前方的向量指向目標位置
肯定向前的向量(藍軸)後,須要肯定繞藍軸旋轉的角度,這裏使用一個表示物體上方向的向量來約束,注意這個參數是非強制性而是以引導的做用旋轉變換組件(優先確保先指向目標位置)
public void LookAt(Vector3 worldPosition, [DefaultValue("Vector3.up")] Vector3 worldUp);
參數
Vector3 worldPosition:物體所看向的位置
[DefaultValue("Vector3.up")] Vector3 worldUp:所指定的向上的向量,默認值爲Vector3.up,即(0,1,0)
transform.LookAt(target.transform.position); transform.LookAt(target.transform.position,Vector3.down);
此重載函數與上方相似,再也不贅述
public void Rotate(Vector3 eulers, [DefaultValue("Space.Self")] Space relativeTo);
參數
Vector3 eulers:旋轉所繞的表示軸的向量(在世界座標系下;考慮模長)
[DefaultValue("Space.Self")] Space relativeTo:確認旋轉軸是在世界座標下仍是自身座標下,默認值是在自身座標系下
transform.Rotate(Vector3.up,Space.World);//繞世界座標系下的(0,1,0)方向,每幀旋轉1度
繞eulers對應的軸進行旋轉,relativeTo肯定在世界仍是自身座標系下
public void Rotate(float xAngle, float yAngle, float zAngle, [DefaultValue("Space.Self")] Space relativeTo);
參數
float xAngle, float yAngle, float zAngle:旋轉所繞向量的三個軸上的信息(在世界座標系下;考慮模長)
[DefaultValue("Space.Self")] Space relativeTo:確認旋轉軸是在世界座標下仍是自身座標下,默認值是在自身座標系下
transform.Rotate(0,5,0,Space.World);//繞世界座標系下的(0,1,0)方向,每幀旋轉5度
繞(x,y,z)對應的軸進行旋轉,relativeTo肯定在世界仍是自身座標系下
public void Rotate(Vector3 axis, float angle, [DefaultValue("Space.Self")] Space relativeTo);
參數
Vector3 axis:所旋轉的軸(不考慮模長)
float angle:每幀旋轉的角度
[DefaultValue("Space.Self")] Space relativeTo:確認旋轉軸是在世界座標下仍是自身座標下,默認值是在自身座標系下
transform.Rotate(Vector3.up * 5, 1, Space.World);//繞世界座標系下的(0,1,0)方向,每幀旋轉1度
以給定角度定義的度數旋轉對象
public void RotateAround(Vector3 point, Vector3 axis, float angle);
參數
Vector3 point:旋轉的中心點
Vector3 axis:旋轉軸
float angle:每幀旋轉角度
transform.RotateAround(Vector3.zero, Vector3.up, 1);//繞世界座標原點,(0,1,0)的方向,每幀1度的速度進行旋轉
public void SetAsFirstSibling();
transform.SetAsFirstSibling();
將物體移動至所在層級的首位
public void SetAsLastSibling();
transform.SetAsLastSibling();
將物體移動至所在層級的末位
public void SetParent(Transform p);
public void SetParent(Transform parent, bool worldPositionStays);
參數
Transform p/Transform parent:父對象的變換屬性
bool worldPositionStays:父對象變換屬性是否保持原來不變
transform.SetParent(parent.transform); transform.SetParent(parent.transform, false);
設置物體的父對象
當worldPositionStays設爲false時,設置父子關係以後,子對象的transform組件保持與未創建關係以前一致;不然物體的在世界座標下的位置保持不變
public void SetPositionAndRotation(Vector3 position, Quaternion rotation);
transform.SetPositionAndRotation(Vector3.zero, Quaternion.Euler(Vector3.up));//將物體設置爲座標原點,旋轉角度設爲(0,1,0)
設置物體在世界座標下的位置與旋轉
public void SetSiblingIndex(int index);
參數
int index:要設置的索引
transform.SetSiblingIndex(1);
設置物體所在在層級中的索引,同時會真正改變層級窗口中的位置
public Vector3 TransformDirection(Vector3 direction);
參數
Vector3 direction:表示某個方向的三維向量
返回值
世界座標系下direction的方向向量
transform.TransformDirection(Vector3.forward);//將自身座標系下的z軸方向轉換爲世界座標系下的方向
將direction由自身座標系轉換爲世界座標系下的向量表示
public Vector3 TransformDirection(float x, float y, float z);
參數
float x, float y, float z:表示方向的三個軸上的參數
返回值
世界座標系下(x,y,z)的方向向量
transform.TransformDirection(0, 0, 1);
將(x,y,z)由自身座標系轉換爲世界座標系下的向量表示
public Vector3 TransformPoint(Vector3 position);
參數
Vector3 position:表示某個點(位置)的三維向量
返回值
世界座標系下position的位置向量
transform.TransformPoint(Camera.main.transform.position);//將物體座標系下的主相機的位置轉換爲座標系下的位置
將position由自身座標系轉換爲世界座標系下的向量表示
public Vector3 InverseTransformPoint(float x, float y, float z);
參數
float x, float y, float z:表示某個點(位置)的三個軸上的參數
返回值
世界座標系下(x,y,z)的位置向量
transform.TransformPoint((0,0,0));
將(x,y,z)由自身座標系轉換爲世界座標系下的向量表示
注意這個函數會受到縮放的影響
public Vector3 TransformVector(Vector3 vector);
參數
Vector3 vector:某個向量
返回值
世界座標系下某個向量的表示
transform.TransformVector(Vector3.forward);//將物體自身座標系下的z軸方向轉換爲世界座標系下的方向
將vector由物體座標系轉換爲世界的座標系下的向量
public Vector3 TransformVector(float x, float y, float z);
參數
float x, float y, float z:表示某個向量的三個軸上的參數
返回值
世界座標系下(x,y,z)的轉換後的向量
transform.TransformVector((0,0,0));
將(x,y,z)由物體自身座標系轉換爲世界座標系下的向量
注意這個函數會受到縮放的影響
public void Translate(Vector3 translation, [DefaultValue("Space.Self")] Space relativeTo);
參數
Vector3 translation:移動方向(考慮模長)
[DefaultValue("Space.Self")] Space relativeTo:相對自身或世界座標系移動,默認爲自身座標系
transform.Translate(Vector3.forward,Space.World);//沿着世界座標系下的前進方向,按每幀1單位的速度平移物體
沿translation的方向,以Space relativeTo的座標系,根據translation的速度進行平移
public void Translate(float x, float y, float z, [DefaultValue("Space.Self")] Space relativeTo);
參數
float x, float y, float z:移動方向在三個軸上的屬性
[DefaultValue("Space.Self")] Space relativeTo:相對自身或世界座標系移動,默認爲自身座標系
transform.Translate(0, 0, 1,Space.World);
沿(x,y,z)的方向,以Space relativeTo的座標系,(x,y,z)速度進行平移
public void Translate(Vector3 translation, Transform relativeTo);
public void Translate(float x, float y, float z, Transform relativeTo);
參數
Vector3 translation:移動方向(考慮模長)
float x, float y, float z:移動方向在三個軸上的屬性
relativeTo:相對移動的變換屬性
transform.Translate(Vector3.up, Camera.main.transform);//相對相機,向上方進行移動 transform.Translate(0,1,0,Camera.main.transform);//相對相機,向上方進行移動
相對relativeTo的transform屬性進行移動
relativeTo爲空時,則相對世界座標移動