UnityEditor编辑器——查找Missing资源

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_34937637/article/details/90722479

功能:

查找出所有所有丢失资源(包括脚本、贴图、材质等)的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();
    }
}

展开阅读全文

查找 missing 错误

10-17

小弟小写一个程序,编译结果如下:rnrnCompiling...rnaddressBook.cpprnE:\C\Project\addressBook\addressBook.cpp(48) : error C2143: syntax error : missing ')' before 'constant'rnE:\C\Project\addressBook\addressBook.cpp(48) : error C2143: syntax error : missing ';' before 'constant'rnE:\C\Project\addressBook\addressBook.cpp(48) : fatal error C1004: unexpected end of file foundrnError executing cl.exe.rnrnaddressBook.obj - 3 error(s), 0 warning(s)rnrn根据指示,我找到改行出错代码,居然是void mycreat(struct student record,int N),搞不懂原因. :rnrn//定义属性rnstruct student rn char name[12]; /* 名字 */rn char sex[2]; /* 性别 */rn char birthday[10]; /* 出生日期 */rn char department[30]; /* 单位 */rn char postcode[7]; /* 邮编 */rn char address[50]; /* 通信地址 */rn char tel[12]; /* 联系电话 */rn char e_mail[20]; /* 联系E-mail */rn *ps;rnrnrn#define N 20rnrnstruct student record[N];rnrn/*---------------函数原型说明------------*/rn rnvoid myprint(); /*-----菜单显示--------------*/rnvoid mycreat(struct student record,int N); /*-----创建记录--------------*/rnvoid mydisplay(struct student record,int N); /*-----显示记录--------------*/rnvoid mysearch(struct student record,int N); /*-----查找记录--------------*/rnvoid mymodify(struct student record,int N); /*-----修改记录--------------*/rnvoid mysort(struct student record,int N); /*-----对通讯录进行排序------*/rnvoid myadd(struct student record,int N); /*-----添加记录--------------*/rnvoid mydelete(struct student record,int N); /*-----删除记录--------------*/ 论坛

没有更多推荐了,返回首页