Unity Destory

Object.Destroy

public static function Destroy(obj: Object, t: float = 0.0F): void;

public static void Destroy(Object obj, float t = 0.0F);

Parameters

obj The object to destroy.
t The optional amount of time to delay before destroying the object.

Description

Removes a gameobject, component or asset.

The object obj will be destroyed now or if a time is specified t seconds from now. If obj is a Component it will remove the component from the GameObject and destroy it. If obj is a GameObject it will destroy the GameObject, all its components and all transform children of the GameObject. Actual object destruction is always delayed until after the current Update loop, but will always be done before rendering.

    // Kills the game object
    Destroy (gameObject);

// Removes this script instance from the game object
    Destroy (this);

// Removes the rigidbody from the game object
    Destroy (rigidbody);

// Kills the game object in 5 seconds after loading the object
    Destroy (gameObject, 5);

// When the user presses Ctrl, it will remove the script
    // named FooScript from the game object
    function Update () {
        if (Input.GetButton ("Fire1") && GetComponent (FooScript))
            Destroy (GetComponent (FooScript));
    }
// Kills the game object
Destroy(gameObject);

// Removes this script instance from the game object
Destroy(this);

// Removes the rigidbody from the game object
Destroy(rigidbody);

// Kills the game object in 5 seconds after loading the object
Destroy(gameObject, 5);

// When the user presses Ctrl, it will remove the script
// named FooScript from the game object
void Update()
{
    if (Input.GetButton("Fire1") && GetComponent<FooScript>())
    {
        Destroy(GetComponent<FooScript>());
    }
}

Destroy is inherited from the UnityEngine.Object base class. Javascript users should consider making a call to UnityEngine.Object.Destroy, rather than Object.Destroy to avoid references being resolved to the .Net System.Object class.

Note :  Destory 继承于UnityEngine  所以如果Destory出现下图情况

可以考虑使用 UnityEngine.Object.Destroy (gameObject);

时间: 2024-08-28 08:42:48

Unity Destory的相关文章

Unity的Destory延迟删除的问题

由于unity的destroy方法是有延迟的,也就是说他并不是马上就会摧毁,也就是说迩调用destroy方法后,实际上他还是存在的,只是在下一帧的时候,他没了,如果迩是想执行一个操作把某个gameObject的物体下的子物体清空再添加新的子物体的话.先destory 再 add的方法是不行的.那么我们可以采取先把之前的物体隐藏然后再把它加入一个摧毁的数组里.这样就可以先添加新物体了.然后在添加完子物体后再集中去把摧毁数组里的gameObject都摧毁掉 说白了就是不能在同一帧里,删除一个Game

C#程序员整理的Unity 3D笔记(九):Unity 3D测试浅析

Unity中的组件上的类,是不能new的,必须通过组件去获取,new出来的东西是null. if (amainfo.nameHash == Animator.StringToHash(Base Layer.idle)) { m_animator.SetBool(Run, true); } 代码无法执行时,在navigation面板中点击Bake,如下图 必须为碰撞体添加碰撞组件,否则Physics.Raycast函数无法检测到 删除destory时的时间,否则产生的多线程会导致计数不准确! Un

unity内存管理

最近一直在研究unity的内存加载,因为它是游戏运行的重中之重,如果不深入理解和合理运用,很可能导致项目因内存太大而崩溃. 详细说一下细节概念:AssetBundle运行时加载:来自文件就用CreateFromFile(注意这种方法只能用于standalone程序)这是最快的加载方法也可以来自Memory,用CreateFromMemory(byte[]),这个byte[]可以来自文件读取的缓冲,www的下载或者其他可能的方式.其实WWW的assetBundle就是内部数据读取完后自动创建了一个

Unity资源解决方案之AssetBundle

1.什么是AssetBundle AssetBundle是Unity pro提供的一种用来存储资源的文件格式,它可以存储任意一种Unity引擎能够识别的资源,如Scene.Mesh.Material.Texture.Audio.noxss等等,同时,AssetBundle也可以包含开发者自定义的二进制文件,只需要将自定义文件的扩展名改为.bytes,Unity就可以把它识别为TextAsset,进而就可以被打包到AssetBundle中.Unity引擎所能识别的资源我们称为Asset,Asset

Unity 3D中的内存管理

本文欢迎转载,但烦请保留此行出处信息:http://www.onevcat.com/2012/11/memory-in-unity3d/ Unity3D在内存占用上一直被人诟病,特别是对于面向移动设备的游戏开发,动辄内存占用飙上一两百兆,导致内存资源耗尽,从而被系统强退造成极差的体验.类似这种情况并不少见,但是绝大部分都是可以避免的.虽然理论上Unity的内存管理系统应当为开发者分忧解难,让大家投身到更有意义的事情中去,但是对于Unity对内存的管理方式,官方文档中并没有太多的说明,基本需要依靠

【转载】Unity 优雅地管理资源,减少占用内存,优化游戏

转自:星辰的<Unity3D占用内存太大的解决方法> 最近网友通过网站搜索Unity3D在手机及其他平台下占用内存太大. 这里写下关于Unity3D对于内存的管理与优化. Unity3D 里有两种动态加载机制:一个是Resources.Load,另外一个通过AssetBundle,其实两者区别不大. Resources.Load就是从一个缺省打进程序包里的AssetBundle里加载资源,而一般AssetBundle文件需要你自己创建,运行时 动态加载,可以指定路径和来源的. 其实场景里所有静

【转载】Unity 合理安排增量更新(热更新)

原帖地址:由于我看到的那个网站发的这篇帖子很大可能是盗贴的,我就暂时不贴地址了.避免伤害原作者 原版写的有点乱,我个人修改整理了下. ---------------------------------------------------------------------------------------------------- 工具 Unity 中的资源来源有三个途径:一个是Unity自动打包资源,一个是Resources,一个是AssetBundle. Unity自动打包资源是指在Uni

Unity常见面试题大全

1.       [C#语言基础]请简述拆箱和装箱. 答: 装箱操作: 值类型隐式转换为object类型或由此值类型实现的任何接口类型的过程. 1.在堆中开辟内存空间. 2.将值类型的数据复制到堆中. 3.返回堆中新分配对象的地址. 拆箱操作: object类型显示转换为值类型或从接口类型到实现该接口值类型的过程. 1.判断给定类型是否是装箱时的类型. 2.返回已装箱实例中属于原值类型字段的地址. 2.        [.NET(C#)] attribute,property,markup,ta

Unity内存申请和释放

转自:http://www.jianshu.com/p/b37ee8cea04c 1.资源类型 GameObject, Transform, Mesh, Texture, Material, Shader, Script和各种其他Assets. 2.资源创建方式 静态引用,在脚本中加一个public GameObject变量,在Inspector面板中拖一个prefab到该变量上,然后在需要引用的地方Instantiate: Resource.Load,资源需要放在Assets/Resource