前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用Unity获取所有子对象及拓展方法的使用

使用Unity获取所有子对象及拓展方法的使用

作者头像
恬静的小魔龙
发布2022-08-07 09:07:23
2.4K0
发布2022-08-07 09:07:23
举报
文章被收录于专栏:Unity3DUnity3D

一、前言

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

二、如何获取所有子对象

第一种方法:

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

代码语言:javascript
复制
foreach(Transform child in transform)
{
    Debug.Log(child.gameObject.name);
}

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

代码语言:javascript
复制
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);
        }
    }
}

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

第二种方法:

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

代码语言:javascript
复制
for (int i = 0; i < transform.childCount; i++)
{
    Debug.Log(transform.GetChild(i).name);
}

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

代码语言:javascript
复制
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脚本

代码语言:javascript
复制
using System.Collections.Generic;
using UnityEngine;

public class MyExtensions : MonoBehaviour
{

}

加上static标识符,然后去掉引用MonoBehaviour

代码语言:javascript
复制
using System.Collections.Generic;
using UnityEngine;

public static class MyExtensions 
{

}

2、编写脚本

代码语言:javascript
复制
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;
    }
}

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

3、使用扩展方法

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

代码语言:javascript
复制
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);
        }
    }
}

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-06-30,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、前言
  • 二、如何获取所有子对象
    • 第一种方法:
      • 第二种方法:
      • 三、使用扩展方法获取所有子对象
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档