资料
GameObjectUtility.GetMonoBehavioursWithMissingScriptCount 获取游戏对象丢失Mono引用的数量
GameObjectUtility.RemoveMonoBehavioursWithMissingScript 移除游戏对象丢失的Mono引用
示例
遍历场景对象,移除丢失的Mono脚本
using UnityEngine;
using UnityEditor;
using UnityEditor.SceneManagement;
public class RemoveMissReferenceScript
{
[MenuItem("Tool/Remove Miss Reference Script")]
private static void Remove()
{
var scene = EditorSceneManager.GetActiveScene();
var rootGameObject = scene.GetRootGameObjects();
foreach (var obj in rootGameObject)
{
RemoveReference(obj);
}
void RemoveReference(GameObject obj)
{
int missCount = GameObjectUtility.GetMonoBehavioursWithMissingScriptCount(obj);
if (missCount > 0)
GameObjectUtility.RemoveMonoBehavioursWithMissingScript(obj);
foreach (Transform item in obj.transform)
RemoveReference(item.gameObject);
}
}
}