Unity

[Unity] 프리팹, 씬 내의 Null, Missing script 한번에 제거하기

Guk-blog 2020. 12. 9. 16:34
728x90
반응형
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public static class ClearNullScript
{
#if UNITY_EDITOR
    [MenuItem("CleanUp/Remove Missing Scripts Recursively Visit Prefabs")]
    private static void FindAndRemoveMissingInSelected()
    {
        var deepSelection = EditorUtility.CollectDeepHierarchy(Selection.gameObjects);
        int compCount = 0;
        int goCount = 0;
        foreach (var o in deepSelection)
        {
            if (o is GameObject go)
            {
                int count = GameObjectUtility.GetMonoBehavioursWithMissingScriptCount(go);
                if (count > 0)
                {
                    // Edit: use undo record object, since undo destroy wont work with missing
                    Undo.RegisterCompleteObjectUndo(go, "Remove missing scripts");
                    GameObjectUtility.RemoveMonoBehavioursWithMissingScript(go);
                    compCount += count;
                    goCount++;
                }
            }
        }
    }
#endif
}

스크립트 생성 후 위의 코드를 붙여준 후에 원하는 씬 혹은 프리팹을 열어주고 타겟들을 모두 선택해주세요

그리고 unity menu창(프로그램 상단)을 보시면 CleanUp이 있습니다
실행해 주면 끝 일일히 확인해가며 안지워도 오케이..?

728x90
반응형