Skip to content

Instantly share code, notes, and snippets.

@TORISOUP
Last active March 1, 2020 12:08
Show Gist options
  • Save TORISOUP/88b27fd792529ec0662f9c701a59ab36 to your computer and use it in GitHub Desktop.
Save TORISOUP/88b27fd792529ec0662f9c701a59ab36 to your computer and use it in GitHub Desktop.
Unity プロジェクトビューで選択中のGameObject以下の自前のComponentを全部消すやつ
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
namespace ComponentRemover.Editor
{
public class ComponentRemover : EditorWindow
{
static ComponentRemover window;
[MenuItem("Window/ComponentRemover")]
static void Open()
{
GetWindow<ComponentRemover>("ComponentRemover");
}
private void OnGUI()
{
if (GUILayout.Button("Remove all components"))
{
CheckGameObjects();
}
}
private void CheckGameObjects()
{
var targets = Selection
.gameObjects
.SelectMany(x => GetChildren(x.transform))
.Distinct();
var sum = 0;
foreach (var t in targets)
{
sum += DestroyComponents(t.gameObject);
}
Debug.Log("Removed:" + sum);
}
private int DestroyComponents(GameObject go)
{
// Debug.Log(go);
var n = 0;
var co = go.GetComponents<MonoBehaviour>();
foreach (var monoBehaviour in co.Where(x =>
{
var ns = x.GetType().Namespace;
return ns != null && !ns.StartsWith("UnityEngine");
}))
{
DestroyImmediate(monoBehaviour);
n++;
}
return n;
}
private static IEnumerable<Transform> GetChildren(Transform t)
{
var i = 0;
yield return t;
while (i < t.childCount)
{
yield return t.GetChild(i);
++i;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment