【Unity3D日常开发】如何获取所有的子对象(child)
发布日期:2021-06-30 11:40:06
浏览次数:3
分类:技术文章
本文共 2614 字,大约阅读时间需要 8 分钟。
推荐阅读
一、前言
这个问题还是比较简单的,无非就是一个for循环就可以全部获取到了,但是我喜欢简单直达,有没有直接就能获取到所有的子对象函数呢,搜了好久都没有,所以我准备写一个扩展函数,来自己补充这个函数,一起来看一下吧。
二、如何获取所有子对象
第一种方法:
使用foreach循环,找到transform下所有的子物体
foreach(Transform child in transform){ Debug.Log(child.gameObject.name);}
比如说,我有一个父物体:m_ParObj,我如何获取到所有的子对象呢:
using System.Collections;using System.Collections.Generic;using UnityEngine;public class SplitTest : MonoBehaviour{ public GameObject m_ParObj; private void Start() { Listm_Child = new List (); foreach (Transform child in m_ParObj.transform) { //Debug.Log(child.gameObject.name); m_Child.Add(child.gameObject); } }}
这样就将所有的子对象保存了下来。
第二种方法:
通过transform.GetChild(i)来获取到所有的子对象:
for (int i = 0; i < transform.childCount; i++){ Debug.Log(transform.GetChild(i).name);}
比如说,我有一个父物体:m_ParObj,我如何获取到所有的子对象呢:
using UnityEngine;public class SplitTest : MonoBehaviour{ public GameObject m_ParObj; private void Start() { GameObject[] m_Child = new GameObject[m_ParObj.transform.childCount]; for (int i = 0; i < m_Child.Length; i++) { m_Child[i] = m_ParObj.transform.GetChild(i).gameObject; } }}
这样就将所有的子对象保存了下来。
三、使用扩展方法获取所有子对象
总感觉获取个子对象还要用for循环有点麻烦,那么咱们就可以写一个扩展方法,直接获取到所有的子对象
1、首先新建一个MyExtensions.cs脚本
using System.Collections.Generic;using UnityEngine;public static class MyExtensions{ }
2、编写脚本
using System.Collections.Generic;using UnityEngine;public static class MyExtensions{ public static ListGetChild(this GameObject obj) { List tempArrayobj = new List (); foreach (Transform child in obj.transform) { tempArrayobj.Add(child.gameObject); } return tempArrayobj; } public static GameObject[] GetChildArray(this GameObject obj) { GameObject[] tempArrayobj = new GameObject[obj.transform.childCount]; for (int i = 0; i < obj.transform.childCount; i++) { tempArrayobj[i] = obj.transform.GetChild(i).gameObject; } return tempArrayobj; }}
这有两个函数,一个是获取所有子对象的List集合,一个是获取所有子对象的数组集合,按需使用。
扩展方法的使用可以参考我这篇文章:
3、使用扩展方法
使用m_ParObj.GetChild()就可以调用扩展方法:using System.Collections.Generic;using UnityEngine;public class SplitTest : MonoBehaviour{ public GameObject m_ParObj; private void Start() { Listm_Child = m_ParObj.GetChild(); for (int i = 0; i < m_Child.Count; i++) { Debug.Log(m_Child[i].gameObject.name); } }}
这样就可以通过一个函数就可以获取到所有的子对象了。
转载地址:https://itmonon.blog.csdn.net/article/details/110849582 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
感谢大佬
[***.8.128.20]2024年04月14日 11时57分07秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
android数据存储(一)
2021-07-03
android数据存储(二)
2021-07-03
android单元测试流程
2021-07-03
android中远程请求网络接口
2021-07-03
linux释放内存
2021-07-03
android安装apk的Intent
2021-07-03
android下载并安装最新apk
2019-04-30
android中Handler的使用
2019-04-30
android中的异步任务
2019-04-30
android解析json
2019-04-30
android解决ListView图片闪动问题
2019-04-30
android图片拖动并限制区域
2019-04-30
android连续点击两次退出应用
2019-04-30
windows之cmd技巧
2019-04-30
xmind常用快捷键
2019-04-30
android的事件机制
2019-04-30
android中aidl
2019-04-30
android中contentProvider及ContentResolver
2019-04-30
android内容提供者获取联系人练习
2019-04-30