在第一节中我们演示了资源规范工具的写法。在上一节中我们遇到了强制Sprite图片的命名规范的情况(不按照规范命名的Sprite加载不出来),这一节就来实现一个检测命名规范的工具。

TextureSetting

修改TextureSetting,增加检测指定文件夹路径内文件名的逻辑

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
using UnityEngine;
using UnityEditor;
using System.Text.RegularExpressions;


public class TextureImportSetting : AssetPostprocessor
{
private static FolderData _playerData;//Editor环境下的变量必须是静态的
private void OnPreprocessTexture()
{
NamingConvention();
SetTexturePara();
}

private void NamingConvention()
{
PlayerFolderNaming();
}
private void PlayerFolderNaming()
{
if (assetPath.Contains(Path.PLAYER_PICTURE_FOLDER))
{
string name = System.IO.Path.GetFileNameWithoutExtension(assetPath);
string pattern = @"^\d+_\d+$";
if (!Regex.IsMatch(name, pattern))
{
if (_playerData == null)
{
_playerData = new FolderData()
{
FolderPath = Path.PLAYER_PICTURE_FOLDER,
NameTip = "0_0"
};
}

Debug.LogError("当前导入资源命名不规范:" + name);
NameMgrWindowData.Add(_playerData, assetPath);
NameMgrWindow.ShowWnd();
}

}
}
private void SetTexturePara()
{
TextureImporter importer = assetImporter as TextureImporter;
importer.textureType = TextureImporterType.Sprite;
importer.mipmapEnabled = false;
}
}

@"^\d+_\d+$"表示必须是“数字_数字”的格式,例如“2_7”、“12_18”。“^”和“$”代表定位开头和结尾,“\d”表示09,“\d+”表示09这些数字至少出现一次。“_”表示中间的字符必须是“_”。

NameMgrWindow

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
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class NameMgrWindow : EditorWindow
{
private Dictionary<string,string> _namesDic = new Dictionary<string,string>();//用来缓存新命名的字典,key是旧名,value是新名
private Vector2 scrollPosition;
[MenuItem("Custom/NameMgrWindow")]
public static void ShowWnd()
{
GetWindow<NameMgrWindow>();
}

private void OnGUI()
{
GUILayout.Label("资源名称管理器", new GUIStyle(GUI.skin.box) { fontSize = 20 });
NameMgrWindowData.RemoveChanged();

scrollPosition = GUILayout.BeginScrollView(scrollPosition);
foreach (var pair in NameMgrWindowData.SpriteDic)
{
GUILayout.BeginHorizontal();
GUILayout.Label("路径:",GUILayout.MaxWidth(50));
GUILayout.Label(pair.Key.FolderPath,GUILayout.MaxWidth(150));
GUILayout.Label("范例:",GUILayout.MaxWidth(50));
GUILayout.Label(pair.Key.NameTip,GUILayout.MaxWidth(150));
GUILayout.EndHorizontal();

GUILayout.BeginHorizontal();
foreach (var path in pair.Value)
{
GUILayout.BeginVertical();

Texture2D texture2D = AssetDatabase.LoadAssetAtPath<Texture2D>(path);
GUILayout.Box(texture2D,GUILayout.Width(80),GUILayout.Height(80));
string name = System.IO.Path.GetFileNameWithoutExtension(path);

//缓存旧名称
if(!_namesDic.ContainsKey(name))
_namesDic.Add(name, name);

GUILayout.BeginHorizontal();

_namesDic[name] = GUILayout.TextField(_namesDic[name], GUILayout.Width(40));
if (GUILayout.Button("确认", GUILayout.Width(30)))//OnGUI是每帧都更新的,所以加个Button
{
if (_namesDic[name] != name)
{
ChangeFileName(name, _namesDic[name], path);
_namesDic.Remove(name);//名称更新后,删除旧名称
}
AssetDatabase.Refresh();
}

GUILayout.EndHorizontal();



GUILayout.EndVertical();
}
GUILayout.EndHorizontal();
}
GUILayout.EndScrollView();
}
private void ChangeFileName(string oldName,string newName,string path)
{
string newPath = path.Replace(oldName, newName);
if(System .IO.File.Exists(newPath))
{
EditorUtility.DisplayDialog("重命名出错", "当前文件夹中已经存在同名文件","确定","关闭");
}
else
{
System.IO.File.Move(path, newPath);
System.IO.File.Delete(path + ".meta");
}
}
}
public class NameMgrWindowData
{
public static Dictionary<FolderData, List<string>> SpriteDic = new Dictionary<FolderData, List<string>>();
public static void Add(FolderData key,string value)
{
if(!SpriteDic.ContainsKey(key))
{
SpriteDic[key] = new List<string>();
}
SpriteDic[key].Add(value);
}
/// <summary>
/// 移除掉已经改名的文件缓存
/// </summary>
public static void RemoveChanged()
{
foreach (var pair in SpriteDic)
{
int count = pair.Value.Count;//每次移除一个元素,Count都会变
for(int i = 0; i < count; i++)
{
if (!System.IO.File.Exists(pair.Value[i]))
{
pair.Value.RemoveAt(i);
i--;
count--;
}
}
}
}
}
public class FolderData
{
public string FolderPath;//需要对命名进行管理的文件夹路径
public string NameTip;//正确名称的提示信息
}

EditorWindow.GetWindow<NameMgrWindow>()方法能够生成NameMgrWindow的实例。

NameMgrWindowData:用来缓存NameMgrWidow内需要显示的数据的类,里面有一个字典,key值是需要设置命名规范的文件夹信息类,value是一个列表,保存了当前命名不规范的图片资源的全路径。

FolderData:用来保存需要强制命名的文件夹路径,以及正确命名的提示