如何找到带有特定父级子代标签的游戏​​对象? [英] How can I find gameobject with tag that are childs of a specific parent?

查看:19
本文介绍了如何找到带有特定父级子代标签的游戏​​对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public void UpdateOrAddShaderPrefabToDoors()
    {
        GameObject[] doorsLeft = GameObject.FindGameObjectsWithTag(c_doorLeft);
        GameObject[] doorsRight = GameObject.FindGameObjectsWithTag(c_doorRight);

        List<GameObject> allDoors = doorsLeft.Union(doorsRight).ToList();

        HashSet<GameObject> prefabParentsOfDoorsNeedRemove = new HashSet<GameObject>();
        allDoors.ForEach(doorGameObject =>
        {
            List<GameObject> shadersChildren = new List<GameObject>();
            for (int i=0; i<doorGameObject.transform.childCount ;i++)
            {
                if (doorGameObject.transform.GetChild(i).name.StartsWith(c_doorShieldFxLocked))
                {
                    shadersChildren.Add(doorGameObject.transform.GetChild(i).gameObject);
                }
            }
            foreach (GameObject shader in shadersChildren)
            {
                GameObject outermostPrefabInstanceRoot = PrefabUtility.GetOutermostPrefabInstanceRoot(shader);
                prefabParentsOfDoorsNeedRemove.Add(outermostPrefabInstanceRoot);
            }
        });

        foreach (GameObject parent in prefabParentsOfDoorsNeedRemove)
        {
            Modify(parent, RemoveFunc);
        }

        HashSet<GameObject> prefabParentsOfDoors = new HashSet<GameObject>();
        allDoors.ForEach(doorGameObject =>
        {
            GameObject outermostPrefabInstanceRoot = PrefabUtility.GetOutermostPrefabInstanceRoot(doorGameObject);
            prefabParentsOfDoors.Add(outermostPrefabInstanceRoot);
        });

        foreach (GameObject parent in prefabParentsOfDoors)
        {
            AddShaderToPrefab(parent);
        }
    }

什么时候做:

GameObject[] doorsLeft = GameObject.FindGameObjectsWithTag(c_doorLeft);
GameObject[] doorsRight = GameObject.FindGameObjectsWithTag(c_doorRight);

它会找到所有的Door_Left"和Door_Right",但其中一些是其他游戏对象的孩子,我只想找到左边的门和右边的门,这些门是Wall_Door_Long_01的孩子

It will find all "Door_Left" and "Door_Right" but some of them are childs of other gameobjects and I want to find the door left and door right only that are childs of : Wall_Door_Long_01

推荐答案

你是否尝试过类似的事情

Have you tried something like

GameObject[] doorLeft = GameObject.FindGameObjectsWithTag(c_doorLeft).Where(o => o.transform.parent.name == "Wall_Door_Long_01");

您可能想使用字符串比较而不是 == 但是,作为示例.

You might want to use string compare rather than == but, as an example.

这篇关于如何找到带有特定父级子代标签的游戏​​对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆