如何在Unity中停用所有子对象?

8
如何在Unity中仅停用所有子对象并仍保持父对象活动?
3个回答

17
foreach (Transform child in transform)
    child.gameObject.SetActive(false);

1
比已检查答案更短! - Jeffery Tang

9
//Assuming parent is the parent game object
for (int i = 0; i < parent.transform.childCount; i++)
{
    var child = parent.transform.GetChild(i).gameObject;
    if (child != null)
        child.SetActive(false);
}

非常感谢,这正是我正在寻找的 =) - AuroraBlaze

0

试一下这个:

public void DisableChildren()  
{    
    foreach (Transform child in transform)     
    {  
        child.gameObject.SetActiveRecursively(false);   
    }   
}

JS 版本(如有需要):

function DisableChildren()   
{   
    for (var child : Transform in transform)    
    {   
        child.gameObject.SetActiveRecursively(false);  
    }  
}

1
SetActiveRecursively在Unity 4中已经过时。 - Roberto

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接