UnityEditor编辑器——查找Missing资源

功能:

查找出所有所有丢失资源(包括脚本、贴图、材质等)的prefab

效果图:

编辑器

代码如下:

using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.Linq;

public class MyFindMissingWindow : EditorWindow
{
    [MenuItem("window/搜索丢失引用的资源")]
    static void ShowWindow()
    {
        GetWindow<MyFindMissingWindow>("查找Missing资源").Show();
        Find();
    }

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

    //寻找missing的资源
    static void Find()
    {
        string[] paths = AssetDatabase.GetAllAssetPaths();
        //加载所有prefab资源
        var gos = paths.Where(path => path.EndsWith("prefab")).Select(path => AssetDatabase.LoadAssetAtPath<GameObject>(path));

        foreach (var item in gos)
        {
            GameObject go = item as GameObject;
            if (go == null)
            {
                continue;
            }
            Component[] cps = go.GetComponentsInChildren<Component>(true);
            foreach (var cp in cps)
            {
                if (cp == null)  //组件为空
                {
                    if (!prefabs.ContainsKey(go)) prefabs.Add(go, new List<Object>());
                    prefabs[go].Add(cp);
                }
                else   //组件不为空
                {
                    SerializedObject so = new SerializedObject(cp);
                    SerializedProperty iterator = so.GetIterator();
                    //获取所有属性
                    while (iterator.NextVisible(true))
                    {
                        if (iterator.propertyType == SerializedPropertyType.ObjectReference)
                        {
                            //引用对象是null 并且 引用ID不是0 说明丢失了引用
                            if (iterator.objectReferenceValue == null && iterator.objectReferenceInstanceIDValue != 0)
                            {
                                if (!refPaths.ContainsKey(cp)) refPaths.Add(cp, new List<string>());
                                refPaths[cp].Add(iterator.propertyPath);

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

                        }
                    }
                }
            }
        }
    }

    //以下只是将查找结果显示
    private Vector3 scroll = Vector3.zero;
    private void OnGUI()
    {
        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();
    }
}

  • 5
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cchoop

有用的话请杯肥宅水

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值