【Unity】入门学习笔记180517——API(4)——GameObject类

GameObject类

一个GameObject对象通常由多个组件component组成,且至少含有一个Transform组件


A、实例属性

1、activeSelf:

public bool activeSelf{ get; }

返回Gameobject对象的Active标识状态

与activeInHierarchy属性的区别在于不受父类的激活状态影响

即即使父类状态未激活,子类状态激活,即使程序运行不显示,但仍然返回True


B、构造方法

public GameObject();

public GameObject(String name);

public GameObject(String name, params Type[] components);


C、实例方法

1、GetComponent / GetComponentInChildren /GetCompinents / GetComponentsInChildren

获取组件

public T GetComponent<T>() Where T: Component;

public Component GetComponent(string type);

public Component GetComponent(Type type);


2、SendMessage

public void SendMessage(string methodName);

~(string methodName, object value);

~(string methodName, SendMessageOptions options);

~(string methodName, object value, SendMessageOptions options);

参数options为信息接收的方式,默认为SendMessageOptions.RequireReceiver

向GameObject自身发送消息,有作用范围:

①和自身同级的物体不会收到消息

②DontRequireReceiver不要求信息的接收方必须有接受消息的方法


此外,还有

BroadcastMessage:向自身及其所有子类发送消息,自身同级物体不会收到消息

SendMessageUpwards:向自身及其所以父类发送消息,自身同级物体不会收到消息


D、静态方法

1、CreatePrimitive

public static GameObject CreatePrimitive( PrimitiveType type);

参数使用例如:PrimitiveType.Sphere

创建一个系统自带的GameObject对象



猜你喜欢

转载自blog.csdn.net/Dylan_Day/article/details/80349156
今日推荐