如圖:簡介明了的理清了各個東東之間的關系
如圖:在Unity里面Root為UnityEngine.Object;但是如果再往底部走,就是C#的Root:System.Object.
GameObject和Component都是繼承于UnityEngine.Object,所有有共同的屬性和方法。
屬性里面重要點的就是:name(名字)
GameObject.name 游戲物體的名字
Component.name 該組件所在游戲物體的名字
Object的靜態方法
1.Object.Destory 銷毀某個組件或游戲物體
// 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)
區別:Object.Destory 的銷毀是將很多需要銷毀的東西放到同一個銷毀池中進行集中銷毀,而Object.DestroyImmediate是立刻馬上銷毀。
3.Object.DontDestroyOnLoad 正在加載(切換)場景時不要銷毀
當一個場景中的某個游戲物體需要存活到下個場景,當切換場景時,就需要對其進行執行該方法。(設置共享的游戲物體)
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
void Awake() {
DontDestroyOnLoad(transform.gameObject);
}
}
4.Object.FindObjectOfType 根據組件類型去查找組件
返回找到的第一個符合條件的組件(返回值為一個Object)
5.Object.FindObjectsOfType 根據組件類型去查找組件
返回找到的所有符合條件的組件(返回值為一個Object數組)
6.Object.Instantiate 實例化
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);