Last active
March 1, 2020 12:08
-
-
Save TORISOUP/88b27fd792529ec0662f9c701a59ab36 to your computer and use it in GitHub Desktop.
Unity プロジェクトビューで選択中のGameObject以下の自前のComponentを全部消すやつ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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