zoukankan      html  css  js  c++  java
  • Unity查询文件是否存在重复引用

    核心:用MD5.ComputeHash来判断是否存在重复文件

    用AssetImporter (资源导入器)来筛选文件类型

    using System.Collections;
    using UnityEngine;
    using UnityEditor;
    using System.Security.Cryptography;
    using System;
    using System.IO;
    using System.Collections.Generic;
    
    public class CheckRepeatElement  {
    
    
    	[MenuItem("Tools/查找重复贴图")]
    	static void ReportTexture()
    	{
    		Dictionary<string,string> md5dic = new Dictionary<string, string> ();
            //在Assets/Resources目录下发现所有后缀为perfabs文件的GUID
            string[] paths = AssetDatabase.FindAssets("t:prefab",new string[]{"Assets/Resources"});
    
    		foreach (var prefabGuid in paths) {
    			string prefabAssetPath = AssetDatabase.GUIDToAssetPath(prefabGuid);
                //递归的查找依赖
    			string[] depend = AssetDatabase.GetDependencies (prefabAssetPath,true);
    			for (int i = 0; i < depend.Length; i++) {
    				string assetPath = depend [i];
    				AssetImporter importer = AssetImporter.GetAtPath(assetPath);
    				//满足贴图和模型资源
    				if (importer is TextureImporter || importer is ModelImporter) {
                        //将文加路经和资源路径连接
    					string md5 = GetMD5Hash(Path.Combine(Directory.GetCurrentDirectory(),assetPath));
    					string path;
    				
    					if (!md5dic.TryGetValue (md5, out path)) {
    						md5dic [md5] = assetPath;
    					}else {
    						if (path != assetPath) {
    							Debug.LogFormat ("{0} {1} 资源发生重复!", path, assetPath);
    						}
    					}
    				}
    			}
    		}
    	}
    	static string GetMD5Hash(string filePath)
    	{
    		MD5 md5 = new MD5CryptoServiceProvider();
    		return BitConverter.ToString(md5.ComputeHash(File.ReadAllBytes(filePath))).Replace("-", "").ToLower();
    	}
    }
    

      

  • 相关阅读:
    Python常用转换函数
    Python随机数
    sublime text的pylinter插件设置pylint_rc后提示错误
    使用Pydoc生成文档
    字符编码笔记:ASCII,Unicode和UTF-8
    Windows编程MessageBox函数
    魔方阵算法及C语言实现
    iOS通讯录整合,兼容iOS789写法,附demo
    谈谈iOS app的线上性能监测
    ReactiveCocoa代码实践之-更多思考
  • 原文地址:https://www.cnblogs.com/chenggg/p/11616585.html
Copyright © 2011-2022 走看看