Unity 查找没有使用的资源

几轮迭代之后项目存在无效资源,借助小工具查找无用资源:

1.获得project所有文件 allRes

2.获取每个文件的依赖文件,组合一个依赖文件列表 dependenciesFileList

3.遍历allRes

        file 是否在dependenciesFileList

                在 跳过

        不在 记录 找到了无效文件

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

namespace CZGame
{

namespace feicun.Assets.UnityLib.Editor
{
    public class ShowAllNotUseResourceDeal
    {
        
        [MenuItem("AssetTools/资源检查")]
        public static void ShowAllNotUseResource()
        {
            EditorWindow.GetWindow(typeof(ShowAllNotUseResource),false,"资源检查"); //>(false,"",true);
        }

    }
}
    public class ShowAllNotUseResource : EditorWindow
    {


        private static List<string> checkType = new List<string>();
        private static List<string> allResource = new List<string>();  //所有使用资源
        private static List<string> notUseReource = new List<string>();  //没有使用过的资源

        static Dictionary<string, List<string>> mapReferenceOtherModulePref = new Dictionary<string, List<string>>();
        Vector2 scrollPos = Vector2.zero;
        private static void StartCheck()
        {
            List<string> withoutExtensions = new List<string>() { ".prefab",".unity" };
            string[] files = Directory.GetFiles(Application.dataPath + "", "*.*", SearchOption.AllDirectories)
                .Where(s => withoutExtensions.Contains(Path.GetExtension(s).ToLower())).ToArray();

            List<string> withoutExtensions2 = new List<string>() {".prefab"};
            //所有project 里面的资源
            string[] UIReource = Directory.GetFiles(Application.dataPath + "", "*.*", SearchOption.AllDirectories)
                 .Where(s => withoutExtensions2.Contains(Path.GetExtension(s).ToLower())).ToArray();

            for (int i = 0; i < UIReource.Length; i++)
            {
                UIReource[i] = UIReource[i].Replace("\\", "/");
                int index = UIReource[i].IndexOf("Assets");
                UIReource[i] = UIReource[i].Substring(index);
            }

            mapReferenceOtherModulePref.Clear();
            float count = 0;
            foreach (string file in files)
            {
                // Debug.Log("files :" + file);
                count++;
                EditorUtility.DisplayProgressBar("Processing...", "检索中....", count / files.Length);

                string strFile = file.Substring(file.IndexOf("Asset")).Replace('\\', '/');
                string[] dependenciesFiles = AssetDatabase.GetDependencies(strFile);

                foreach (string depFile in dependenciesFiles)
                {
                    bool isNeedShow = false;
                    foreach (string type in checkType)
                    {
                        //存在设置类型  需要显示
                        if (depFile.IndexOf(type) > -1)
                        {
                            isNeedShow = true;
                            break;
                        }
                    }
                    if (isNeedShow == false)
                    {
                        continue;
                    }
                    allResource.Add(depFile);

                }
            }

            EditorUtility.ClearProgressBar();


            for (int i = 0; i < UIReource.Length; i++)
            {
                bool isUse = false;
                foreach (string usePath in allResource)
                {
                    if (UIReource[i] == usePath)
                    {
                        isUse = true;
                        break;
                    }
                }

                if (isUse == false)
                {
                    notUseReource.Add(UIReource[i]);
                }

            }
        }

        void OnGUI()
        {

            EditorGUILayout.BeginHorizontal();
            if (GUILayout.Button("列出所有未使用过的资源", GUILayout.Width(200)))
            {
                allResource = new List<string>();
                notUseReource = new List<string>();
                checkType = new List<string>() { ".prefab"};
                // checkType.Add(".jpg");
                // checkType.Add(".png");
                StartCheck();
            }

            if (GUILayout.Button("删除所有 !", GUILayout.Width(200)))
            {

                float count = 0;
                foreach (var path in notUseReource)
                {
                    count++;
                    EditorUtility.DisplayProgressBar("Processing...", "删除中... ("+ count + "/ " + notUseReource.Count + ")", count / notUseReource.Count);
                    File.Delete(path);
                    AssetDatabase.Refresh();
                }

                EditorUtility.ClearProgressBar();
            }
            EditorGUILayout.EndHorizontal();

            scrollPos = EditorGUILayout.BeginScrollView(scrollPos);
            foreach (var path in notUseReource)
            {

                if(path.IndexOf(".prefab") > -1) {
                    Debug.Log("Unuse prefab : "+ path);
                    // EditorGUILayout.BeginHorizontal();
                    // Material t = (Material)AssetDatabase.LoadAssetAtPath(path, typeof(Material));
                    // EditorGUILayout.ObjectField(t, typeof(Material), false);

                    // if (GUILayout.Button("删除", GUILayout.Width(200)))
                    // {
                    //     File.Delete(path);
                    //     AssetDatabase.Refresh();
                    // }

                    // EditorGUILayout.Space();

                    // EditorGUILayout.EndHorizontal();
                }

                // if(path.IndexOf(".mat") > -1) {
                //     EditorGUILayout.BeginHorizontal();
                //     Material t = (Material)AssetDatabase.LoadAssetAtPath(path, typeof(Material));
                //     EditorGUILayout.ObjectField(t, typeof(Material), false);

                //     if (GUILayout.Button("删除", GUILayout.Width(200)))
                //     {
                //         File.Delete(path);
                //         AssetDatabase.Refresh();
                //     }

                //     EditorGUILayout.Space();

                //     EditorGUILayout.EndHorizontal();
                // }

                // if(path.IndexOf(".png") > -1||path.IndexOf(".jpg") > -1||path.IndexOf(".tga") > -1) {
                //     EditorGUILayout.BeginHorizontal();
                //     Texture2D t = (Texture2D)AssetDatabase.LoadAssetAtPath(path, typeof(Texture2D));
                //     EditorGUILayout.ObjectField(t, typeof(Texture2D), false);

                //     if (GUILayout.Button("删除", GUILayout.Width(200)))
                //     {
                //         File.Delete(path);
                //         AssetDatabase.Refresh();
                //     }

                //     EditorGUILayout.Space();

                //     EditorGUILayout.EndHorizontal();
                // }



            }
            EditorGUILayout.EndScrollView();



        }
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值