查找gameObject的子对象的子对象

本文关键字:对象 gameObject 查找 | 更新日期: 2023-09-27 18:20:04

我在场景中有一个预制件,我想访问该预制件的子级,该预制件结构如下:

PauseMenu
    UI_Resume
        TextField
        TextField2
            UI_Side_Back  <---- (I need this child)
    UI_Home

transform.FindChild只返回一级子级,该变换中的loop也在一级子代中循环:

foreach (Transform item in PooledPause.transform) {
        Debug.Log(item.name);
}

我认为它需要是一个递归方法或其他什么。我怎样才能找到那个孩子?

查找gameObject的子对象的子对象

您可以使用路径来查找转换:

 var target = transform.Find("UI_Resume/TextField2/UI_Side_Back");

来自Transform.Find:的文档

如果名称包含"/"字符,它将像路径名称一样遍历层次结构。

上面的"RecursiveChildFind"不起作用,因为它只搜索一个子项,而不是所有子项。工作版本如下:

Transform RecursiveFindChild(Transform parent, string childName)
{
    foreach (Transform child in parent)
    {
        if(child.name == childName)
        {
            return child;
        }
        else
        {
            Transform found = RecursiveFindChild(child, childName);
            if (found != null)
            {
                    return found;
            }
        }
    }
    return null;
}

这里还有另一个解决方案,可以让您基于任何标准在任何深度中找到孩子。它采用深度优先的方法。因此,我建议将感兴趣的对象放置在尽可能高的树上,以节省一些操作。

它是这样使用的:parent.FindRecursive("child");

或者,如果您需要更大的灵活性:parent.FindRecursive(child => child.GetComponent<SpriteRenderer>() != null && child.name.Contains("!"));

using System;
using UnityEngine;
public static class TransformExtensions
{
    public static Transform FindRecursive(this Transform self, string exactName) => self.FindRecursive(child => child.name == exactName);
    public static Transform FindRecursive(this Transform self, Func<Transform, bool> selector)
    {
        foreach (Transform child in self)
        {
            if (selector(child))
            {
                return child;
            }
            var finding = child.FindRecursive(selector);
            if (finding != null)
            {
                return finding;
            }
        }
        return null;
    }
}

我尝试了所有的解决方案,但都不起作用。使用Unity Find不起作用,因为我不知道孩子父母的名字。这里的递归解决方案只有当你的父母只有一个孩子时才有效,但我的情况并非如此。因此,我创建了以下通用递归查找器,可以在任何类型的GameObject层次结构(或树)中工作。

public static Transform RecursiveFindChild(Transform parent, string childName)
{
    Transform child = null;
    for (int i = 0; i < parent.childCount; i++)
    {
        child = parent.GetChild(i);
        if (child.name == childName)
        {
            break;
        }
        else
        {
            child = RecursiveFindChild(child, childName);
            if (child != null)
            {
                break;
            }
        }
    }
    return child;
}

注意:小心使用,避免使用大的GameObject树。

我之前没有为Unity3D开发,但我建议您为对象设置一个标记,并根据该标记进行搜索。像这样:

  public static T FindComponentInChildWithTag<T>(this GameObject parent, string tag)where T:Component{
       Transform t = parent.transform;
       foreach(Transform tr in t)
       {
              if(tr.tag == tag)
              {
                   return tr.GetComponent<T>();
              }
              else
              {
                   tr.GetComponent<T>().FindComponentInChildWithTag(tag);
              }
       }
  }

或者像这样。

但如果你真的想按名字搜索,你可以把以前的代码改成这样:

if(tr.name == "object name")

或者像这样做find():

tr.Find("Bone");
//or
parent.Find("UI_Resume/TextField2/UI_Side_Back");

感谢Dan Puzey的回答,如果你想在对象上进行递归循环,你可以这样实现:

void Start()
{
    GameObject theChild = RecursiveFindChild (theParentGameObject.transform, "The Child Name You Want");
}
GameObject RecursiveFindChild(Transform parent, string childName)
{
    foreach (Transform child in parent) {
        if(child.name == childName)
            return child.gameObject;
        else 
            return RecursiveFindChild(child, childName);
    }
    return null;
}

尝试这个递归方法。

public static Transform RecursiveFindChild(Transform parent, string childName)
{
    Transform result = null;
    foreach (Transform child in parent)
    {
        if (child.name == childName)
            result = child.transform;
        else
            result = RecursiveFindChild(child, childName);
        if (result != null) break;
    }
    return result;
}

如果您不知道父级的名称,则可以使用以下方法来实现这一点:

Transform GetChildWithName(GameObject go, string childName)
{
    Transform child = null;
    foreach (Transform t in go.GetComponentsInChildren<Transform>())
    {
        if (t.name == childName)
        {
            child = t;
            break;
        }
    }
    return child;
}

还可以加入我的版本。如何按名称查找Child Child。

更新循环不友好。

public GameObject FindChildGameObjectByName(GameObject go, string gameObjectName)
{
    GameObject retrunGO = null;
    for (int i = 0; i < go.transform.childCount; i++)
    {
        if (go.transform.GetChild(i).name == gameObjectName)
        {
            retrunGO = go.transform.GetChild(i).gameObject;
        }
        GameObject tmp = FindChildGameObjectByName(go.transform.GetChild(i).gameObject, gameObjectName);
        retrunGO = (tmp != null ? tmp : retrunGO);
    }
    return retrunGO;
}