zoukankan      html  css  js  c++  java
  • 《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对象。
  • 相关阅读:
    Linux常用命令及详细说明 — 结合工作(侧重性能监控,包括CPU、内存、IO、网络、磁盘等)
    navicat连接不上Linux服务器上的mysql的解决办法
    Git之rebase、merge和cherry pick的区别详解—面试常问
    阿里《JAVA实习生入职测试题—2019最新》之答案详解(连载一)
    技术语言框架学习方法论
    阿里《JAVA实习生入职测试题—2019最新》之答案详解(连载二)
    C# 文件/文件夹一般操作(File、Directory)
    Log4Net 使用及组合公共类
    VmWare 15 设置Centos7 共享文件夹及问题记录
    Centos 7 使用(Service iptables stop/start)关闭/打开防火墙 Failed to stop iptables.service: Unit iptables.service not loaded.
  • 原文地址:https://www.cnblogs.com/colve/p/5222844.html
Copyright © 2011-2022 走看看