《Unity_API解析》 第三章 GameObject类

来源:互联网 发布:中国移动积分兑换软件 编辑:程序博客网 时间:2024/06/11 02:20
GameObject类实例属性

activeSelf属性:GameObject的Active标识

public bool activeSelf{ get; }
功能说明 此属性用来返回GameObject对象的Active标识状态,即物体的活跃状态。
注意:此属性与activeInHierarchy的区别。activeInHierarchy属性的功能是返回GameObject实例在程序运行时的激活状态,只有当GameObject实例的状态被激活时才会返回true。而且它会受其父物体对象激活状态的影响,如果其父类至最顶层的对象中有一个对象未被激活,activeInHierarchy就会返回false。
 
GameObject类构造方法
1.public GameObject();
2.public GameObject(string name);
参数为构造GameObject对象的名字
3.public GameObject(string name,params Type[]  components);
参数那么为GameObject对象的名字,components为构造对象要添加的组件类型集合,多个组件之间用逗号隔开。
功能说明 此构造方法用来创建一个GameObject对象。
 
GameObject类实例方法
GetComponent方法:获取组件
1.public T GetComponent<T> where T : Component;
2.public Component GetComponent(string type);
其中参数type为组件名。
3.public Component GetComponent(Type type);
其中参数type为组件类型。
功能说明 此方法用于获取GameObject中第一个符合Type类型的Component。
注意:与此方法功能相似的方法有GetComponentInChildren,GetComponents和GetComponentsInChildren。
1.在使用GetComponents(type:Type)方法时
Component[] cjs = GetComponents(typeof(configurableJoint)) as Component[];,这样写是不可以的:
ConfigurableJoint[] cjs = GetComponents(typeof(ConfigurableJoint)) as Configurable Joint[];
因为ConfigurableJoint不是Component,而是其子类,建议使用其泛型方式。
2.在使用GetComponentsInChidren(type:Type,includeInactive:boolean = false)方法时,不可以这样写。
Component[] cjs = GetComponentsInChildren(typeof(ConfigurableJoint),false) as Component[];
ConfigurableJoint[] cjs = GetComponentsInChidren(typeof(ConfigurableJoint), false) as ConfigurableJoint[];
因为ConfigurableJoint不是Component,而是其子类,建议使用其泛型方式。
 
SendMessage方法:发送消息
1.public void SendMessage(string methodName);
2.public void SendMessage(string methodName, object value);
3.public void SendMessage(string methodName, SendMessageOptions options);
4.public void SendMessage(string methodName, object value, SendMessageOptions options);
参数methodName为接收消息的方法名字,value为信息的内容,options为信息的接收方式,默认为SendMessageOptions.RequireReceiver。
功能说明 此方法的功能是向GameObject自身发送消息,对其作用范围说明如下:
1.和接受消息对象同级的物体不会收到消息。
2.SendMessageOptions有两个可选方式:SendMessageOptions.RequireReceiver和SendMessageOptions.DontRequireReceiver。前者要求信息的接收方必须有接受信息的方法,否则程序会报错,后者无要求。
提示:与此方法功能相似的方法有BroadcastMessage和SendMessageUpwards,对其功能说明如下:
1.BroadcastMessage方法的功能是向对象自身及其所有子类发送消息。和对象同级的物体不会收到消息。
2.SendMessageUpwards方法的功能是向对象自身及其所有父类发送消息。和自身同级的物体不会收到消息。
 
GameObject类静态方法
Createprimitive方法 : 创建GameObject对象
public static GameObject CreatePrimitive(PrimitiveType type);
参数为枚举PrimitiveType的类型值。
功能说明:此方法的功能是创建一个系统自带的GameObject对象。
0 0
原创粉丝点击