//==========================
// - FileName: TextTrueSetting.cs
// - Created: true.
// - CreateTime: 2020/11/11 19:44:02
// - Email: 1670328571@qq.com
// - Region: China WUHAN
// - Description: 当前资源导入前
//==========================
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;
public class TextTrueSetting : AssetPostprocessor
{
private static FolderData playerData = null;
private void OnPreprocessTexture()
{
SetTexturePara();
NamingConventions();
}
private void NamingConventions()
{
PlayerNaming();
}
private void PlayerNaming()
{
//需要规范的文件名
if(assetPath.Contains(ConstPath.Res_Pictures_BattleUI))
{
string name = Path.GetFileNameWithoutExtension(Path.GetFileName(assetPath));
string rule = "^[0-9]+_[0-9]+$";
Match result = Regex.Match(name, rule);
if(result.Value == "")
{
if(playerData == null)
{
playerData = new FolderData
{
FolderPath = ConstPath.Res_Pictures_BattleUI,
NameTip = "0_0"
};
}
Debug.LogError("当前导入资源名称错误,名称为:" + name);
NameMgrWindowData.Add(playerData, assetPath);
NameMgrWindow.ShowWindow();
}
}
}
private void SetTexturePara()
{
TextureImporter importer = (TextureImporter)assetImporter;
//所有导入图片设置为当前类型
importer.textureType = TextureImporterType.Sprite;
importer.mipmapEnabled = false;
}
}
//==========================
// - FileName: NameMgrWindow.cs
// - Created: true.
// - CreateTime: 2021/02/18 23:59:06
// - Email: 1670328571@qq.com
// - Region: China WUHAN
// - Description: 弹窗
//==========================
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
public class NameMgrWindow : EditorWindow
{
//缓存
private Dictionary<string, string> namesDict = 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.spriteDict)
{
//标题-横向
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 (string path in pair.Value)
{
//一组数据排列
GUILayout.BeginVertical();
Texture2D texture2D = AssetDatabase.LoadAssetAtPath<Texture2D>(path);
//显示所有有问题的图片
GUILayout.Box(texture2D, GUILayout.Height(100), GUILayout.Width(100));
string name = Path.GetFileNameWithoutExtension(path);
if(!namesDict.ContainsKey(name))
{
namesDict[name] = name;
}
//横向对象
GUILayout.BeginHorizontal();
//文本输入框
namesDict[name] = GUILayout.TextArea(namesDict[name], GUILayout.Width(80));
if (GUILayout.Button("确认", GUILayout.Width(50)))
{
if (name != namesDict[name])
{
//修改文件名称
ChangFileName(name, namesDict[name], path);
namesDict.Remove(name);
}
//刷新数据
AssetDatabase.Refresh();
}
GUILayout.EndHorizontal();
GUILayout.EndVertical();
}
GUILayout.BeginHorizontal();
}
}
private void ChangFileName(string sourceName,string destName,string path)
{
string destPath = path.Replace(sourceName, destName);
if(File.Exists(destPath))
{
Debug.LogError("当前文件名称已存在");
}
else
{
File.Move(path, destPath);
}
}
}
public class NameMgrWindowData
{
public static Dictionary<FolderData, List<string>> spriteDict = new Dictionary<FolderData, List<string>>();
public static void Add(FolderData key,string value)
{
if (!spriteDict.ContainsKey(key))
{
spriteDict[key] = new List<string>();
}
spriteDict[key].Add(value);
}
public static void UpdateData()
{
foreach (KeyValuePair<FolderData,List<string>> pari in spriteDict)
{
int count = pari.Value.Count;
for (int i = 0; i < count; i++)
{
//如果不存在
if (!File.Exists(pari.Value[i]))
{
pari.Value.Remove(pari.Value[i]);
//考虑到如下问题
//0 1 2 3
//1 2 3
i--;
}
}
}
}
}
public class FolderData
{
//当前文件夹路径
public string FolderPath;
//当前所在的名称格式、提示信息
public string NameTip;
}