一,destroy gameObject

删除名为xxx的gameObject

错误方法1:

Destroy(xxx);

又比如本帧内需要正确的childCount值,则上面方法也不行。

错误方法2:
        xxx.transform.parent=null;

  Destroy(xxx);

        
正确的方法:

DestroyImmediate(xxx);

  

二,destroy all children

删除node的所有子节点

正确的方法:
        List<GameObject> childList = new List<GameObject> ();
        int childCount = node.transform.childCount;
        for (int i=0; i<childCount; i++) {
            GameObject child=node.transform.GetChild(i).gameObject;
            childList.Add(child);
        }
        for (int i=0; i<childCount; i++) {
            DestroyImmediate(childList[i]);
        }
        (node.transform.childCount == 0).MustBeTrue ();

 

相关文章: