using System.Collections;
|
using System.Collections.Generic;
|
using UnityEngine;
|
using UnityEditor;
|
using System.IO;
|
using System;
|
using System.Text;
|
|
public class IconNameNormalizeTool
|
{
|
static string rootPath = Application.dataPath + "/ResourcesOut/UI/Sprite/FabaoIcon";
|
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 string[] headLines = new string[3];
|
|
public static void IconNameNormalize()
|
{
|
try
|
{
|
ParseConfig();
|
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 RenameSprites()
|
{
|
var allFiles = new DirectoryInfo(rootPath).GetFiles("*.png", SearchOption.AllDirectories);
|
|
var total = allFiles.Length;
|
var index = 0;
|
|
foreach (var file in allFiles)
|
{
|
var fileInfo = new FileInfo(file.FullName);
|
var pathStringArray = fileInfo.DirectoryName.Split('\\');
|
var folderName = pathStringArray[pathStringArray.Length - 1];
|
|
var prefix = StringUtility.Contact(folderName, "_");
|
if (!file.Name.StartsWith(prefix))
|
{
|
var fileNameWithOutExtersion = file.Name.Replace(".png", "");
|
var replaceName = StringUtility.Contact(prefix, fileNameWithOutExtersion);
|
RenameAsset(StringUtility.Contact(spriteRelativePath, folderName, "/", file.Name), replaceName);
|
ReplaceIconConfig(folderName, fileNameWithOutExtersion, replaceName);
|
}
|
|
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);
|
}
|
|
|
}
|