美文网首页
unity 在Editor下删除场景中的物体

unity 在Editor下删除场景中的物体

作者: 爱喝粥的西瓜 | 来源:发表于2021-11-03 16:41 被阅读0次

unity在Editor模式下通过代码删除场景中的物体没有办法使用Destory来删除,需要使用DestroyImmediate

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class DeleteSelectObjectChild : Editor
{
    [MenuItem("XGL/DeleteChild", false, 10)]
    public static void DeleteSelectChild()
   {
       Transform[] selected = Selection.transforms;
        foreach(Transform t in selected)
        {
            GameObject[] go = new GameObject[t.childCount];
            for(int i = 0; i < t.childCount;++i)
            {
                go[i] = t.GetChild(i).gameObject;
            }
            for(int i = 0;i < go.Length;++i)
            {
                DestroyImmediate(go[i]);
            }
            t.gameObject.SetActive(true);
        }
   }

}

相关文章

网友评论

      本文标题:unity 在Editor下删除场景中的物体

      本文链接:https://www.haomeiwen.com/subject/gslczltx.html