当前位置: 首页 > ds >正文

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 → 同时导致activeInHierarchyisActiveAndEnabled变为false

 

二、重要方法

1. 从脚本获取其他的组件,例如RigidBory等

方法签名参数说明返回值时间复杂度使用建议
T GetComponent<T>()无参数单个TO(1)优先使用的标准方式
Component GetComponent(string type)type: 类型名称字符串ComponentO(n)避免使用,存在性能损耗
Component GetComponent(Type type)type: System.Type类型对象ComponentO(n)非泛型方案,需类型转换
bool TryGetComponent<T>(out T component)component: 输出参数boolO(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向上层级链

三、参数详解对照表

参数名称适用方法默认值作用说明
includeInactiveGetComponentInChildren
GetComponentInParent
falsetrue时包含非激活对象:
- 对子对象:搜索被SetActive(false)的对象
- 对父对象:允许父级链中存在被禁用的对象
resultsGetComponents系方法-预分配的List容器,用于接收结果,避免内存分配
type非泛型方法-指定组件类型:
- 字符串版本效率较低
- Type对象需使用typeof运算符获取

四、总结

关键属性总表

属性类型作用描述重要说明
gameObjectGameObject当前脚本依附的游戏对象所有MonoBehaviour的根基
transformTransform快捷访问gameObject.transform等效于gameObject.transform
enabledbool控制脚本的启用状态(true=执行Update等生命周期方法)仅影响脚本本身,不影响其他组件
tagstring获取/设置游戏对象的标签建议使用CompareTag方法进行标签比较
layerint获取/设置游戏对象的层级(0-31)可通过LayerMask.NameToLayer转换层名称
isActiveAndEnabledbool复合状态检测(gameObject.activeInHierarchy && enabled)判断脚本是否真正起效的关键属性
useGUILayoutbool是否参与旧版GUI布局计算(默认true)现代UI系统(UGUI)不受此属性影响
gameObject.activeSelfbool对象自身的激活状态(不受父对象影响)仅反映SetActive的直接设置
gameObject.activeInHierarchybool对象在场景中的实际激活状态(受父对象层级影响)决定对象是否实际参与渲染、物理计算等

组件获取方式详解 

单组件获取方法

方法签名参数说明包含自身搜索范围推荐指数
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: 结果列表
同上

五、注意事项 

  1. 组件缓存​:Start/Awake 预存常用组件
  2. 高频优化​:Update 中禁用 GetComponent
  3. 空值防护​:必用 TryGetComponent
  4. 标签规范​:CompareTag 代替字符串
  5. 状态隔离​:物理组件单独管理
  6. 预制延迟​:实例化后延迟初始化
  7. 循环禁忌​:禁止组件相互引用
http://www.xdnf.cn/news/4135.html

相关文章:

  • 形式化数学——Lean求值表达式
  • 【数据治理】数据架构设计
  • 2962. 统计最大元素出现至少 K 次的子数组
  • 1. 设计哲学:让字面量“活”起来,提升表达力和安全性
  • java stream
  • Python训练打卡Day16
  • 【AI绘画】Ottohans Beier风格雕刻版画
  • 我的世界Minecraft游戏服务器搭建教程:腾讯云Java版
  • java CompletableFuture 异步编程工具用法1
  • 免费在线练字宝藏Z2H 免安装高效生成 vs 笔顺功能补缺
  • Docker 容器 - Dockerfile
  • 大模型微调Fine-tuning:从概念到实践的全面解析
  • #基础Machine Learning 算法(上)
  • 第三章 - 软件质量工程体系
  • 【codeforces 2070c】二分答案详解
  • PostgreSQL 的 pg_current_wal_lsn 函数
  • 15届蓝桥杯国赛 立定跳远
  • 红黑树和AVL树封装map和set的细节 以及 map的operator[]重载的底层
  • 从Rtos到Linux:学习的策略
  • 基于思考过程评价的心理问题咨询对话记性评估
  • Kotlin带接收者的Lambda介绍和应用(封装DialogFragment)
  • Guass数据库实验(数据字典设计、交叉表设计)
  • 基于MATLAB图像中的圆形目标识别和标记
  • DDR在PCB布局布线时的注意事项及设计要点
  • 人工智能数学基础(九)—— 信息论
  • 用户模块 - IP归属地技术方案
  • 【Ubuntu 安装Docker CE-Jenkins】
  • 促销量化模型简介和示例
  • 商业秘密泄露后的法律救济
  • 36、C#中的⽅法声明参数关键字params,ref,out的意义及⽤法