본문 바로가기
개발/Unity

Unity) Resources 모든 오브젝트 Missing(null) 확인

by 테샤르 2022. 2. 23.

현재 Scene의 모든 오브젝트 Missing(null) 확인

현재 Scene 기준으로 모든 Component를 찾아서 null인지 판단하는 코드이다.

 

반응형

작업을 하다보면 중간에 삭제가되서 연결이 자동으로 끊어지는 Missing 난 상태가 종종 생긴다.

그런경우에 Missing난 걸 일일이 찾는데는 오래걸리기 때문에 에디터툴을 하나 만들어 두고 사용하는게 좋아보인다.

 

using UnityEngine;
using UnityEditor;
public class FindMissingScripts : EditorWindow
{
    [MenuItem("Window/FindMissingScripts")]
    public static void ShowWindow()
    {
        EditorWindow.GetWindow(typeof(FindMissingScripts));
    }
 
    public void OnGUI()
    {
        if (GUILayout.Button("Find Missing Resources"))
        {
            FindInCurrentScene();
        }
    }

    private static void FindInCurrentScene()
    {
        List<GameObject> objectsInScene = new List<GameObject>();
        foreach (GameObject go in Resources.FindObjectsOfTypeAll(typeof(GameObject)) as GameObject[])
        {
            if (go.hideFlags != HideFlags.None)
                continue;

            objectsInScene.Add(go);
        }

        foreach (GameObject g in objectsInScene)
        {
            var result =IsNullComponents(g);
            Debug.Log($"Searched {g} GameObjects, {result.Item1} Mission :{result.Item2}");
        }
    }

    private static (int,int) IsNullComponents(GameObject _root)
    {
        int go_count = 0;
        int missing_count = 0;

        Component[] components = _root.GetComponents<Component>();
        go_count = components.Length;
        for (int i = 0; i < components.Length; i++)
        {
            if (components[i] == null)
            {
                missing_count++;
                string s = _root.name;
                Transform t = _root.transform;
                while (t.parent != null)
                {
                    s = t.parent.name + "/" + s;
                    t = t.parent;
                }
                Debug.Log($"{s}  has an empty script attached in position: {i} : {_root}");
            }
        }

        return (go_count, missing_count);
    }

 

Resource.FindObjectsOfTypeAll : [링크]

 

Unity - Scripting API: Resources.FindObjectsOfTypeAll

This function can return any type of Unity object that is loaded, including game objects, prefabs, materials, meshes, textures, etc. It will also list internal objects, therefore be careful with the way you handle the returned objects. Contrary to Object.F

docs.unity3d.com

 

 

★☆☆☆☆

 

반응형

댓글