Untiy3D 查找重复资源

项目中,常常会遇到资源重复的情况,这里介绍一种资源查重的方法,代码如下

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System;
using System.IO;
using System.Security.Cryptography;
public class Script01 {
    [MenuItem("Tools/Report/查找重复资源")]
    static void ReportTexture() {
        Dictionary<string, string> md5dic = new Dictionary<string, string>();
        Dictionary<string, string> md5Prefabs = new Dictionary<string, string>();
        string[] paths = AssetDatabase.FindAssets("t:prefab", new string[] { "Assets/Game02_ClearUseLess" }); // 你想查的目标目录的预制体
        Debug.Log("预制体" + paths.Length);
        foreach (var prefabGuid in paths)
        {
            string prefabAssetPath = AssetDatabase.GUIDToAssetPath(prefabGuid); // 从GUID拿到资源的路径
            // Debug.Log(prefabAssetPath);
            string[] depend = AssetDatabase.GetDependencies(prefabAssetPath, true);
            for (int i = 0; i < depend.Length; i++)
            {
                string assetPath = depend[i];
                // Debug.Log(assetPath);
                AssetImporter importer = AssetImporter.GetAtPath(assetPath);
                // 满足贴图和模型资源
                if (importer is TextureImporter || importer is AudioImporter)
                {
                    // Debug.Log("-------------------------    是    ----------------------------");
                    // Debug.Log(Directory.GetCurrentDirectory());
                    string md5 = GetMD5Hash(Path.Combine(Directory.GetCurrentDirectory(), assetPath)); //获取md5
                    // Debug.Log(md5 + assetPath);
                    string path;
                    md5dic.TryGetValue(md5, out path);
                    if (path == null)
                    {
                        md5dic[md5] = assetPath;
                        md5Prefabs[md5] = prefabAssetPath;
                        // Debug.Log(assetPath);
                    }
                    else
                    {
                        // Debug.Log(path + "   " + assetPath);
                        if (path != assetPath)
                        {
                            Debug.LogFormat("资源重复{0},{1}:预制体位置{2}", path, assetPath, prefabAssetPath);
                        }
                    }
                }
                else {
                    // Debug.LogFormat("都不是{0}:", importer);
                }
            }
            // Debug.Log("-----------------------------------------" + prefabAssetPath + "-------------------------------------------------");
        }
    }

    static string GetMD5Hash(string filePath) {
        MD5 md5 = new MD5CryptoServiceProvider();
        return BitConverter.ToString(md5.ComputeHash(File.ReadAllBytes(filePath))).Replace("-", "").ToLower();
    }

}

这是对应的路径下的对所有prefab的查找

将string[] paths = AssetDatabase.FindAssets("t:prefab", new string[] { "Assets/Game02_ClearUseLess" }); // 你想查的目标目录的预制体

改为string[] paths = AssetDatabase.FindAssets("t:scene", new string[] { "Assets/Game02_ClearUseLess" }); // 你想查的目标目录的预制体

参考资料《Unity3D 游戏开发第二版》

猜你喜欢

转载自www.cnblogs.com/BXLH/p/10986533.html
今日推荐