public sealed class GameObject : Object{}
GameObject 是Unity场景中所有实体的基础对象,继承自
UnityEngine.Object
的密闭类,它由Transform或其衍生组件和若干组件组成。
变量
activeInHierarchy
public bool activeInHierarchy { get; }
定义 GameObject 在 Scene 中是否处于活动状态。
- 如果启用了其 GameObject.activeSelf 属性,以及其所有父项的这一属性,则该 GameObject 处于活动状态。
- 与 GameObject.activeSelf 不同,它还检查是否有父 GameObject 影响此 GameObject 的当前活动状态。
- 当停用父 GameObject 时,即使其子项不可见,通常也会将它们标记为活动,这样根据 GameObject.activeSelf,它们仍处于活动状态。但 GameObject.activeInHierarchy 会确认其层级视图中的成员尚未停用此 GameObject,从而确保不会发生这种情况。
activeSelf
public bool activeSelf { get; }
此 GameObject 自身的活跃状态。(只读)
该属性返回此 GameObject 的自身的活跃状态,这是使用 GameObject.SetActive 设置的。 注意,GameObject 可能因为父GameObject 未处于活跃状态,其也会返回 true。如果我们要检查此 GameObject 实际上在该场景中是否被视为处于活跃状态,请使用 GameObject.activeInHierarchy。
isStatic
public bool isStatic { get; set; }
获取或设置 GameObject 的 StaticEditorFlags。
- 如果已启用任何 StaticEditorFlags,则返回 true。如果所有 StaticEditorFlags 均已禁用,则返回 false。
- 如果设置为 true,则启用所有 StaticEditorFlags。如果设置为 false,则禁用所有 StaticEditorFlags。
- StaticEditorFlags 确定哪些 Unity 系统将 GameObject 视为静态,并在 Unity 编辑器的预计算中包含 GameObject。在运行时设置 StaticEditorFlags 对这些系统没有影响。
StaticEditorFlags
描述哪些 Unity 系统将 GameObject 视为静态,并在 Unity 编辑器的预计算中包含 GameObject。
在运行时设置 StaticEditorFlags 对这些系统没有影响。
ContributeGI | 在全局光照计算中包括目标网格渲染器。 |
---|---|
OccluderStatic | 在遮挡剔除系统中,将 GameObject 标记为静态遮挡物。 |
OccludeeStatic | 在遮挡剔除系统中,将 GameObject 标记为静态被遮挡物。 |
BatchingStatic | 将 GameObject 的网格与其他符合条件的网格组合起来,有可能降低运行时渲染成本。静态批处理,运行时可以使用StaticBatchingUtility.Combine 进行合并。 |
NavigationStatic | 预计算NavMesh导航数据时包含 GameObject。 |
OffMeshLinkGeneration | 预计算NavMesh导航数据时包含的GameObject网格外链接对象。 |
ReflectionProbeStatic | 为 Reflection Probes 反射探针(其 Type 属性设置为 Baked)预计算数据的GameObject。 |
layer
public int layer { get; set; }
GameObject 所在的层。
- Unity 提供32个层,从0-31,系统保留0到5层。
- 层用于相机的渲染过滤和物理射线过滤。
scene
public Scene scene { get; }
该 GameObject 所属的场景。
sceneCullingMask
public ulong sceneCullingMask { get; }
Unity 用于确定在哪个场景中渲染该 GameObject 的场景剔除遮罩。
- 此值与
UnityEditor.SceneManagement.EditorSceneManager.GetSceneCullingMask(gameObject.scene)
获取的值相同,EditorSceneManager.SetSceneCullingMask
可修改场景的SceneCullingMask,此值为掩码值。
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。
构造函数
public GameObject();
public GameObject(string name);
public GameObject(string name, params Type[] components);
创建一个新游戏的对象。
- 创建GameObject 将添加 Transform组件,
- components 将会附加对应类型的组件。前提是继承于
Component
。
公共函数
AddComponent
public Component AddComponent(Type componentType);
public T AddComponent<T>() where T : Component;
为GameObject添加组件
- 需要移除某个组件使用
Object.Destroy(this)
。
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
,没有对应方法时会触发异常。
SetActive
public void SetActive(bool value);
激活或者停用对象
- GameObject 可能因为父GameObject 处于非活动状态。在这种情况下,调用 SetActive 不会激活它,而是仅设置此 GameObject 的本地状态。
- 该状态可使用
GameObject.activeSelf
加以检查。 - Unity不会调用已停用 GameObject 的脚本的 Update() 方法。
- 当 GameObject 设置
SetActive(true)
或SetActive(false)
时,将调用OnEnable
或OnDisable
。 - 当父GameObject 非活动状态,设置
SetActive(true)
或SetActive(false)
时,将不会调用OnEnable
或OnDisable
。父GameObject ,设置SetActive(true)
或SetActive(false)
时,会调用活跃状态的子GameObject的OnEnable
或OnDisable
。
TryGetComponent
public bool TryGetComponent(Type type, out Component component);
public bool TryGetComponent<T>(out T component);
获取是否有指定类型的组件,如果存在返回true,输出组件。
- 与
GameObject.GetComponent
相比的显著差异在于,如果请求的组件不存在,则此方法不在编辑器中进行内存分配。
静态函数
CreatePrimitive
public static GameObject CreatePrimitive(PrimitiveType type);
创建一个具有原始Mesh Render和相应Collider的游戏对象。
- 在运行时可能失败。发生此问题的原因是 您的项目在运行时未引用以下组件:
MeshFilter
、MeshRenderer
和BoxCollider
或SphereCollider
。避免此崩溃的建议方式是 声明这些类型的私有属性,如:private MeshFilter mf;
。剥离系统将识别它们的用途,这会将它们包含在此版本中,这样便不会剥离这些组件。
PrimitiveType
- Sphere:球体对象
- Capsule:胶囊体对象
- Cylinder:圆柱体对象
- Cube:立方体对象
- Plane:平面对象
- Quad:四边形对象
Find
public static GameObject Find(string name);
按
name
查找GameObject,然后返回它。
- 仅返回活跃的GameObject。
- 如果未找到具有
name
的 GameObject,则返回 null。 - 如果
name
包含“/”字符,则会向路径名称那样遍历此层级视图。如Root/Camera
,会找到Root下的Camera对象。 - 建议不要每帧都使用此函数,而是在
Awake()
或Start()
时将结果缓存到成员变量中,或者使用GameObject.FindWithTag(tag)
。 - 如果您要查找子GameObject,使用
Transform.Find(name)
通常会更加合适。 - 如果正在使用多个场景运行此游戏,则 Find 将在所有这些场景中进行搜索。
FindGameObjectsWithTag
public static GameObject[] FindGameObjectsWithTag(string tag);
返回标签为
tag
的活动GameObjects的数组。如果未找到任何GameObject,则返回空数组。
- 标签在使用前必须在标签管理器中加以声明。如果此标签不存在,或者传递了空字符串或null作为标签,则将抛出 UnityException。
FindWithTag
public static GameObject FindWithTag(string tag);
返回一个标记为
tag
的活动 GameObject。如果未找到GameObject,则返回 null。
- 标签在使用前必须在标签管理器中加以声明。如果此标签不存在,或者传递了空字符串或null作为标签,则将抛出 UnityException。
- 此方法返回它找到的具有指定
tag
的第一个GameObject。如果一个场景包含多个具有指定标签的活动GameObjects,则无法保证此方法返回特定的GameObject。