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. ArcPy之Python介绍

    1.Python简介 Python是一种面向对象.解释型计算机程序设计语言;Python是一种简单易学,功能强大的编程语言.它有高效率的高层数据结构,简单而有效地实现面向对象编程.Python简洁的语 ...

  2. Mac OX上安装MongoDb

    MongoDB的安装有好多种安装方法,有普通青年的HomeBrew方式,也有文艺青年的源码编译方式.我只想快速的装起来用一下,所以我选最简单的HomeBrew. 请参考官方文档 : http://do ...

  3. 关于asp.net impersonation的一些谣传

    以下皆是理论知识,未经证实. system.web下面的配置节 <identity impersonate="true" /> 或者<identity imper ...

  4. Understanding the Uncertain Geographic Context Problem

    "The areal units (zonal objects) used in many geographical studies are arbitrary, modifiable, a ...

  5. RecyclerView局部刷新那点事

    1.局部刷新的引入 提到RecyclerView,我们首先想到的是ListView,对于ListView的局部刷新,我们之前已经有解决方案,[android:ListView的局部刷新]当时的解决方案 ...

  6. RxJava 和 RxAndroid 一 (基础)

    1.RxJava 项目地址 https://github.com/ReactiveX/RxJava 2.RxAndroid 项目地址    https://github.com/ReactiveX/R ...

  7. ReactiveCocoa学习总结

    最近一直断断续续学习关于ReactiveCocoa的知识内容,对于它的一些基础内容将通过本文进行一个总结,主要是一些入门知识 一:RACSignal一些运用 @interface RACSignalT ...

  8. Java你可能不知道的事系列(1)

    概述 本类文章会不段更新分析学习到的经典面试题目,在此记录下来便于自己理解.如果有不对的地方还请各位观众拍砖. 今天主要分享一下常用的字符串的几个题目,相信学习java的小伙伴们对String类是再熟 ...

  9. OC NSString(字符串)

    OC NSString(字符串) 多行文字字面量 NSString * string = @"abC" @"DEF" @"hjk" @&qu ...

  10. iOS & Mac JSON To Model

    NSString * jsonPath=[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Cont ...