顯示具有 Transform 標籤的文章。 顯示所有文章
顯示具有 Transform 標籤的文章。 顯示所有文章

2016年1月3日 星期日

Unity開發筆記 - GameObject & Transform 的操作

Script 中的物件參考

在 Script 中,「gameObject」關鍵字代表了掛載這個 Script 元件的 GameObject;而「transform」關鍵字則代表該 GameObject 所掛載的 Transform 元件,亦可透過「gameObject.transform」來取得 GameObject 掛載的 transform 元件。

GameObject
├Transform
├Script (MonoBehaviour)
├.....
├.....
├.....


物件的名稱及標籤

// 取得名稱
string Name = gameObject.name;

// 取得標籤
string Tag = gameObject.tag;

物件的座標

// 取得世界座標
Vector3 Pos = gameObject.transform.position;

// 設置世界座標 為 (x, y, z)
gameObject.transform.position = new Vector3(x, y, z);

// 設置世界座標 為 (0, 0, 0)
gameObject.transform.position = Vector3.zero;

// 取得本地座標
Vector3 Pos = gameObject.transform.localPosition;

// 設置本地座標 為 (x, y, z)
gameObject.transform.localPosition = new Vector3(x, y, z);

// 設置本地座標 為 (0, 0, 0)
gameObject.transform.localPosition = Vector3.zero;

物件的旋轉角度

// 取得世界旋轉角度
Vector3 Rot = gameObject.transform.rotation.eulerAngles;

// 設置世界旋轉角度 為 (x, y, z) Ex-1
gameObject.transform.rotation.eulerAngles = new Vector3(x, y, z);
// 設置世界旋轉角度 為 (x, y, z) Ex-2
gameObject.transform.rotation = Quaternion.Euler (new Vector3(x, y, z));

// 設置世界旋轉角度 為 (0, 0, 0) Ex-1
gameObject.transform.rotation.eulerAngles = Vector3.zero;
// 設置世界旋轉角度 為 (0, 0, 0) Ex-2
gameObject.transform.rotation = Quaternion.Euler (new Vector3(0, 0, 0));

// 取得本地旋轉角度
Vector3 Rot = gameObject.transform.localRotation.eulerAngles;

// 設置本地旋轉角度 為 (x, y, z) Ex-1
gameObject.transform.localRotation.eulerAngles = new Vector3(x, y, z);
transform.localRotation.eulerAngles= new Vector3(x, y, z);
// 設置本地旋轉角度 為 (x, y, z) Ex-2
gameObject.transform.localRotation = Quaternion.Euler (new Vector3(x, y, z));

transform.localRotation = Quaternion.Euler (new Vector3(x, y, z));

// 設置本地旋轉角度 為 (0, 0, 0) Ex-1
gameObject.transform.localRotation.eulerAngles= Vector3.zero;
// 設置本地旋轉角度 為 (0, 0, 0) Ex-2
gameObject.transform.localRotation = Quaternion.Euler (new Vector3(0, 0, 0));

物件的縮放尺寸

// 取得本地縮放尺寸
Vector3 Sca = gameObject.transform.localScale;

// 設置本地縮放尺寸為 (x, y, z)
gameObject.transform.localScale = new Vector3(x, y, z);

// 設置本地縮放尺寸為 (0, 0, 0)
gameObject.transform.localScale = Vector3.zero;

物件操作

// 取得子物件數量
int ChildCount = gameObject.transform.childCount;

// 以子物件的索引位置取得子物件(GameObject)
int ChildIndex = 0;
GameObject Child = gameObject.transform.getChild( ChildIndex ).gameObject;

// 取得父物件(GameObject)
GameObject Parent = gameObject.transform.parent.gameObject;

// 設置父物件(GameObject)
GameObject NewParent = new GameObject();
gameObject.transform.SetParent( NewParent.transfrom );

// 取得指定名稱的物件
string ObjectName = "SomeGameObject";
GameObject SearchObject = GameObject.Find( ObjectName );

// 毀滅指定物件
GameObject SomeObject = new GameObject();
GameObject.Destroy( SomaObject );

// 場景載入時不毀滅指定物件
GameObject SomeObject = new GameObject();
GameObject.DontDestoryOnLoad( SomeObject );

// 複製物件
GameObject SomeObject = new GameObject();
GameObject CloneObject = GameObject.Instantiate( SomeObject );

// 取得物件的掛載元件 Component - Collider
Collider currentCollider = gameObject.GetComponent<Collider>();

// 取得物件的掛載元件 Component - Renderer
Renderer currentRenderer = gameObject.GetComponent<Renderer>();

2016年1月2日 星期六

Unity開發筆記 - GameObject & Transform 的關係

Unity 在腳本撰寫時,最初會碰上的大概就是 GameObject 和 Transform 這兩個物件了。雖然過去曾經參與過幾次 Unity 開發的研習,但我在實際接觸開發時,卻一直以為這是兩個各自獨立的物件。

以類別而言,當然必定是兩兩獨立的,後來才想通,每個 GameObject 在場景中,必定存在某個「位置(Position)」,而 GameObject 這個物件本身並沒有提供這個屬性來存取,因為「位置」這個屬性是被定義在 Transform 類別中的。

而若以「位置」為基礎反推回去,當然必定是用來表示某個座標,而這樣用來表示「位置」的屬性,除非是用在做相關的算術運算,不然以「位置」來思考,它作為座標的表示,確實必須存在一個物體讓「位置」這個屬性去表示它的位置。


就上述的邏輯而言,當一個物體(GameObject)被置於場景(Scene)中時,它必定存在於某個「位置」上,不然你怎麼知道如何在畫面上找到它呢?


所以在這樣的思維上,一個 GameObject 便代表了一個場景上的實體(可見的顯示物件),即便它是Empty GameObject,你也能清楚見到它位於什麼位置。


因此若從不同層面來看,GameObject 和 Transform 的確是各自獨立卻又相互依存的。單從類別層面來看,兩個是不同類別表現了不同的資訊:若從顯示物件的層面而論,彼此卻又是不能拆解分離的。


但由於這樣的特性,可能有人一樣曾在物件存取時會被誤導,因為GameObject 並不能「直接存取」用來表示它「位置」的 Position 這個屬性,甚至是 Rotation 或 Scale,因為Position、Rotation 及 Scale這三個屬性其實是在 Transform 類別中才定義的,GameObject並不存在這三個屬性,只能透過對應的 Transform 去存取該 GameObject 的 Position、Rotation 及 Scale。


小結:


GameObject 在場景(Scene)中,僅在三維空間中代表一個物體,並存在名稱Name)及標籤(Tag)等屬性,且在程式碼中可透過靜態方法來取得對應名稱的 GameObject 物件。

Transform 這個物件在 Unity 中較像是一個元件(Component),一個被掛載在 GameObject 上的元件,用以表示與提供存取該 GameObject 的位置(Position)、旋轉角度(Rotation)及Scale(縮放尺寸)等相關的資訊。

當在場景上新增一個GameObject時,該 GameObject 便會自動附加上Transfrom 這個元件(Component),與其它元件不同不須手動掛載上該元件
,而其他類型的元件則須自行手動附加,如碰撞器(Collider)等元件。

需要特別注意一點,GameObject 的子物件,亦必須透過 Transform 來存取,Transform 提供 childCount 屬性及 GetChild() 方法來取得子物件的數量及指定索引位置的子物件。