查询父节点。
这个只能查出子节点,不能遍历孙子节点。遍历索引。
按名字查找
Transform child = this.transform.Find("bb");
if (child != null)
{
Debug.Log("找到了节点");
}
else
{
Debug.Log("没找到节点");
}
如果是顶级节点,他的父级就是空。
---------------------
修改父节点。
下方代码可以设置节点状态。
//设置为一级节点,其中/为根目录
this.transform.SetParent(null);
Transform child = this.transform.Find("bb");
if(child.gameObject.activeSelf)
{
//把节点设为不可见
child.gameObject.SetActive(false);
}
else
{
//把节点设为可见
child.gameObject.SetActive(true);
}