如圖:簡(jiǎn)介明了的理清了各個(gè)東東之間的關(guān)系
如圖:在Unity里面Root為UnityEngine.Object;但是如果再往底部走,就是C#的Root:System.Object.
GameObject和Component都是繼承于UnityEngine.Object蓝撇,所有有共同的屬性和方法。
屬性里面重要點(diǎn)的就是:name(名字)
GameObject.name 游戲物體的名字
Component.name 該組件所在游戲物體的名字
Object的靜態(tài)方法
1.Object.Destory 銷毀某個(gè)組件或游戲物體
// Kills the game object
Destroy(gameObject);
// Removes this script instance from the game object
Destroy(this);
// Removes the rigidbody from the game object
Destroy(rigidbody);
// Kills the game object in 5 seconds after loading the object
Destroy(gameObject, 5);
2.Object.DestroyImmediate 立即銷毀組件或游戲物體
Destroys the object obj immediately. You are strongly recommended to use Destroy instead.(推薦使用Object.Destory)
區(qū)別:Object.Destory 的銷毀是將很多需要銷毀的東西放到同一個(gè)銷毀池中進(jìn)行集中銷毀陈莽,而Object.DestroyImmediate是立刻馬上銷毀唉地。
3.Object.DontDestroyOnLoad 正在加載(切換)場(chǎng)景時(shí)不要銷毀
當(dāng)一個(gè)場(chǎng)景中的某個(gè)游戲物體需要存活到下個(gè)場(chǎng)景,當(dāng)切換場(chǎng)景時(shí)传透,就需要對(duì)其進(jìn)行執(zhí)行該方法。(設(shè)置共享的游戲物體)
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
void Awake() {
DontDestroyOnLoad(transform.gameObject);
}
}
4.Object.FindObjectOfType 根據(jù)組件類型去查找組件
返回找到的第一個(gè)符合條件的組件(返回值為一個(gè)Object)
5.Object.FindObjectsOfType 根據(jù)組件類型去查找組件
返回找到的所有符合條件的組件(返回值為一個(gè)Object數(shù)組)
6.Object.Instantiate 實(shí)例化
public static Object Instantiate(Object original);
public static Object Instantiate(Object original, Transform parent);
public static Object Instantiate(Object original, Transform parent, bool instantiateInWorldSpace);
public static Object Instantiate(Object original, Vector3 position, Quaternion rotation);
public static Object Instantiate(Object original, Vector3 position, Quaternion rotation, Transform parent);