如何在Unity中仅停用所有子对象并仍保持父对象活动?
foreach (Transform child in transform)
child.gameObject.SetActive(false);
//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);
}
试一下这个:
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);
}
}