public class Component : Object{}
组成GameObject的所有组件的基本类,继承自
UnityEngine.Object
。
- 我们的代码不会直接创建 Component,而是编写脚本代码,然后将该脚本添加到GameObject。
变量
gameObject
public GameObject gameObject { get; }
此组件添加到的GameObject·。
tag
public string tag { get; set; }
此GameObject 的标签。
- 可使用标签来识别游戏对象。
- 使用标签前,必须在标签和层管理器中声明它们。
- 您不应通过 Awake() 或 OnValidate() 方法设置标签。因为组件唤醒的顺序是不确定的,因此可能导致意外行为,例如标签在唤醒时被覆盖。如果尝试执行此操作,Unity 将生成一条警告:“SendMessage cannot be called during Awake, CheckConsistency, or OnValidate”。
- layer有数量限制,tag并没有数量限制。
transform
public Transform transform { get; }
附加到此 GameObject 的 Transform。
公共函数
BroadcastMessage
public void BroadcastMessage(string methodName);
public void BroadcastMessage(string methodName, object parameter);
public void BroadcastMessage(string methodName, [Internal.DefaultValue("null")] object parameter, [Internal.DefaultValue("SendMessageOptions.RequireReceiver")] SendMessageOptions options);
public void BroadcastMessage(string methodName, SendMessageOptions options);
调用此游戏对象或其任何子项中的每个 MonoBehaviour 上名为
methodName
的方法。
-
- 不会将消息发送到非活动对象
parameter
为值类型有拆装箱损耗- 当
options
为SendMessageOptions.RequireReceiver
,没有对应方法时会触发异常。
CompareTag
public bool CompareTag(string tag);
当前GameObject的tag是否为目标
tag
。
GetComponent
public Component GetComponent(string type);
public T GetComponent<T>();
public Component GetComponent(Type type);
获取对应类型的第一个组件。
- 返回找到的第一个组件,属性视图的第一个此类型组件。
- 如果预期存在多个相同类型的组件,请用
gameObject.GetComponents
获取对应类型数组。 - 非活跃组件也可以获取到。
- 推荐使用
TryGetComponent
GetComponentInChildren
public Component GetComponentInChildren(Type type, bool includeInactive);
public T GetComponentInChildren<T>();
public T GetComponentInChildren<T>([Internal.DefaultValue("false")] bool includeInactive);
public Component GetComponentInChildren(Type type);
获取自身或者子GameObject及所有后续子 GameObject指定类型。
- 返回找到的第一个组件,属性视图的第一个此类型组件,或层级视图从上至下的查找顺序。
- 非活跃组件也可以获取到。
- 非活跃的子GameObject上的类型通过设置
includeInactive
为true获取。
GetComponentInParent
public Component GetComponentInParent(Type type, bool includeInactive);
public Component GetComponentInParent(Type type);
public T GetComponentInParent<T>();
public T GetComponentInParent<T>([Internal.DefaultValue("false")] bool includeInactive);
获取自身或者父GameObject指定类型。
- 返回找到的第一个组件,属性视图的第一个此类型组件,向上递归。
- 非活跃组件也可以获取到。
- 非活跃的父GameObject上的类型通过设置
includeInactive
为true获取。
GetComponents
public void GetComponents<T>(List<T> results);
public Component[] GetComponents(Type type);
public T[] GetComponents<T>();
public void GetComponents(Type type, List<Component> results);
返回此对象此类型所有组件。
- 非活跃组件也可以获取到。
GetComponentsInChildren
public void GetComponentsInChildren<T>(List<T> results);
public T[] GetComponentsInChildren<T>();
public void GetComponentsInChildren<T>(bool includeInactive, List<T> results);
public T[] GetComponentsInChildren<T>(bool includeInactive);
public Component[] GetComponentsInChildren(Type type, [Internal.DefaultValue("false")] bool includeInactive);
public Component[] GetComponentsInChildren(Type type);
获取自身或者子GameObject及所有后续子 GameObject所有指定类型。
- 包含目标 GameObject 的子 GameObject 以及所有后续子 GameObject。
- 非活跃组件也可以获取到。
- 非活跃的子GameObject上的类型通过设置
ncludeInactive
为true获取。
GetComponentsInParent
public T[] GetComponentsInParent<T>();
public void GetComponentsInParent<T>(bool includeInactive, List<T> results);
public T[] GetComponentsInParent<T>(bool includeInactive);
public Component[] GetComponentsInParent(Type type, [Internal.DefaultValue("false")] bool includeInactive);
public Component[] GetComponentsInParent(Type type);
获取自身或者父GameObject所有指定类型。
- 非活跃组件也可以获取到。
- 非活跃的父GameObject上的类型通过设置
includeInactive
为true获取。
SendMessage
public void SendMessage(string methodName);
public void SendMessage(string methodName, object value);
public void SendMessage(string methodName, [Internal.DefaultValue("null")] object value, [Internal.DefaultValue("SendMessageOptions.RequireReceiver")] SendMessageOptions options);
public void SendMessage(string methodName, SendMessageOptions options);
调用此游戏对象中的每个 MonoBehaviour 上名为
methodName
的方法。
- 不会将消息发送到非活动对象
value
为值类型有拆装箱损耗- 当
options
为SendMessageOptions.RequireReceiver
,没有对应方法时会触发异常。
SendMessageUpwards
public void SendMessageUpwards(string methodName);
public void SendMessageUpwards(string methodName, object value);
public void SendMessageUpwards(string methodName, [Internal.DefaultValue("null")] object value, [Internal.DefaultValue("SendMessageOptions.RequireReceiver")] SendMessageOptions options);
public void SendMessageUpwards(string methodName, SendMessageOptions options);
调用此游戏对象中的每个 MonoBehaviour 上或此对象的每个父级上名为
methodName
的方法。
- 不会将消息发送到非活动对象
value
为值类型有拆装箱损耗- 当
options
为SendMessageOptions.RequireReceiver
,没有对应方法时会触发异常。
TryGetComponent
public bool TryGetComponent(Type type, out Component component);
public bool TryGetComponent<T>(out T component);
获取是否有指定类型的组件,如果存在返回true,输出组件。
- 与
GameObject.GetComponent
相比的显著差异在于,如果请求的组件不存在,则此方法不在编辑器中进行内存分配。