批量删除Hierarchy面板中的组件
-
一、目的
当场景中有很多组件需要删掉的时候,一个个删掉很麻烦,需要一键删除。 -
二、使用方法
Hierarchy面板中选择物体,右键,Tools->RemoveComponent,弹出面板设置好后,点击确定即可。 -
三、代码
using System;
using UnityEditor;
using UnityEngine;
/// <summary>
/// 批量删除组件
/// </summary>
public class RemoveComponentEditor : EditorWindow
{
private static EditorWindow window;
private static GameObject select;
private static string className = "Image";//类名
private static string dllName = "UnityEngine.UI";//程序集名
private static string namespaceName = "UnityEngine.UI";//命名空间
[MenuItem("GameObject/Tools/RemoveComponent", priority = 0)]
public static void RemoveComponent()
{
select = Selection.activeGameObject;
window = EditorWindow.GetWindow(typeof(RemoveComponentEditor));
}
private void OnGUI()
{
if (select)
{
GUI.Label(new Rect(20, 40, 200, 20), "程序集名:");
dllName = GUI.TextField(new Rect(110, 40, 100, 20), dllName, 20);
GUI.Label(new Rect(20, 80, 200, 20), "命名空间:");
namespaceName = GUI.TextField(new Rect(110, 80, 100, 20), namespaceName, 20);
GUI.Label(new Rect(20, 120, 200, 20), "组件名:");
className = GUI.TextField(new Rect(110, 120, 100, 20), className, 20);
if (GUI.Button(new Rect(20, 160, 190, 20), "确定"))
{
if (!string.IsNullOrEmpty(className))
{
try
{
Type t = System.Reflection.Assembly.Load(dllName).GetType(namespaceName + "." + className);
GameObject selectGO = select;
var nodes = selectGO.GetComponentsInChildren(t,true);
for (int i = 0; i < nodes.Length; i++)
{
DestroyImmediate(nodes[i]);
}
}
catch (Exception e)
{
Debug.LogError(e.Message);
window.Close();
return;
}
}
window.Close();
}
}
}
}