Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 查找游戏对象的子对象的子对象_C#_Unity3d - Fatal编程技术网

C# 查找游戏对象的子对象的子对象

C# 查找游戏对象的子对象的子对象,c#,unity3d,C#,Unity3d,我在场景中有一个预制件,我想访问该预制件的子对象,该预制件的结构如下: PauseMenu UI_Resume TextField TextField2 UI_Side_Back <---- (I need this child) UI_Home tr.Find("Bone"); //or parent.Find("UI_Resume/TextField2/UI_Side_Back"); void Start

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

PauseMenu
    UI_Resume
        TextField
        TextField2
            UI_Side_Back  <---- (I need this child)
    UI_Home
tr.Find("Bone");

//or

parent.Find("UI_Resume/TextField2/UI_Side_Back");
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;
}

我认为它应该是一个递归方法或者其他什么。如何找到该子级?

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

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

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


不久前我还没有为Unity3D开发,但我建议您为对象设置一个标记,并按标记进行搜索。比如:

或者像这样执行find():

PauseMenu
    UI_Resume
        TextField
        TextField2
            UI_Side_Back  <---- (I need this child)
    UI_Home
tr.Find("Bone");

//or

parent.Find("UI_Resume/TextField2/UI_Side_Back");
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;
}

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

PauseMenu
    UI_Resume
        TextField
        TextField2
            UI_Side_Back  <---- (I need this child)
    UI_Home
tr.Find("Bone");

//or

parent.Find("UI_Resume/TextField2/UI_Side_Back");
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;
}
上面的“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;
}

我尝试了所有的解决办法,但没有一个对我有效。使用Unity
Find
不起作用,因为我不知道孩子父母的名字。这里的递归解决方案只有在父母只有一个孩子的情况下才有效,我的情况也不是这样。因此,我创建了以下通用递归查找器,它可以在任何类型的
GameObject
hierarch(或tree)中工作

publicstatictransformrecursivefindchild(transformparent,stringchildname)
{
transformchild=null;
对于(int i=0;i

注意:小心使用,避免使用大型
游戏对象
树。

试试这种递归方法

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;
}
转换GetChildWithName(游戏对象go,字符串childName)
{
transformchild=null;
foreach(在go.GetComponentsInChildren()中转换t)
{
if(t.name==childName)
{
child=t;
打破
}
}
返回儿童;
}

谢谢,这比递归循环要好得多这没关系,但如果将要查找的对象的名称作为方法的参数则更好。由于这一点,它将更加通用,并可用于本项目的其他地方或其他地方。“查找”和了解对象的确切路径是不同的事情。你想要的是对已知路径的引用。@JohnStock噢,真的吗?你是这么想的吗?谢谢你对我五年前的问题发表了非常有帮助的评论@Hossen Rashno这是我的想法,也是事实。这个网站依赖于与问题相匹配的答案,无论是5分钟前还是50年前,都没有区别。这个页面仍然是相关的,仍然被搜索引擎索引。任何在children中搜索“Finding”children的人在这里都不会得到帮助,因为你的问题实际上不是关于查找,而是关于引用一个已知元素。仅仅因为有人指出了纠正或缺陷,就放弃这种态度。需要考虑的一点是,如果父母(go)的名字与孩子的名字相同,它将返回父母。为了避免这种情况,只需添加一个检查,如果它与父项不同