《Unity_API解析》 第三章 GameObject类
2016-02-27 14:11
417 查看
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对象。
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对象。
相关文章推荐
- [置顶] unity中制作序列帧动画
- [置顶] Unity中基于NGUI插件制作序列帧动画
- 碰撞检测之Ray-Capsule检测
- Unity3d MonoBehaviour 的可重写函数整理
- Unity3d中的属性(Attributes)整理
- Share 学习二:Unity shader 的组织形式
- CCIE学习笔记 3---BGP 团体属性 community
- Unity 编辑器路径设置输入框
- 反序列化多个XML文件后出现Unity卡死
- Unity性能优化专题(腾讯牛人分享经验)
- Unity3D-Android版本调用Java函数
- 【Unity】关于LightMap过度曝光、NormalMap变红
- Unity动态对象优化
- Unity系列文章
- Unity全面优化
- Unity 的延时加载(defer resolve)
- Unity获取场景gameobject的路径
- Ubuntu unity 桌面正常工作在VNC上
- Unity3D 学习 控制角色运动控制
- Unity NGUI之ScrollView扩展-2