如图:简介明了的理清了各个东东之间的关系
scene&gameobject&Component.png如图:在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);