Untiy3D 查找重复资源

Posted bxlh

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 游戏开发第二版》

以上是关于Untiy3D 查找重复资源的主要内容,如果未能解决你的问题,请参考以下文章

Untiy3D按方向键获取值

untiy3D-初学NGUI遇到问题

Untiy3D学习笔记记录

Untiy3D的游戏物体的实例和刚体的使用

untiy3d的MonoBehaviour的默认函数调用顺序

Untiy3D性能优化实战记录