天天看點

短視訊源碼php,自動查找重複貼圖

短視訊源碼php的素材庫中,存在各種各樣的商品貼圖,為了避免出現重複的情況,可以使用如下的代碼進行查找:

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();
    }
}