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

Unity 获取物体的子物体的方法

Unity 中要获取物体的子物体,可以使用以下一些方法。

1、只获取一级节点的子物体:

    public Transform tran;// Start is called before the first frame updatevoid Start(){foreach (Transform child in tran){Debug.Log(child.name);}}

使用该方法只会获取物体中一级节点的子物体。

2、获取物体的所有节点的子物体,使用GetComponentsInChildren方法:

    public Transform tran;// Start is called before the first frame updatevoid Start(){foreach (Transform child in tran.GetComponentsInChildren<Transform>()){Debug.Log(child.name);}}

使用上面方法可以获取物体中所有节点的子物体,但它会包括该物体本身。

使用GetComponentsInChildren方法,还可以获取某种指定类型的子物体,这个特别好用,如:

Renderer[] renderers = GetComponentsInChildren<Renderer>();

3、还有一种比较常用的方法,即使用Transform组件的GetChild()方法。

这个方法通过子物体的索引获取子物体,索引从0开始,按照子物体在层次结构中的顺序进行编号。

    void Start(){// 获取当前物体的 Transform 组件Transform parentTransform = transform;// 遍历所有子物体for (int i = 0; i < parentTransform.childCount; i++){// 获取子物体的 Transform 组件Transform childTransform = parentTransform.GetChild(i);// 在这里可以对子物体进行操作,例如打印子物体的名称Debug.Log("子物体名称:" + childTransform.name);}}

http://www.lryc.cn/news/248508.html

相关文章:

  • RocketMQ 读写压测
  • PHP调用API接口的方法及实现(一键采集淘宝商品详情数据)
  • 得物App安卓冷启动优化-Application篇
  • 【实战教程】PHP与七牛云的完美对接
  • Go 接口:nil接口为什么不等于nil?
  • (UM1724) STM32 Nucleo-64 boards User manual
  • SourceInsight - Relation Windows
  • 二维数组处理
  • 线性表——(2)线性表的顺序存储及其运算的实现
  • 数据结构 -- 图论之最小生成树
  • 【已解决】游戏缺少xinput1_3.dll的详细解决方案与详情解析
  • 华天动力-OA8000 MyHttpServlet 文件上传漏洞复现
  • 小航助学题库蓝桥杯题库c++选拔赛(23年8月)(含题库教师学生账号)
  • [Ubuntu 18.04] RK3399搭建NFS服务实现共享目录
  • Java---抽象类讲解
  • CNAS认可是什么?CNAS软件测试报告如何获取?
  • Tomcat 修改版本号
  • Python算法——霍夫曼编码树
  • hql面试题之上海某资深数仓开发工程师面试题-求不连续月份的月平均值
  • VT驱动开发
  • 火柴人版王者-Java
  • docker 中的–mount 和-v 参数有啥区别
  • 设计规则:模块化的力量
  • 数据结构与算法之递归: LeetCode 78. 子集 (Typescript版)
  • C# 使用 Fody 监控方法执行时间
  • J2EE征程——第一个纯servletCURD
  • BatchOutput PDF for Mac(PDF 批量处理软件)
  • 记一次oracle错误处理
  • hugging face下载dataset时候出现You must be authenticated to access it.问题解决
  • 数据结构---树