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

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下删除场景中的物体)