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