unity, destroy all children

删除node的所有子节点:

错误方法:

int childCount = node.transform.childCount;

        for (int i=0; i<childCount; i++) {
            GameObject child=node.transform.GetChild(i).gameObject;
            Destroy(child);
        }

以上方法之所以错误,是因为Destroy在下一帧才生效,而在本帧之内各child还存在,例如在上面代码之后输出node.transform.childCount会发现结果不为0。所以如下接下来的逻辑对child是否已经立即删除有依赖(很多时候会有依赖,比如在删除child之后又创建同名的child,并使用findChild获取并修改之,则此时将无法确定到底是修改了已删除的还是修改了新创建的),则会造成莫名奇妙的逻辑错误。

正确方法1:
        List<GameObject> childList = new List<GameObject> ();
        
        int childCount = node.transform.childCount;
        for (int i=0; i<childCount; i++) {
            GameObject child=node.transform.GetChild(i).gameObject;
            childList.Add(child);
        }
        for (int i=0; i<childCount; i++) {
            childList[i].transform.parent=null;
            Destroy(childList[i]);
        }
        (node.transform.childCount == 0).MustBeTrue ();
        
正确方法2:

List<GameObject> childList = new List<GameObject> ();

int childCount = node.transform.childCount;
            for (int i=0; i<childCount; i++) {
                GameObject child=node.transform.GetChild(i).gameObject;
                childList.Add(child);
            }
            for (int i=0; i<childCount; i++) {
                DestroyImmediate(childList[i]);
            }


由于unity官方强烈不推荐使用DestroyImmediate,所以最好是用方法1.

时间: 2024-11-07 18:41:51

unity, destroy all children的相关文章

unity, iterate immediate children and iterate all children

遍历所有直接子节点(immediate children): foreach (Transform child in transform) { // do whatever you want with child transform object here } 注:transform.childCount返回的是直接子节点个数. 遍历所有子节点(包括根节点本身及所有子孙节点): Transform[] allChildren = GetComponentsInChildren<Transform

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

unity, Destroy注意事项

Destroy不是立即发生作用,而是推迟到帧末,所以下面代码是错误的: void OnTriggerEnter(Collider other){   if (other.gameObject.tag == "coin") { m_score++; Destroy(other.gameObject); } } 会导致吃一个金币score加好几次的问题.因为OnTriggerEnter一帧之内可能会触发好几次.正确的写法是: void OnTriggerEnter(Collider oth

点击图片放大功能

<!doctype html> <html> <head> <meta charset="utf-8" /> <title>点击图片放大</title> <style type="text/css"> a img{border:0px;} .latentzoom { CURSOR: pointer; outline: none } .latentzoom-image { BORDER

DOJO DOM 功能

In this tutorial, you'll learn about how to use Dojo to manipulate the DOM in a simple, cross-browser way. Using basic DOM knowledge and only a few Dojo functions, you will be able to efficiently create, read, update and delete elements in the page o

CoinEye PRIVACY POLICY

PRIVACY POLICY First, welcome to use the app Thank you for using our products and services ("Services"). Service provided by the app by using our services, you agree to the terms. Please read carefully. Our range of services is very broad, so so

unity, particle play once and destroy

粒子播放一次后销毁:        //ref: http://answers.unity3d.com/questions/219609/auto-destroying-particle-system.html        //ref: http://answers.unity3d.com/questions/41855/cannot-implicitly-convert-type-unityengineobject-t.html        particle.GetComponent<Pa

unity, collider/trigger on children

参考:http://answers.unity3d.com/questions/410711/trigger-in-child-object-calls-ontriggerenter-in-pa.html

关于Unity中的道具拾取(专题六)

原理就是把道具做成触发器,触发器就是当我们有碰撞发生的时候,只会检测碰撞,而不会有任何改变物理运动状态的过程. 触发器非常适合道具拾取,因为它不会改变原本运动物体的任何物理属性,但是依然会检测碰撞,响应物理事件. 道具拾取实例 1.创建Unity项目和文件目录,保存场景 2.导入金币模型资源rc_fx_obj_04_mod.FBX和obj_04_tex.png,设置材质球的shader为Lagacy Shaders---->Diffuse,颜色设置为255,255,255,255 3.创建一个平