资源规范工具之导入图片名称规范工具
程序员文章站
2022-04-01 09:44:17
...
(旧)TextureSetting
《资源规范工具之图片格式规范工具》中的脚本,当时只有设置新导入图片参数的功能。
现增加以下功能:
某些文件夹下的图片名称有一个统一的规范,当导入的图片文件名与规范不一致时,会报错并且跳出弹框告诉开发者文件名需要修改。
using System.IO;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;
public class TextureSetting : AssetPostprocessor
{
private static FolderData _playerData;
private void OnPreprocessTexture()
{
SetTexturePara();
NamingConventions();
}
private void SetTexturePara()
{
TextureImporter importer = (TextureImporter)assetImporter;
importer.textureType = TextureImporterType.Sprite;
importer.mipmapEnabled = false;
}
private void NamingConventions()
{
PlayerNaming();
}
private void PlayerNaming()
{
if(assetPath.Contains(Paths.PLAYER_PICTURE_FOLDER))
{
string name = Path.GetFileNameWithoutExtension(assetPath);
string pattern = "^[0-9]+_[0-9]+&";
Match result = Regex.Match(name, pattern);
if(result.Value == "")
{
if(_playerData == null)
{
_playerData = new FolderData();
_playerData.FolderPath = Paths.PLAYER_PICTURE_FOLDER;
_playerData.NameTip = "命名例子:0_0";
}
Debug.LogError("当前导入资源名称错误,名称为:"+name);
NameMgrWindow.ShowWindow();
NameMgrWindowData.Add(_playerData, assetPath);
}
}
}
}
NameMgrWindow.cs
弹框。显示不规范的图片的缩略图和名称,可以在弹框内对名称进行修改。
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
public class NameMgrWindow : EditorWindow
{
private Dictionary<string,string> _namesDic = new Dictionary<string,string>();
public static void ShowWindow()
{
EditorWindow.GetWindow<NameMgrWindow>();
}
private void OnGUI()
{
GUILayout.Label("资源名称管理");
NameMgrWindowData.UpdateData();
foreach(KeyValuePair<FolderData,List<string>> pair in NameMgrWindowData.SpriteDic)
{
GUILayout.BeginHorizontal();//1
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();//1
GUILayout.BeginHorizontal();//4
foreach (string path in pair.Value)
{
GUILayout.BeginVertical();//2
Texture2D texture2D = AssetDatabase.LoadAssetAtPath<Texture2D>(path);
GUILayout.Box(texture2D,GUILayout.Height(80),GUILayout.Width(80));
string name = Path.GetFileNameWithoutExtension(path);
if (!_namesDic.ContainsKey(name))
_namesDic[name] = name;
GUILayout.BeginHorizontal();//3
_namesDic[name] = GUILayout.TextArea(_namesDic[name], GUILayout.Width(40));
if(GUILayout.Button("确认", GUILayout.Width(30)))
{
if (name != _namesDic[name])
{
string tempPath = ChangeFileName(name, _namesDic[name], path);
if (tempPath != null)
{
Debug.LogError("当前文件名称已存在,文件地址为:"+tempPath);
continue;
}
_namesDic.Remove(name);
}
AssetDatabase.Refresh();//刷新Unity编辑界面
}
GUILayout.EndHorizontal();//3
GUILayout.EndVertical();//2
}
GUILayout.EndHorizontal();//4
}
}
private string ChangeFileName(string sourceName,string destName,string path)
{
string destPath = path.Replace(sourceName, destName);
if (File.Exists(destPath))
{
Debug.LogError("当前文件名称已存在");
return destPath;
}
File.Move(path, destPath);
return null;
}
}
public class NameMgrWindowData
{
public static Dictionary<FolderData,List<string>> SpriteDic = new Dictionary<FolderData, List<string>>();
public static void Add(FolderData key, string value1)
{
if(!SpriteDic.ContainsKey(key))
SpriteDic[key] = new List<string>();
SpriteDic[key].Add(value1);
}
public static void UpdateData()
{
foreach (KeyValuePair<FolderData, List<string>> pair in SpriteDic)
{
int count = pair.Value.Count;
for (int i = 0; i < count; i++)
{
if (!File.Exists(pair.Value[i]))
{
pair.Value.Remove(pair.Value[i]);
i--;
count--;
}
}
}
}
}
public class FolderData
{
public string FolderPath;
public string NameTip;
}
上一篇: 提取图像的各个通道,进行显示
下一篇: 强化飞机界面
推荐阅读