UNITY Destroy()和DestroyImadiate()的区别
UNITY Destroy()和DestroyImadiate()的区别
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 framevoid Update () {if (Input.GetKey(KeyCode.LeftControl)){var go = Instantiate(cubeGo);go.transform.SetParent(rootTrans);}else if (Input.GetKey(KeyCode.A)){//11111111111for (int i = 0; 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)){//2222222222while (true){var cc = rootTrans.childCount;//UnityEngine.Debug.Log("cc============================" + cc);if (cc == 0)break;timer.Reset();timer.Start();DestroyImmediate(rootTrans.GetChild(0).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 = 0; i < root.childCount; ++i){//方式一,正确删除所有子结点var c = root.GetChild(i).gameObject;GameObject.Destroy(c);}for (var i = 0; i < root.childCount; ++i){//方式二,删不干净,会有一些剩余节点var c = root.GetChild(0).gameObject;GameObject.DestroyImmediate(c);}for (var i = 0; i < root.childCount; ++i){//方式三,删不干净,会有一些剩余节点,只删除了偶数节点0,2,4,6,剩余奇数节点1,3,5,7var c = root.GetChild(i).gameObject;GameObject.DestroyImmediate(c);}while (root.childCount > 0){//方式四,正确删除所有子结点var c = root.GetChild(0).gameObject;GameObject.DestroyImmediate(c);}
for (int i = go.childCount - 1; i >= 0; i--) {//方式五,正确删除所有子结点
GameObject.DestroyImmediate(go.GetChild (i).gameObject);
}
}
posted on 2017-04-07 19:07 时空观察者9号 阅读(...) 评论(...) 编辑 收藏
UNITY Destroy()和DestroyImadiate()的区别相关推荐
- UNITY Destroy()和DestroyImadiate()都不会立即释放对象内存
UNITY Destroy()和DestroyImadiate()都不会立即释放对象内存 如题,destroyimadiate是立即将物体从场景hierachy中移除,并标记为 "null& ...
- Destroy 和 DestroyImmediate 的区别
Destroy 和 DestroyImmediate 的区别 使用destroy时,对象不会被马上删除. Debug.log("删除前的子节点数量为:"+parent.childC ...
- unity, destroy gameObject destroy all children
一,destroy gameObject 删除名为xxx的gameObject 错误方法1: Destroy(xxx); 以上方法之所以错误,是因为Destroy在下一帧才生效,而在本帧之内xxx还存 ...
- Unity3D中Enabled、Destroy与Active的区别
Unity3D游戏对象消失三种方法的区别: 1.gameObject.active:是否在场景中停用该物体,在你gameObject.active =false中,则你在场景中用find找不到该物体. ...
- Unity Mono和IL2CPP的区别
*目录 Unity是如何实现跨平台的? Mono介绍 IL2CPP介绍 Mono与IL2CPP的区别* 一.Unity是如何实现跨平台的? 跨平台:一次编译,不需要任何代码修改,应用程序就可以运行在任 ...
- Delphi的对象注销方法Destroy和free的区别
当您使用完对象后,您应该及时撤销它,以便把这个对象占用的内存释放出来.您可以通过调用一个注销方法来撤销您的对象,它会释放分配给这个对象的内存. Delphi的注销方法有两个:Destroy和Free. ...
- Unity之Update与FixedUpdate区别
下面这段代码演示游戏暂停 using UnityEngine; using System.Collections;public class GamePauseTest : MonoBehaviour ...
- wxpython wx.Destroy()和wx.Close()区别
在官方文档中有这样一段描述 Note that calling Close does not guarantee that the window will be destroyed; but it p ...
- Unity中SetPassCall, Batches的区别,它们与Draw call 的关系
先放官方对于Draw call batch的解释:https://docs.unity3d.com/Manual/DrawCallBatching.html 在Unity 的优化中,我们经常说要优化D ...
最新文章
- 我要学ASP.NET MVC 3.0(一): MVC 3.0 的新特性
- 【CyberSecurityLearning 45】PHP基础+变量、运算符、流程控制语句
- 《赛博朋克2077》是如何将人物表情和口型本地化的?
- matlab数组存字符串,MATLAB字符串数组存储为CSV格式
- C/C++——一些与输入有关的istream类成员函数
- JAVA内存模型与线程安全
- E20170618-hm
- 开课吧:什么是包?如何定义包?
- unity透明通道加颜色_关于Unity伽马校正的一点笔记
- 大数据导论章节答案_智慧树APP大数据导论第三单元章节测试答案
- wpa_supplicant介绍
- 魔兽世界燃烧的远征服务器状态,魔兽世界燃烧的远征前夕补丁 燃烧的远征副本介绍...
- 小狮子荣光不复 瑞星信息去年亏损7300万元
- android v2签名机制,APK签名机制之——V2签名机制详解
- 国产开源「文本-视频生成」模型!免费在线体验,一键实现视频生成自由
- 第九届玲珑轻院校赛随笔
- 电脑变慢,4K对齐来解决
- 科技部原副部长吴忠泽:尽早抢占元宇宙高地,掌握下一代互联网的话语权和主动权
- 397_压缩图片到一定大小(质量)
- 100%都会感染,第一波症状最重,后面会轻。家里一定要备这些东西!