Untiy基础学习(六)MonoBehaviour基类的简单介绍
目录
本文末尾有全部总结,如有需要可直接跳转
一、几个重要成员
二、重要方法
1. 从脚本获取其他的组件,例如RigidBory等
2. 获取多个组件
3.子对象组件获取
3.1 单组件获取(深度搜索)
3.2 多组件获取(全子树)
4.父对象组件获取
4.1单组件获取(层级搜索)
4.2 多组件获取(全父链)
三、参数详解对照表
四、总结
关键属性总表
组件获取方式详解
单组件获取方法
多组件获取方法:
五、注意事项
本文末尾有全部总结,如有需要可直接跳转
一、几个重要成员
这个就是gameObject
(1)本(Object)对象的名字 this.gameObject.name
(2)本(Object)对象的身上的transform组件。快捷访问,this.gameObject.transform
(3)使得某个脚本是否激活,注意是脚本。控制脚本的激活状态(true=启用,false=禁用)
禁用时:Update()、FixedUpdate()等生命周期方法将停止执行
启用时:会触发OnEnable(),禁用时触发OnDisable()
(4)tag:访问或设置GameObject的标签
(5)layer:访问或设置GameObject所在的层级(0-31)
(6)isActiveAndEnabled:复合状态检查(GameObject激活且脚本启用)
属性 | 检查内容 | 典型应用场景 |
---|---|---|
gameObject.activeSelf | 仅检查当前对象自身的激活状态(与父对象无关) | 判断对象是否被自身设置为非激活状态 |
gameObject.activeInHierarchy | 检查对象在场景中的实际激活状态(受父对象层级影响) | 判断对象是否真实参与场景交互 |
enabled | 仅检查脚本组件是否启用 | 控制脚本逻辑是否执行 |
isActiveAndEnabled | 同时满足:activeInHierarchy == true 且 enabled == true | 判断脚本是否真正处于工作状态 |
状态变化 | 影响结果 |
---|---|
父对象被禁用 | → activeInHierarchy 变为false → isActiveAndEnabled 变为false |
脚本被禁用 (enabled = false ) | → isActiveAndEnabled 立即变为false,即使对象处于激活状态 |
对象自身被禁用 (SetActive(false) ) | → activeSelf 变为false → 同时导致activeInHierarchy 和isActiveAndEnabled 变为false |
二、重要方法
1. 从脚本获取其他的组件,例如RigidBory等
方法签名 | 参数说明 | 返回值 | 时间复杂度 | 使用建议 |
---|---|---|---|---|
T GetComponent<T>() | 无参数 | 单个T | O(1) | 优先使用的标准方式 |
Component GetComponent(string type) | type: 类型名称字符串 | Component | O(n) | 避免使用,存在性能损耗 |
Component GetComponent(Type type) | type: System.Type类型对象 | Component | O(n) | 非泛型方案,需类型转换 |
bool TryGetComponent<T>(out T component) | component: 输出参数 | bool | O(1) | 安全获取的首选方式 |
上面表格中的23一般不推荐使用,主要是麻烦。你还要自己检测类型正确与否。
使用演示:
// 标准获取
Rigidbody rb = GetComponent<Rigidbody>();// 安全获取
if (TryGetComponent<Collider>(out Collider col)) {col.enabled = false;
}// 类型转换示例
Component c = GetComponent(typeof(Animator));
Animator anim = c as Animator;
2. 获取多个组件
方法签名 | 参数说明 | 返回值 | 内存分配 |
---|---|---|---|
T[] GetComponents<T>() | 无参数 | T数组 | 是 |
int GetComponents<T>(List<T> results) | results: 结果存储列表 | 找到的数量 | 否 |
void GetComponents(Type type, List<Component> results) | type: 组件类型 results: 结果列表 | void | 否 |
使用实例:
// 使用预分配列表减少GC
List<Collider> colliders = new List<Collider>(5);
GetComponents(colliders);// 清空复用列表
colliders.Clear();
GetComponents(colliders);
3.子对象组件获取
3.1 单组件获取(深度搜索)
方法签名 | 参数说明 | 默认值 | 搜索范围 |
---|---|---|---|
T GetComponentInChildren<T>(bool includeInactive = false) | includeInactive: 包含非激活对象 | false | 自身+所有子对象 |
T GetComponentInChildren<T>() | 无参数 | includeInactive=false | 同上 |
// 查找第一个激活的渲染器
Renderer rend = GetComponentInChildren<Renderer>();// 包含非激活对象
Collider col = GetComponentInChildren<Collider>(true);
3.2 多组件获取(全子树)
方法签名 | 参数说明 | 默认值 | 结果顺序 |
---|---|---|---|
T[] GetComponentsInChildren<T>(bool includeInactive = false) | includeInactive: 包含非激活对象 | false | 深度优先顺序 |
void GetComponentsInChildren<T>(bool includeInactive, List<T> results) | includeInactive: 同上 results: 结果列表 | - | 同上 |
T[] GetComponentsInChildren<T>() | 无参数 | includeInactive=false | 同上 |
// 使用列表避免数组分配
List<Transform> children = new List<Transform>(10);
GetComponentsInChildren(true, children);
4.父对象组件获取
4.1单组件获取(层级搜索)
方法签名 | 参数说明 | 默认值 | 搜索方向 |
---|---|---|---|
T GetComponentInParent<T>(bool includeInactive = false) | includeInactive: 包含非激活父对象 | false | 自身 → 父级 → 祖父级 |
T GetComponentInParent<T>() | 无参数 | includeInactive=false | 同上 |
// 获取最近的UI Canvas
Canvas canvas = GetComponentInParent<Canvas>();// 包含未激活的父对象
Rigidbody rb = GetComponentInParent<Rigidbody>(true);
4.2 多组件获取(全父链)
方法签名 | 参数说明 | 默认值 | 结果顺序 |
---|---|---|---|
T[] GetComponentsInParent<T>(bool includeInactive = false) | includeInactive: 包含非激活父对象 | false | 自身 → 父级 → 祖父级 |
void GetComponentsInParent<T>(bool includeInactive, List<T> results) | includeInactive: 同上 results: 结果列表 | - | 同上 |
// 示例结构:GrandParent ← Parent ← Self
// 返回数组顺序:[SelfComponent, ParentComponent, GrandParentComponent]
RectTransform[] parents = GetComponentsInParent<RectTransform>();
小结:就是获取一堆组件的这种,都会优先检查自己身上有没有组件,都会将自身的组件算进去的
方法类型 | 包含自身 | 搜索方向 | 默认激活要求 |
---|---|---|---|
GetComponentsInChildren | 是 | 向下深度优先 | 是 |
GetComponentsInParent | 是 | 向上层级链 | 是 |
GetComponentInChildren | 是 | 向下深度优先 | 是 |
GetComponentInParent | 是 | 向上层级链 | 是 |
三、参数详解对照表
参数名称 | 适用方法 | 默认值 | 作用说明 |
---|---|---|---|
includeInactive | GetComponentInChildren GetComponentInParent | false | true时包含非激活对象: - 对子对象:搜索被SetActive(false)的对象 - 对父对象:允许父级链中存在被禁用的对象 |
results | GetComponents系方法 | - | 预分配的List容器,用于接收结果,避免内存分配 |
type | 非泛型方法 | - | 指定组件类型: - 字符串版本效率较低 - Type对象需使用typeof运算符获取 |
四、总结
关键属性总表
属性 | 类型 | 作用描述 | 重要说明 |
---|---|---|---|
gameObject | GameObject | 当前脚本依附的游戏对象 | 所有MonoBehaviour的根基 |
transform | Transform | 快捷访问gameObject.transform | 等效于gameObject.transform |
enabled | bool | 控制脚本的启用状态(true=执行Update等生命周期方法) | 仅影响脚本本身,不影响其他组件 |
tag | string | 获取/设置游戏对象的标签 | 建议使用CompareTag方法进行标签比较 |
layer | int | 获取/设置游戏对象的层级(0-31) | 可通过LayerMask.NameToLayer转换层名称 |
isActiveAndEnabled | bool | 复合状态检测(gameObject.activeInHierarchy && enabled) | 判断脚本是否真正起效的关键属性 |
useGUILayout | bool | 是否参与旧版GUI布局计算(默认true) | 现代UI系统(UGUI)不受此属性影响 |
gameObject.activeSelf | bool | 对象自身的激活状态(不受父对象影响) | 仅反映SetActive的直接设置 |
gameObject.activeInHierarchy | bool | 对象在场景中的实际激活状态(受父对象层级影响) | 决定对象是否实际参与渲染、物理计算等 |
组件获取方式详解
单组件获取方法
方法签名 | 参数说明 | 包含自身 | 搜索范围 | 推荐指数 |
---|---|---|---|---|
T GetComponent() | 无参数 | 是 | 仅自身 | ★★★★★ |
bool TryGetComponent(out T component) | component: 输出参数 | 是 | 仅自身 | ★★★★★ |
T GetComponentInChildren(bool includeInactive = false) | includeInactive: 是否包含非激活子对象 | 是 | 自身+所有子对象(深度优先) | ★★★★☆ |
T GetComponentInParent(bool includeInactive = false) | includeInactive: 是否包含非激活父对象 | 是 | 自身→父级→祖父级 | ★★★★☆ |
多组件获取方法:
方法签名 | 参数说明 | 包含自身 | 搜索范围 | 内存分配 |
---|---|---|---|---|
T[] GetComponents() | 无参数 | 是 | 仅自身 | 是 |
void GetComponents(List results) | results: 结果存储列表 | 是 | 仅自身 | 否 |
T[] GetComponentsInChildren(bool includeInactive = false) | includeInactive: 是否包含非激活子对象 | 是 | 自身+所有子对象(深度优先) | 是 |
void GetComponentsInChildren(bool includeInactive, List results) | includeInactive: 同上 results: 结果列表 | 是 | 同上 | 否 |
T[] GetComponentsInParent(bool includeInactive = false) | includeInactive: 是否包含非激活父对象 | 是 | 自身→父级→祖父级 | 是 |
void GetComponentsInParent(bool includeInactive, List results) | includeInactive: 同上 results: 结果列表 | 是 | 同上 | 否 |
五、注意事项
- 组件缓存:Start/Awake 预存常用组件
- 高频优化:Update 中禁用 GetComponent
- 空值防护:必用 TryGetComponent
- 标签规范:CompareTag 代替字符串
- 状态隔离:物理组件单独管理
- 预制延迟:实例化后延迟初始化
- 循环禁忌:禁止组件相互引用