《Unity_API解析》 第三章 GameObject类

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对象。
posted on 2016-02-27 14:11 Colve 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/colve/p/5222844.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值