unity中利用深度递归查找子物体

我所遇到的问题是:如何在层级关系未知的情况下,查找某一特定名称的子物体
代码如下:

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

public class FindChild : MonoBehaviour  
{
    private void OnGUI()
    {
        if (GUILayout.Button("FindChild"))
        {
            //Debug.Log("找到了");
            Transform tt =CheckChild(this.transform,c1);
            if (tt)
            {
                Debug.Log("找到了");//这句可以改成你所要进行的操作
            }
            else
            {
                Debug.Log("未找到");     
            }
        }
    }
    public  Transform CheckChild(Transform TF,string childName)
    {
        Transform x = TF.Find(childName);//查找名字为childName的子物体
        if (x!=null)
        {
            return x;
        }
        if (TF.childCount == 0)
        {
            return null; 
        }
        for (int i = 0; i 

你可能感兴趣的:(unity中利用深度递归查找子物体)