Object.Destory

Destory(Object)并没有立刻,马上,及时的删除这个Object。

举例

在使用NGUI的Table或Grid进行布局时,就需要注意了:尽量不要使用Destroy 来销毁GameObject,而是使用gameObject.SetActive(false);

建议方法

建议使用setactive(false)替代destory

int max = parent.childCount;
//全部都隐藏
for (int i = 0; i < max; i++)
{
parent.GetChild(i).gameObject.SetActive(false);
}
int idx = 0;
//根据需要显示,并设置值
foreach (CWeaponVo weaponVo in weapons)
{
UILabel atkLabel;
var nature = weaponVo.Info.Nature.ToLower(); if (!propertyLabels.TryGetValue(nature, out atkLabel))
{
newObj = parent.GetChild(idx).gameObject;
newObj.SetActive(true);
sprite = newObj.GetComponent<UISprite>();
sprite.spriteName = iconWpProperty[nature];
atkLabel = GetControl<UILabel>("TotalLabel", newObj.transform);
propertyLabels[nature] = atkLabel;
}
idx++;
}

慎用Destory

//每次Refresh时都销毁之前的
void Refresh()
{
foreach (var obj in PropertyObjList)
{
Destroy(obj);
}
PropertyObjList.Clear();
}
void RenderUI()
{
foreach (CWeaponVo weaponVo in weapons)
{
UILabel atkLabel;
var nature = weaponVo.Info.Nature.ToLower(); //生成新的
if (!propertyLabels.TryGetValue(nature, out atkLabel))
{
newObj = Instantiate(properTemplate) as GameObject;
CBase.Assert(newObj);
newObj.SetActive(true);
CTool.SetChild(newObj, parent.gameObject);
sprite = newObj.GetComponent<UISprite>();
sprite.spriteName = iconWpProperty[nature];
atkLabel = GetControl<UILabel>("TotalLabel", newObj.transform);
propertyLabels[nature] = atkLabel;
PropertyObjList.Add(newObj);
}
}
//重设Table位置
properTable.Reposition();
}

意外后果

使用Destory(obj),在重设Table的位置时,因为Destory不及时 所以残留着之前的child

Instance GameObject

Instance NGUI widget

在Instance 绑有ngui组件的prefab时,建议把gameobject.setActive(false)之后再instance。不然很容易引起多生成一个UICamera。

NGUI版本:3.6.x

生成多的UICamera?

因为ngui的panel在渲染时,会检查组件是否在UICamera下,instance生成的临时对像没有ParentRoot,很容易引起bug。

 

文档资料

文档:http://game.ceeger.com/Script/Object/Object.Destroy.html

Object.Destroy慎用的更多相关文章

  1. Tips9: Destroy( )函数中的 延迟摧毁 功能

    你知道Object.Destroy()函数吗?在脚本中用来摧毁一个游戏物体或组件,可是你知道他能在执行后延迟一段时间后才摧毁物体吗,其实很简单: using UnityEngine; public c ...

  2. Object Pascal中文手册 经典教程

    Object Pascal 参考手册 (Ver 0.1)ezdelphi@hotmail.com OverviewOverview(概述)Using object pascal(使用 object p ...

  3. Unity Destroy和DestroyImmediate

    Destroy(Object obj, float t = 0.0F); 删除一个游戏对象,组件或者资源. 物体obj现在被销毁或在指定了t时间过后销毁.如果obj是组件,它将从GameObject销 ...

  4. Unity基础知识学习笔记二

    1,object Instantiate(object original,Vector3 position,Quaternion rotation)       克隆原始物体,并返回克隆物体.     ...

  5. JQuery Easy Ui DataGrid

    Extend from $.fn.panel.defaults. Override defaults with $.fn.datagrid.defaults. The datagrid display ...

  6. Execution Order of Event Functions

    In Unity scripting, there are a number of event functions that get executed in a predetermined order ...

  7. Unity3D核心类型一览

    Unity3D核心类型一览 本文记录了Unity3D的最基本的核心类型.包括Object.GameObject.Component.Transform.Behaviour.Renderer.Colli ...

  8. TextBox 英文文档

    TextBox Extend from $.fn.validatebox.defaults. Override defaults with $.fn.textbox.defaults. The Tex ...

  9. unity3D技术之事件函数的执行顺序[转]

    unity3D技术之事件函数的执行顺序 转自http://www.yxkfw.com/?p=13703   在unity的脚本,有大量的脚本执行按照预先确定的顺序执行的事件函数.此执行顺序说明如下: ...

随机推荐

  1. angular源码分析:angular源代码的获取与编译环境安装

    一.安装git客户端 1.windows环境推荐使用TortoiseGit. 官网地址:http://tortoisegit.org 下载地址:http://tortoisegit.org/downl ...

  2. SAP打印机配置

    SAP打印机配置 一.SAP打印原理 SAP的打印过程分两个步骤: 1.创建假脱机请求: 2.创建输出请求: 在点击打印按钮后,系统会提示创建假脱机请求后,你可以选择直接生成输出请求,或者手动生成输出 ...

  3. 定时从多个Excel导入数据到SQL数据库

    Scheduling Data Imports in SQL Server Importing data into a SQL Server database isn't really that tr ...

  4. elipse + maven + tomcat + springMVC环境搭建

    1. java JDK安装 http://jingyan.baidu.com/article/b24f6c82c989da86bfe5dab2.html 2.eclipse安装 http://jing ...

  5. 3D Touch介绍:电子秤App与快捷操作

    随着iPhone6s与6s plus的到来,苹果给我们展现了一种全新的交互方式:重按手势.你可能知道,这个特性已经在Apple Watch和MacBook上推出了,不过那时叫Force Touch,就 ...

  6. error=Error Domain=NSURLErrorDomain Code=-1003

    我的情况:模拟器 可以返回数据 .真机不可以.我手机连接的同事的共享,我以为他的网段和后台的网段在同一个网段.一直在找错误,打开手机网络设置一看  ,原来不在同一网段.手机的网络必须要跟PC的在同一个 ...

  7. 查看特定View的默认属性值

    当我在分析focus.touch事件处理代码时发现,有些属性对代码的逻辑有非常重要的影响,比如clickable.focusable 这些属性.这时我们自然而然的想到,那么这些属性的默认值是什么呢?在 ...

  8. CEF3可行性

    Chromium Embedded Framework 顾名思义,内嵌式CHROME,详细的介绍参阅 http://yogurtcat.com/posts/cef/hello-cef.html 为什么 ...

  9. C#问题

    1.结构体里面是否可以有属性? 可以有属性.实测代码以及截图. In C#, we can use the following statement to convert a string s to a ...

  10. Flume应用场景及架构原理

    Flume概念 Flume是一个分布式.可靠.和高可用的海量日志聚合的系统,支持在系统中定制各类数据发送方,用于收集数据:同时,Flume提供对数据进行简单处理,并写到各种数据接受方(可定制)的能力. ...