using System.Collections;
using System.Collections.Generic;
using System.Timers;
using UnityEngine;
using System.Diagnostics;
public class testdestroy : MonoBehaviour { GameObject cubeGo;
Transform rootTrans;
// Use this for initialization Stopwatch timer;
void Start () {
cubeGo = GameObject.Find("Cube");
rootTrans = GameObject.Find("root").transform; timer = new Stopwatch();
} // Update is called once per frame
void Update () {
if (Input.GetKey(KeyCode.LeftControl))
{
var go = Instantiate(cubeGo);
go.transform.SetParent(rootTrans); }
else if (Input.GetKey(KeyCode.A))
{//11111111111
for (int i = ; i < rootTrans.GetChildCount(); ++i)
{
timer.Reset();
timer.Start();
Destroy(rootTrans.GetChild(i).gameObject);
timer.Stop(); UnityEngine.Debug.Log("1:time elipsed===:" + timer.Elapsed);
} rootTrans.DetachChildren();
}
else if (Input.GetKey(KeyCode.D))
{//2222222222
while (true)
{
var cc = rootTrans.childCount;
//UnityEngine.Debug.Log("cc============================" + cc);
if (cc == )
break;
timer.Reset(); timer.Start();
DestroyImmediate(rootTrans.GetChild().gameObject);
timer.Stop(); UnityEngine.Debug.Log("2:time elipsed===:" + timer.Elapsed);
} }
else if (Input.GetKey(KeyCode.F))
{
DestroyImmediate(cubeGo.transform);
}
}
}
1,DestroyImmediate立即销毁目标,并将目标置为null,且将目标的所有上层引用置空,如用DestroyImmediate销毁OBJ下的所子物体后 OBJ.childcount将变为0,见代码//22222222222
2,Destroy则在本帧结束前,渲染之前销毁目标和上层引用。不会立即销毁,Destroy调用后,目标数据仍然存在,不为null,上层引用也正常。见代码//111111111处,因为销毁物体后,childcount
仍然保持不变,这常会造成其它地方的逻辑出错,子结点已经销毁了却还能取到,因此,我们应该将子结点从父物体摘除,rootTrans.DetachChildren();这样其它地方代码再获取子结点及其数量时就不导致逻辑出错了。
3,基于上述原理,测试Destroy和DestroyImmediate的时间占用,发现后者几乎是前者的10倍。

补充:
    void Start () {
var root = gameObject.transform;
for (var i = ; i < root.childCount; ++i)
{//方式一,正确删除所有子结点
var c = root.GetChild(i).gameObject;
GameObject.Destroy(c);
}
for (var i = ; i < root.childCount; ++i)
{//方式二,删不干净,会有一些剩余节点
var c = root.GetChild().gameObject;
GameObject.DestroyImmediate(c);
}
for (var i = ; i < root.childCount; ++i)
{//方式三,删不干净,会有一些剩余节点,只删除了偶数节点0,2,4,6,剩余奇数节点1,3,5,7
var c = root.GetChild(i).gameObject;
GameObject.DestroyImmediate(c);
}
while (root.childCount > )
{//方式四,正确删除所有子结点
var c = root.GetChild().gameObject;
GameObject.DestroyImmediate(c);
}
  

      for (int i = go.childCount - 1; i >= 0; i--) {//方式五,正确删除所有子结点
        GameObject.DestroyImmediate(go.GetChild (i).gameObject);
      }

    }

最新文章

  1. 使用nodejs爬取拉勾苏州和上海的.NET职位信息
  2. DB2常用sql函数 (转载)
  3. 给大一的学弟学妹们培训java web的后台开发讨论班计划
  4. php 通过curl post发送json数据实例
  5. Loadrunner在场景中添加多个负载机报错:Action.c(38): Error -26488: Could not obtain information about submitted解决方法
  6. Oracle中建立表
  7. UAT测试,PPT测试
  8. How a woman&#39;s glance can tell a man if she&#39;s interested
  9. UVa572 - Oil Deposits
  10. hdoj 2202 最大三角形
  11. Linux下安装和配置java开发环境(jdk/eclipse/tomcat)
  12. DHCP Option 60 的理解
  13. LeetCode:链表排序
  14. flume+kafka+storm单机部署
  15. WPF文本框只允许输入数字
  16. 10个JavaScript常见BUG及修复方法
  17. javaweb笔记09—(session会话及验证码问题)
  18. sql中的in与not in,exists与not exists的区别
  19. 使用swoole编写简单的echo服务器
  20. centos安装man中文手册

热门文章

  1. wikioi 1021 玛丽卡
  2. 055——VUE中vue-router之路由参数的随意设置与伪静态链接地址处理:
  3. HDU 4686 Arc of Dream 矩阵快速幂,线性同余 难度:1
  4. SGU 144. Meeting 概率dp 几何概率分布 难度:0
  5. bzoj2134
  6. Python基础学习----参数和返回值
  7. 代码控制 textarea 控件是否为KindEditor 编辑框
  8. mongodb数据处理工具
  9. streamsets Executors 说明
  10. 深入理解java虚拟机,类加载