UNITY 5.2 GameObject类

本文详细介绍了Unity中GameObject类的基本属性、构造函数、公共函数及其用途,包括如何添加、获取、操作组件,以及消息传递机制。通过实例演示了如何在游戏开发中灵活运用这些功能,提高开发效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

此类继承于命名空间 UnityEngine 中的Object类

变量:

public Transform transform { get; }
说明:只读属性,每个对象都具有的 变换组件


public bool activeInHierarchy { get; }
说明:只读属性,若该GameObject是活动的,则该对象和他所有的父对象的 GameObject.activeSelf 是 TRUE


public bool activeSelf { get; }
说明:只读属性,该对象是否活动


public bool isStatic { get; set; }
说明:标记对象是否 isStatic


public int layer { get; set; }
说明:对象的层级 [0...31] ,控制渲染层级和射线投射层级


public string tag { get; set; }
说明:标志类型

构造函数:

public GameObject(string name);
说明:创建一个只有名字的对象


public GameObject(string name, params Type[] components);
说明:创建一个有名字和传入组件的对象

Public函数:

添加组件函数:

public Component AddComponent(string className);
说明:传入组件类名

public T AddComponent<T>() where T : Component;
说明:通过泛型创建

public Component AddComponent(Type componentType);
说明:组件类型创建

消息传递函数:
public void BroadcastMessage(string methodName);
public void BroadcastMessage(string methodName, object parameter);
public void BroadcastMessage(string methodName, SendMessageOptions options);
public void BroadcastMessage(string methodName, object parameter, SendMessageOptions options);

说明: 该对象向自己或者子对象发送消息,在该对象或者该子对象(MonoBehaviour)中调用名字为 methodName 的方法 可以选择是否有参数
SendMessageOptions(RequireReceiver 必须有消息接受者,否则打印错误(默认) 与 DontRequireReceiver 不需要消息接受者 )


public void SendMessage(string methodName);
public void SendMessage(string methodName, object value);
public void SendMessage(string methodName, SendMessageOptions options);
public void SendMessage(string methodName, object value, SendMessageOptions options);

说明: 该对象向自己发送消息,在该对象(MonoBehaviour)中调用名字为 methodName 的方法 可以选择是否有参数
SendMessageOptions(RequireReceiver 必须有消息接受者,否则打印错误(默认) 与 DontRequireReceiver 不需要消息接受者 )


public void SendMessageUpwards(string methodName);
public void SendMessageUpwards(string methodName, object value);
public void SendMessageUpwards(string methodName, SendMessageOptions options);
public void SendMessageUpwards(string methodName, object value, SendMessageOptions options);

说明: 该对象向自己或者父对象发送消息,在该对象或者父对象(MonoBehaviour)中调用名字为 methodName 的方法 可以选择是否有参数
SendMessageOptions(RequireReceiver 必须有消息接受者,否则打印错误(默认) 与 DontRequireReceiver 不需要消息接受者 )


public T GetComponent<T>();
public Component GetComponent(string type);
public Component GetComponent(Type type);
说明:通过组件类型或者组件名称返回该对象中包含的组件,否则为null

public Component GetComponentInChildren(Type type);
public T GetComponentInChildren<T>();
说明:通过组件类型返回对象或者子对象中包含的组件对象,depth优先,只返回最先找到的那一个 否则为null

public T GetComponentInParent<T>();
public Component GetComponentInParent(Type type);
说明:通过组件类型返回父对象中包含的组件对象,depth优先,只返回最先找到的那一个 否则为null

public T[] GetComponents<T>();
public Component[] GetComponents(Type type);
说明:返回该对象中的所有该类型的组件数组

public void GetComponents<T>(List<T> results);
public void GetComponents(Type type, List<Component> results);
说明:返回该对象中的所有该类型的组件列表

public T[] GetComponentsInChildren<T>();
public T[] GetComponentsInChildren<T>(bool includeInactive);
说明:通过组件类型返回所有在该对象或者子对象中的组件数组,includeInactive是否包含不活动的对象

public Component[] GetComponentsInChildren(Type type);
public Component[] GetComponentsInChildren(Type type, bool includeInactive);
说明:通过组件类型返回所有在该对象或者子对象中的组件数组,includeInactive是否包含不活动的对象

public void GetComponentsInChildren<T>(List<T> results);
public void GetComponentsInChildren<T>(bool includeInactive, List<T> results);
说明:通过组件类型返回所有在该对象或者子对象中的组件列表,includeInactive是否包含不活动的对象

public T[] GetComponentsInParent<T>();
public T[] GetComponentsInParent<T>(bool includeInactive);
说明:通过组件类型返回所有在该对象或者父对象中的组件数组,includeInactive是否包含不活动的对象

public Component[] GetComponentsInParent(Type type);
public Component[] GetComponentsInParent(Type type, bool includeInactive);
说明:通过组件类型返回所有在该对象或者父对象中的组件数组,includeInactive是否包含不活动的对象

public void GetComponentsInParent<T>(bool includeInactive, List<T> results);
说明:通过组件类型返回所有在该对象或者父对象中的组件数组,includeInactive是否包含不活动的对象


public bool CompareTag(string tag);
说明:比较对象tag 与 tag 是否相等

public void SetActive(bool value);
说明:设置该对象是否活动

Static函数
public static GameObject CreatePrimitive(PrimitiveType type);
说明:通过 PrimitiveType 创建对象,包含碰撞盒子(Sphere、 Capsule、 Cylinder、 Cube、 Plane、 Quad)

public static GameObject Find(string name);
说明:通过 对象名称 找到该对象,若没有该名称对象或者该对象不是活动的则返回null,若名称中出现 '/' 字符,则将名字以该字符分割成层级关系。尽量不要在每一帧中使用该函数

public static GameObject FindWithTag(string tag);
说明:返回用该 tag 标记的活动的一个对象,tag在使用前必须先声明

public static GameObject[] FindGameObjectsWithTag(string tag);
说明:返回用该 tag 标记的活动的对象的数组,tag在使用前必须先声明


继承成员

变量:
hideFlags
说明:在场景编辑器中的编辑状态
name
说明:该对象的名称

Public函数:
public int GetInstanceID();
说明:返回对象的唯一ID

public string ToString();
说明:返回对象的名称

Static函数:

public static void Destroy(Object obj, float t = 0.0F);
说明:如果Object是组件,则移除该组件并销毁,若Object是对象,则移除并销毁该对象的所有子对象和组件、资源,等待时间 t 执行


public static void DestroyImmediate(Object obj, bool allowDestroyingAssets = false);
说明:立即销毁对象,allowDestroyingAssets是否销毁资源,不建议用,代码中最好使用 Destroy


public static void DontDestroyOnLoad(Object target);
说明:保证该对象在加载新场景时不会被自动回收,并保留完整的变换组件


public static Object FindObjectOfType(Type type);
说明:返回第一个该类型的活动的对象,不推荐每帧使用


public static Object[] FindObjectsOfType(Type type);
说明:返回所有该类型的对象数组


public static Object Instantiate(Object original, Vector3 position, Quaternion rotation);
public static Object Instantiate(Object original);
说明:original 期望被克隆的对象, position 新坐标, rotation 新坐标,若被克隆的是组件对象,那么附加该组件的对象也会被克隆

操作符

Object.bool 
说明:该对象是否为 null


public static bool operator !=(Object x, Object y);
public static bool operator ==(Object x, Object y);
说明:判断两个对象是否相等


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值