在Unity场景中所有实体的基类。
最近在研究U3D的API,感觉太多了不好记,所以在此做了个整理。
全文基于Unity官方文档,萌新能力不足,如果有错误,欢迎指出_(:з」∠)_
[info]变量[/info]
activeInHierarchy
public bool activeInHierarchy;
返回一个bool值,判断游戏物体是否在程序运行时被激活。
如果父物体被禁用,那么无论面板中是否勾选上,都将返回false。
activeSelf
public bool activeSelf;【只读】
返回一个bool值,判断物体在本地活跃状态,不考虑父物体的状态。
即使返回true,如果父物体没有被启用,那么该物体也不会起作用。
isStatic
public bool isStatic;
返回一个bool值,判断物体是否为静态。
layer
public int layer;
游戏物体所在的层级,范围在[0,31]。
通常用来使摄像机有选择性的渲染或者忽略射线。
scene
public SceneManagement.Scene scene;
游戏物体所处的场景。
tag
public string tag;
游戏物体的标签。常用来区分游戏物体的类别。
transform
public Transform transform;
游戏物体的transform信息。
name 【继承】【父类:Object】
public string name;
游戏对象的名称。
组件和游戏对象共享的名称。
[info]构造器[/info]
注意:
创建游戏物体至少且必须会有一个Transform组件;
GameObject player;
player = new GameObject("Player");
[info] 实例方法[/info]
CompareTag
public bool CompareTag(string tag);
判断物体的标签是不是[kbd]tag[/kbd]。
if (other.gameObject.CompareTag("Player")) Destroy(other.gameObject);
(怎么感觉用处不大呢_(:з」∠)_ )
GetComponent
此方法用于获取游戏物体中第一个符合Type类型的组件。
public T GetComponent<T>();【常用】
[kbd]T[/kbd] 需要检索的组件类型;public Component GetComponent(Type type);
[kbd]type[/kbd] 需要检索的组件类型;public Component GetComponent(string type);
[kbd]type[/kbd] 需要检索的组件名;Transform transform = GetComponent<Transform>();
使用字符串获取组件的方法可能效率较低,但是适用于从JavaScript中获取C#脚本之类的场合。
GetComponents
public Component[] GetComponents(Type type);
作用同上,不过返回的是组件集合。
注意:
在使用该方法时:
Components[] cjs = GetComponents(typeof(Example)) as Component[]; //正确
不可以这样写:
Components[] cjs = GetComponents(typeof(Example)) as Example[]; //错误
因为Example不是Component,而是其子类,建议使用其他泛型方式。
GetComponentsInChildren()同理。
GetComponentInChildren
获取子物体的组件;
GetComponentsInChildren
获取子物体上的指定组件集合。
public Component[] GetComponentsInChildren(Type type);
public Component[] GetComponentsInChildren(Type type, bool includeInactive);
同GetComponents和GetComponentInChildren;
GetComponentsInParent
public T[] GetComponentsInParent<T>(bool includeInactive);
public Component[] GetComponentsInParent(Type type, bool includeInactive = false);
获取父物体上指定的组件集合,包括父物体的父物体等等。
[kbd]T[/kbd],[kbd]type[/kbd] 需要检索的组件类型; [kbd]includeInactive[/kbd] 是否检索被禁用的组件;另一个重载方法:
List<HingeJoint> hingeJoints = new List<HingeJoint>(); GetComponentsInParent<HingeJoint>(false, hingeJoints);
AddComponent
给一个游戏物体添加个组件。
public T AddComponent<T>(); 【常用】
[kbd]T[/kbd] 需要添加的组件类型;public Component AddComponent(Type componentType);
[kbd]componentType[/kbd] 需要添加的组件类型;Obsolete public Component AddComponent(string className);
[kbd] className[/kbd] 需要添加的组件名;SphereCollider sc = gameObject.AddComponent<SphereCollider>() as SphereCollider;
SphereCollider sc = gameObject.AddComponent(typeof(SphereCollider)) as SphereCollider;
SphereCollider sc = gameObject.AddComponent("SphereCollider") as SphereCollider;
SetActive
public void SetActive(bool value);
激活或禁用该对象。
gameObject.SetActive(false);
一个游戏物体可能由于父物体没有激活而没有被激活,这种情况下,调用SetActive()方法将不会使它生效,但是却可以改变他的本地状态,即可以使用activeSelf查看。此时一旦父物体被激活它就会生效。
SendMessage
public void SendMessage(string methodName, object value = null, SendMessageOptions options = SendMessageOptions.RequireReceiver);
向一个游戏物体发送消息,调用该物体的所有组件中的目标方法;
[kbd]methodName[/kbd] 需要调用的方法的名字; [kbd]value[/kbd] 传递给调用方法的参数,可选; [kbd]options[/kbd] 如果目标游戏物体不存在该方法时是否报错;public class ExampleClass : MonoBehaviour { void ApplyDamage(float damage) { print(damage); } void Example() { gameObject.SendMessage("ApplyDamage", 5.0F); } }
被调用的方法可以选择是否忽略接收参数 [kbd]object parameter[/kbd]和消息设置[kbd]SendMessageOptions options[/kbd] ;
当消息设置为【SendMessageOptions.RequireReceiver】时如果没有一个组件接收到广播的消息的话将会报错。
消息不会发送给未激活的游戏物体(setActive());
SendMessageUpwards
public void SendMessageUpwards(string methodName, object value = null, SendMessageOptionsoptions = SendMessageOptions.RequireReceiver);
向一个游戏物体发送消息,调用该物体和其父物体的所有组件中的目标方法;
其他同上;
BroadcastMessage
public void BroadcastMessage(string methodName, object parameter = null, SendMessageOptions options = SendMessageOptions.RequireReceiver);
广播消息,调用该游戏物体和其所有子物体身上方法名为[kbd]methodName[/kbd]的方法;
public class ExampleClass : MonoBehaviour { void ApplyDamage(float damage) { print(damage); } void Example() { gameObject.BroadcastMessage("ApplyDamage", 5.0F); }
被调用的方法可以选择是否忽略接收参数 [kbd]object parameter[/kbd]和消息设置[kbd]SendMessageOptions options[/kbd] ;
当消息设置为【SendMessageOptions.RequireReceiver】时如果没有一个组件接收到广播的消息的话将会报错。
[info] 静态方法[/info]
CreatePrimitive
Find
public static GameObject Find(string name);
通过[kbd]name[/kbd]查找游戏物体并返回该游戏对象。
public class ExampleClass : MonoBehaviour {
public GameObject hand;
void Example() {
hand = GameObject.Find("Hand");
hand = GameObject.Find("/Hand");
hand = GameObject.Find("/Monster/Arm/Hand");
hand = GameObject.Find("Monster/Arm/Hand");
}
}
这个方法只会查找已启用的游戏对象,如果找不到就返回【null】;
如果name中包含’/’,将会在层级面板中像查看目录那样寻找;
因为比较占用资源,所以不建议每帧都调用Find()方法。可以在Start()总调用,或者使用FIndWithTag来代替;
FindWithTag
通过[kbd]tag[/kbd]标签查找游戏物体并返回该游戏对象。
这个方法只会查找已启用的游戏对象,如果找不到就返回【null】;
在使用标签之前一定要在标签管理器中声明;
FindGameObjectsWithTag
通过[kbd]tag[/kbd]标签查找游戏物体集合。
这个方法只会查找已启用的游戏对象,如果找不到就返回空集合;
如果标签不存在,字符串为空或者null,将抛出异常;
Destroy 【继承】【父类:Objective】
public static void Destroy(Object obj);
public static void Destroy(Object obj, float t = 0.0F);
移除一个游戏物体或组件。
[kbd]obj[/kbd] 要销毁的游戏对象; [kbd]t[/kbd] 延迟销毁的时间,单位为秒;实际销毁的时间在当前循环之后,下次渲染之前如果需要立即销毁,使用DestroImmediately();
DestroyImmediate 【继承】【父类:Objective】
public static void DestroyImmediate(Object obj, bool allowDestroyingAssets = false);
立即销毁一个游戏对象或组件。
[kbd]allowDestroyingAssets[/kbd] 是否允许assers也被销毁;可能会发生不可预知的错误,所以如果非特殊需要,建议使用Destroy();
DontDestroyOnLoad【继承】【父类:Objective】
public static void DontDestroyOnLoad(Object target);
使物体不要在加载新的游戏场景时销毁。
public class ExampleClass : MonoBehaviour { void Awake() { DontDestroyOnLoad(transform.gameObject); } }
以为Unity总是在加载新的游戏场景时总会销毁旧场景的所有物体,所有才有了此方法。。。
在Awake()中使用;
如果是游戏物体或组件的话它的Transform信息也将被保留;
不知不觉竟然写了五千多字了。。
脑阔痛 _(:з」∠)_
Instantiate 【继承】【父类:Objective】
Instantiate(prefab, new Vector3(i * 2.0F, 0, 0), Quaternion.identity);
新游戏物体的子物体也会跟着实 【父类:Objective】
public static Object FindObjectOfType(Type type);
查找指定类型的对象。
GUITexture texture = (GUITexture)FindObjectOfType(typeof(GUITexture));
返回第一个找到的;
该方法效率较低,不建议每帧都调用;
FindObjectsOfType 【继承】【父类:Objective】
public static Object[] FindObjectsOfType(Type type);
查找指定类型的对象集合。
其他同上;
[info]操作符[/info]
bool
描述游戏物体是否存在。
public class ExampleClass : MonoBehaviour { void Example() { if (rigidbody) Debug.Log("Rigidbody attached to this transform"); } }
完