微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Unity用脚本自动查找重复贴图

查找Assets/Resources目录中,重复的贴图

using System.Collections;
using UnityEngine;
using UnityEditor;
using System.Security.Cryptography;
using System;
using System.IO;
using System.Collections.Generic;

public class FindRepetRes  {

	[MenuItem("Tools/Report/查找重复贴图")]
	static void ReportTexture()
	{
		Dictionary<string,string> md5dic = new Dictionary<string, string> ();
		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);
						}
					}
				}
			}
		}
	}
	
	/// <summary>
	/// 获取文件Md5
	/// </summary>
	/// <returns>The M d5 hash.</returns>
	/// <param name="filePath">File path.</param>
	static string GetMD5Hash(string filePath)
	{
		MD5 md5 = new MD5CryptoServiceProvider();
		return BitConverter.ToString(md5.ComputeHash(File.ReadAllBytes(filePath))).Replace("-", "").ToLower();
	}
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐