| using System.Collections; | 
| using System.Collections.Generic; | 
| using UnityEngine; | 
| using UnityEditor; | 
| using System.IO; | 
| using System; | 
| using System.Text; | 
|   | 
| public class IconNameReplaceTool | 
| { | 
|   | 
|     static string referencePath = Application.dataPath + "/切片资源"; | 
|     static string rootPath = Application.dataPath + "/ResourcesOut/UI/Sprite"; | 
|     static string spriteRelativePath = "Assets/ResourcesOut/UI/Sprite/"; | 
|   | 
|     static string textPath = Application.dataPath + "/ResourcesOut/Refdata/Config/Icon.txt"; | 
|     static Dictionary<string, Dictionary<string, string>> iconNameDictionary = new Dictionary<string, Dictionary<string, string>>(); | 
|   | 
|     static Dictionary<long, string> referenceResourceSizeAndNames = new Dictionary<long, string>(); | 
|   | 
|     static string[] headLines = new string[3]; | 
|   | 
|     [MenuItem("程序/Sprite/Sprite名称过渡")] | 
|     public static void IconNameNormalize() | 
|     { | 
|         try | 
|         { | 
|             ParseConfig(); | 
|             ParseReferenceResources(); | 
|             RenameSprites(); | 
|             WriteIconConfig(); | 
|         } | 
|         catch (Exception ex) | 
|         { | 
|             Debug.Log("资源替换异常:" + ex); | 
|         } | 
|         finally | 
|         { | 
|             EditorUtility.ClearProgressBar(); | 
|         } | 
|     } | 
|   | 
|     static void ParseConfig() | 
|     { | 
|         var lines = File.ReadAllLines(textPath); | 
|   | 
|         for (int i = 0; i < 3; i++) | 
|         { | 
|             headLines[i] = lines[i]; | 
|         } | 
|   | 
|         for (int i = 3; i < lines.Length; i++) | 
|         { | 
|             var line = lines[i]; | 
|             var valueStrings = line.Split('\t'); | 
|   | 
|             for (int j = 0; j < valueStrings.Length; j++) | 
|             { | 
|                 var folder = valueStrings[1]; | 
|                 var key = valueStrings[0]; | 
|                 var value = valueStrings[2]; | 
|   | 
|                 Dictionary<string, string> keyValues = null; | 
|                 if (!iconNameDictionary.ContainsKey(folder)) | 
|                 { | 
|                     iconNameDictionary[folder] = keyValues = new Dictionary<string, string>(); | 
|                 } | 
|                 else | 
|                 { | 
|                     keyValues = iconNameDictionary[folder]; | 
|                 } | 
|   | 
|                 keyValues[key] = value; | 
|             } | 
|         } | 
|     } | 
|   | 
|   | 
|     static void ParseReferenceResources() | 
|     { | 
|         var allFiles = new DirectoryInfo(referencePath).GetFiles("*.png", SearchOption.AllDirectories); | 
|         for (int i = 0; i < allFiles.Length; i++) | 
|         { | 
|             var fileInfo = allFiles[i]; | 
|             var size = fileInfo.Length; | 
|             var name = fileInfo.Name; | 
|             referenceResourceSizeAndNames[size] = name; | 
|         } | 
|     } | 
|   | 
|     static void RenameSprites() | 
|     { | 
|         var allFiles = new DirectoryInfo(rootPath).GetFiles("*.png", SearchOption.AllDirectories); | 
|   | 
|         var total = allFiles.Length; | 
|         var index = 0; | 
|   | 
|         foreach (var file in allFiles) | 
|         { | 
|             var pathStringArray = file.DirectoryName.Split('\\'); | 
|             var folderName = pathStringArray[pathStringArray.Length - 1]; | 
|             var size = file.Length; | 
|   | 
|             if (referenceResourceSizeAndNames.ContainsKey(size)) | 
|             { | 
|                 var oldName = file.Name.Replace(".png", ""); | 
|                 var newName = referenceResourceSizeAndNames[size].Replace(".png", ""); | 
|                 RenameAsset(StringUtility.Contact(spriteRelativePath, folderName, "/", file.Name), newName); | 
|                 ReplaceIconConfig(folderName, oldName, newName); | 
|             } | 
|   | 
|             index++; | 
|   | 
|             EditorUtility.DisplayProgressBar("替换图片名", "替换中.......", index / (float)total); | 
|         } | 
|   | 
|         EditorUtility.ClearProgressBar(); | 
|     } | 
|   | 
|     static void WriteIconConfig() | 
|     { | 
|         var lines = new List<string>(); | 
|         lines.AddRange(headLines); | 
|   | 
|         foreach (var key in iconNameDictionary.Keys) | 
|         { | 
|             var keyValues = iconNameDictionary[key]; | 
|             foreach (var keyValue in keyValues) | 
|             { | 
|                 lines.Add(StringUtility.Contact(keyValue.Key, "\t", key, "\t", keyValue.Value)); | 
|             } | 
|         } | 
|   | 
|         File.WriteAllLines(textPath, lines.ToArray(), Encoding.UTF8); | 
|     } | 
|   | 
|     private static void ReplaceIconConfig(string _folder, string _fileName, string _newFileName) | 
|     { | 
|         if (iconNameDictionary.ContainsKey(_folder)) | 
|         { | 
|             var keyValues = iconNameDictionary[_folder]; | 
|   | 
|             foreach (var key in keyValues.Keys) | 
|             { | 
|                 if (keyValues[key] == _fileName) | 
|                 { | 
|                     keyValues[key] = _newFileName; | 
|                     break; | 
|                 } | 
|             } | 
|         } | 
|   | 
|     } | 
|   | 
|     static void RenameAsset(string _path, string _newName) | 
|     { | 
|         AssetDatabase.RenameAsset(_path, _newName); | 
|     } | 
|   | 
| } |