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. 给SHP文件定义投影

    #!/usr/bin/env python # -*- coding: utf-8 -*- import urllib.request import os def get_epsg_code(epsg ...

  2. SharePoint 2013 REST 服务使用简介

    1.创建测试使用列表”REST Demo”,插入一些测试数据,如下图: 2.添加内容编辑器,并且添加脚本引用以及HTML代码,如下图: Result的Div为显示结果使用,input标签触发REST服 ...

  3. 高性能JS笔记4——算法和流程控制

    一.循环 for.while.do while三种循环的性能都没有多大区别.foreach 的性能较其他三种差 . 既然循环没有多大区别,注意循环内的代码控制. 减少迭代次数. 减少迭代工作量. 推荐 ...

  4. 之二:CAKeyframeAnimation - 关键帧动画

    是CApropertyAnimation的子类,跟CABasicAnimation的区别是:CABasicAnimation只能从一个数值(fromValue)变到另一个数值(toValue),而CA ...

  5. iOS 设备标识

    //UUID    NSLog(@"-=-=-=-=-=-=-=-==-=-==qqqqqqqqqqqqqqqqqqqqqqq:::::::::::%@", [[NSUUID UU ...

  6. 【代码笔记】iOS-3个section,每个都有header.

    一,效果图: 二,工程目录. 三,代码 RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController ...

  7. 【读书笔记】iOS网络-负载

    负载指的是在服务的请求响应事务中交换的数据.常见的负载格式包括XML,JSON与HTML. 进入与发出的负载数据存在很多形式与大小.比如,有些开发者会使用原生的字符串或是以分隔符分开的数据与Web S ...

  8. 我的android学习经历5

    android在strings.xml文件中,写string对象时,如何加入空格 <string name="password">密    码:</string& ...

  9. Runnable,Thread实现多线程以及Runnable的同步资源共享

    (一) 实现多线程有两种方式 (1) 继承Thread类,重写run()方法,如以下例子 class MyThread extends Thread{ public void run(){ // } ...

  10. IrfanView 4.36 中文版发布了

    IrfanView 4.36 简体中文便携版 小而快的图片浏览器 仅仅不到2M的小软件,功能却能与体积大到几十M的ACDSee相媲美!这个软件就是IrfanView.图片.音频.视频浏览,图片批量格式 ...