许可优化
产品
解决方案
服务支持
关于
软件库
当前位置:服务支持 >  软件文章 >  【Unity3D日常开发】如何获取所有的子对象(child)

【Unity3D日常开发】如何获取所有的子对象(child)

阅读数 5
点赞 0
article_banner

推荐阅读

大家好,我是佛系工程师☆恬静的小魔龙☆,不定时更新Unity开发技巧,觉得有用记得一键三连哦。

一、前言

这个问题还是比较简单的,无非就是一个for循环就可以全部获取到了,但是我喜欢简单直达,有没有直接就能获取到所有的子对象函数呢,搜了好久都没有,所以我准备写一个扩展函数,来自己补充这个函数,一起来看一下吧。

二、如何获取所有子对象

第一种方法:

使用foreach循环,找到transform下所有的子物体

foreach(Transform child in transform)
{
	Debug.Log(child.gameObject.name);
}
csharp
运行
  • 1
  • 2
  • 3
  • 4

比如说,我有一个父物体:m_ParObj,我如何获取到所有的子对象呢:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SplitTest : MonoBehaviour
{
    public GameObject m_ParObj;

    private void Start()
    {
        List<GameObject> m_Child = new List<GameObject>();
        foreach (Transform child in m_ParObj.transform)
        {
            //Debug.Log(child.gameObject.name);
            m_Child.Add(child.gameObject);
        }
    }
}
csharp
运行
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

这样就将所有的子对象保存了下来。

第二种方法:

通过transform.GetChild(i)来获取到所有的子对象:

for (int i = 0; i < transform.childCount; i++)
{
	Debug.Log(transform.GetChild(i).name);
}
csharp
运行
  • 1
  • 2
  • 3
  • 4

比如说,我有一个父物体: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;
        }
    }
}

csharp
运行
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

这样就将所有的子对象保存了下来。

三、使用扩展方法获取所有子对象

总感觉获取个子对象还要用for循环有点麻烦,那么咱们就可以写一个扩展方法,直接获取到所有的子对象

1、首先新建一个MyExtensions.cs脚本

using System.Collections.Generic;
using UnityEngine;

public static class MyExtensions
{

}

csharp
运行
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

2、编写脚本

using System.Collections.Generic;
using UnityEngine;

public static class MyExtensions
{
    public static List<GameObject> GetChild(this GameObject obj)
    {
        List<GameObject> tempArrayobj = new List<GameObject>();
        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;
    }
}
csharp
运行
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

这有两个函数,一个是获取所有子对象的List集合,一个是获取所有子对象的数组集合,按需使用。

扩展方法的使用可以参考我这篇文章:
【Unity3D日常开发】扩展方法的使用

3、使用扩展方法
使用m_ParObj.GetChild()就可以调用扩展方法:

using System.Collections.Generic;
using UnityEngine;

public class SplitTest : MonoBehaviour
{
    public GameObject m_ParObj;

    private void Start()
    {
        List<GameObject> m_Child = m_ParObj.GetChild();
        for (int i = 0; i < m_Child.Count; i++)
        {
            Debug.Log(m_Child[i].gameObject.name);
        }
    }
}
csharp
运行
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

这样就可以通过一个函数就可以获取到所有的子对象了。

Unity爱好者社区 分享Unity相关的技术文章、工具资源等。

微信公众号


免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删
相关文章
QR Code
微信扫一扫,欢迎咨询~

联系我们
武汉格发信息技术有限公司
湖北省武汉市经开区科技园西路6号103孵化器
电话:155-2731-8020 座机:027-59821821
邮件:tanzw@gofarlic.com
Copyright © 2023 Gofarsoft Co.,Ltd. 保留所有权利
遇到许可问题?该如何解决!?
评估许可证实际采购量? 
不清楚软件许可证使用数据? 
收到软件厂商律师函!?  
想要少购买点许可证,节省费用? 
收到软件厂商侵权通告!?  
有正版license,但许可证不够用,需要新购? 
联系方式 155-2731-8020
预留信息,一起解决您的问题
* 姓名:
* 手机:

* 公司名称:

姓名不为空

手机不正确

公司不为空