Unity搜索丢失引用的资源

using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEditor;
using System.Linq;
using System.Reflection;
using UnityEditor.SceneManagement;

public class FindMissingWindow : EditorWindow
{
    static Dictionary<Object, List<Object>> prefabs = new Dictionary<Object, List<Object>>();

    static Dictionary<Object, List<string>> refPaths = new Dictionary<Object, List<string>>();

    private Vector3 scroll = Vector3.zero;

    [MenuItem("Tools/搜索丢失引用的资源")]
    static void ShowWindow()
    {
        GetWindow<FindMissingWindow>("查找Missing资源").Show();
    }

    public static void FindMissingReferencesInAssets()
    {
        var gos = new List<GameObject>();
        prefabs = new Dictionary<Object, List<Object>>();
        refPaths = new Dictionary<Object, List<string>>();
        string selectPath = GetCurrentAssetDirectory();
        if (string.IsNullOrEmpty(selectPath))
        {
            EditorUtility.DisplayDialog("错误提示", "请选择一个文件夹。", "确定");
        }
        else
        {
            string[] files = Directory.GetFiles(selectPath, "*.prefab", SearchOption.AllDirectories);
            for (int i = 0; i < files.Length; i++)
            {
                string path = files[i];
                if (i < files.Length)
                {
                    EditorUtility.DisplayProgressBar("查找进度" + i + "/" + files.Length, "查找Prefab:" + path, (float)(i / files.Length));
                }

                gos.Add(AssetDatabase.LoadAssetAtPath<GameObject>(path));
            }
            EditorUtility.ClearProgressBar();
            FindMissingReferences(gos);
        }
    }

    public static string GetCurrentAssetDirectory()
    {
        foreach (var obj in Selection.GetFiltered<Object>(SelectionMode.Assets))
        {
            var path = AssetDatabase.GetAssetPath(obj);

            if (string.IsNullOrEmpty(path))
                continue;
            if (System.IO.Directory.Exists(path))
            {
                return path;
            }
            else if (System.IO.File.Exists(path))
            {
                return System.IO.Path.GetDirectoryName(path);
            }
        }

        return "";
    }

    private static void FindMissingReferences(List<GameObject> gameObjects)
    {
        if (gameObjects == null)
        {
            return;
        }

        int progress = 0;
        foreach (var item in gameObjects)
        {
            GameObject go = item as GameObject;
            if (go == null)
            {
                continue;
            }

            if (progress < gameObjects.Count)
            {
                EditorUtility.DisplayProgressBar("进度" + progress + "/" + gameObjects.Count, "检查资源:" + go.name, (float)(progress / gameObjects.Count));
            }

            var components = go.GetComponentsInChildren<Component>(true);

            foreach (var component in components)
            {
                if (component == null)
                {
                    if (!prefabs.ContainsKey(go)) prefabs.Add(go, new List<Object>());
                    prefabs[go].Add(component);
                    continue;
                }

                SerializedObject so = new SerializedObject(component);
                var sp = so.GetIterator();

                var objRefValueMethod = typeof(SerializedProperty).GetProperty("objectReferenceStringValue",
                    BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);

                // Iterate over the components' properties.
                while (sp.NextVisible(true))
                {
                    if (sp.propertyType == SerializedPropertyType.ObjectReference)
                    {
                        string objectReferenceStringValue = string.Empty;

                        if (objRefValueMethod != null)
                        {
                            objectReferenceStringValue = (string)objRefValueMethod.GetGetMethod(true).Invoke(sp, new object[] { });
                        }

                        if (sp.objectReferenceValue == null
                            && (sp.objectReferenceInstanceIDValue != 0
                                || objectReferenceStringValue.StartsWith("Missing")))
                        {
                            if (!refPaths.ContainsKey(component)) refPaths.Add(component, new List<string>());
                            refPaths[component].Add(sp.propertyPath);

                            if (!prefabs.ContainsKey(go)) prefabs.Add(go, new List<Object>());
                            prefabs[go].Add(component);
                        }
                    }
                }
            }

            progress++;
        }

        EditorUtility.ClearProgressBar();
    }

    void OnInspectorUpdate() //更新  
    {
        Repaint(); //重新绘制  
    }

    private void OnGUI()
    {
        if (GUILayout.Button("选择指定目录,寻找missing的资源", GUILayout.Width(250), GUILayout.Height(40)))
        {
            FindMissingReferencesInAssets();
        }

        scroll = EditorGUILayout.BeginScrollView(scroll);
        EditorGUILayout.BeginVertical();
        foreach (var item in prefabs)
        {
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.ObjectField(item.Key, typeof(GameObject), true, GUILayout.Width(200));
            EditorGUILayout.BeginVertical();
            foreach (var cp in item.Value)
            {
                EditorGUILayout.BeginHorizontal();
                if (cp)
                {
                    EditorGUILayout.ObjectField(cp, cp.GetType(), true, GUILayout.Width(200));
                    if (refPaths.ContainsKey(cp))
                    {
                        string missingPath = null;
                        foreach (var path in refPaths[cp])
                        {
                            missingPath += path + "|";
                        }

                        if (missingPath != null)
                            missingPath = missingPath.Substring(0, missingPath.Length - 1);
                        GUILayout.Label(missingPath);
                    }
                }
                else
                {
                    GUILayout.Label("丢失脚本!");
                }

                EditorGUILayout.EndHorizontal();
            }

            EditorGUILayout.EndVertical();
            EditorGUILayout.EndHorizontal();
        }

        EditorGUILayout.EndVertical();
        EditorGUILayout.EndScrollView();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值