Unity query whether the file exists duplicate references

Core: with MD5.ComputeHash to determine whether there are duplicate files

With AssetImporter (resource importer) to filter file types

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 / Find duplicates map")]
	static void ReportTexture()
	{
		Dictionary<string,string> md5dic = new Dictionary<string, string> ();
        // find all suffixes in Assets / Resources directory for the file GUID perfabs
        string[] paths = AssetDatabase.FindAssets("t:prefab",new string[]{"Assets/Resources"});

		foreach (var prefabGuid in paths) {
			string prefabAssetPath = AssetDatabase.GUIDToAssetPath(prefabGuid);
            // recursive lookup dependence
			string[] depend = AssetDatabase.GetDependencies (prefabAssetPath,true);
			for (int i = 0; i < depend.Length; i++) {
				string assetPath = depend [i];
				AssetImporter importer = AssetImporter.GetAtPath(assetPath);
				// maps and models to meet the resource
				if (importer is TextureImporter || importer is ModelImporter) {
                    // add the text path and resource path connection
					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} duplication of resources!", Path, assetPath);
						}
					}
				}
			}
		}
	}
	static string GetMD5Hash(string filePath)
	{
		MD5 md5 = new MD5CryptoServiceProvider();
		return BitConverter.ToString(md5.ComputeHash(File.ReadAllBytes(filePath))).Replace("-", "").ToLower();
	}
}

  

Guess you like

Origin www.cnblogs.com/chenggg/p/11616585.html