少年修仙传客户端基础资源
dabaoji
2025-06-09 8ee0256378cbf5dbc9d76ed10b60b65a844ef4dd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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);
    }
 
}