49 lines
1.4 KiB
C#
49 lines
1.4 KiB
C#
using UnityEngine;
|
|
using UnityEditor;
|
|
using System.Collections.Generic;
|
|
|
|
public class RemoveMissingScripts
|
|
{
|
|
[MenuItem("Tools/Remove Hierarchy Missing Scripts")]
|
|
private static void RemoveMissingScriptsFromSelection()
|
|
{
|
|
// 获取选中的对象
|
|
GameObject[] selectedObjects = Selection.gameObjects;
|
|
|
|
if (selectedObjects.Length == 0)
|
|
{
|
|
Debug.LogWarning("No objects selected in Hierarchy.");
|
|
return;
|
|
}
|
|
|
|
int totalRemoved = 0;
|
|
|
|
// 遍历所有选中的对象
|
|
foreach (GameObject go in selectedObjects)
|
|
{
|
|
// 包括所有子对象
|
|
int removed = RemoveMissingScriptsRecursively(go);
|
|
totalRemoved += removed;
|
|
|
|
Debug.Log($"Removed {removed} missing scripts from {go.name} and its children.");
|
|
}
|
|
|
|
Debug.Log($"Total removed missing scripts: {totalRemoved}");
|
|
}
|
|
|
|
private static int RemoveMissingScriptsRecursively(GameObject gameObject)
|
|
{
|
|
int removedCount = 0;
|
|
|
|
// 处理当前对象
|
|
removedCount += GameObjectUtility.RemoveMonoBehavioursWithMissingScript(gameObject);
|
|
|
|
// 递归处理所有子对象
|
|
foreach (Transform child in gameObject.transform)
|
|
{
|
|
removedCount += RemoveMissingScriptsRecursively(child.gameObject);
|
|
}
|
|
|
|
return removedCount;
|
|
}
|
|
} |