[英]How can I get the prefan parent of any child and then search for specific childs?
[英]How can I find gameobject with tag that are childs of a specific parent?
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
你有沒有嘗試過類似的東西
GameObject[] doorsLeft = GameObject.FindGameObjectsWithTag(c_doorLeft).Where(o => o.transform.parent.name == "Wall_Door_Long_01");
作為示例,您可能想要使用字符串比較而不是 == 但是。
您可以檢查該對象的所有子項。
List<GameObject> doorsLeft = new List<GameObject>();
foreach (Transform child in transform){
if (child.tag == tag){
doorsLeft.add(child.gameObject);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.