利用客户端的表配置来查询对应资源的路径下是否存在该资源
这几天有个测试的需求,就是要把时装都跑一遍检查哪一些没有资源,但是时装太多了,有七百多套,一个个去看显然是不现实的。
不过还好也是查找资源,所以就打算做一个检查工具,思路就是利用表内配置的资源ID,去查找对应资源目录下是否存在该资源。
这里大体的思路就是先做一个表的遍历,将对应的ID抽出来,并且放到对应的字典内。
然后再去资源目录下遍历文件名称,由于是有ID的,在名字基本一致的情况下,通过正则的方式将ID取出,然后存入list中。
再通过遍历字典,比对字典内的values是否存在于list中即可。
2021年5月7日更新日志:
1、增加了日志的输出,包括如果目录下没有对应的日志文件也会去生成
using System.Diagnostics; using System.Collections.Generic; using System.IO; using System.Text; using System; using System.Text.RegularExpressions; using UnityEditor; using UnityEngine; using UnityEngine.UI; using MiniFire.Handler; using System.Linq; namespace MiniEditor { public class AvatarPrefabCheckTool : EditorWindowBase { private static AvatarPrefabCheckTool _instance = null; public static AvatarPrefabCheckTool instance { get { if (_instance == null) { _instance = GetWindow<AvatarPrefabCheckTool>(); _instance.minSize = new Vector2(600, 50); _instance.titleContent = new GUIContent("时装检查工具"); } return _instance; } } private string CharacterPath = "/GameAssets/Character/looseItems"; private string facePath = "/GameAssets/Character/faceTextrue"; private const string SAVE_Character_PATH = "SAVE_Characte_ALTAS_PATH"; private const string SAVE_face_PATH = "SAVE_FACE_ALTAS_PATH"; public override void Initialization() { base.Initialization(); CharacterPath = EditorPrefs.GetString(SAVE_Character_PATH, CharacterPath); facePath = EditorPrefs.GetString(SAVE_face_PATH, facePath); } protected override void OnTitleGUI() { base.OnTitleGUI(); } protected override void OnBodyGUI() { base.OnBodyGUI(); EditorGUILayout.BeginHorizontal(); CharacterPath = EditorGUILayout.TextField("时装资源目录:", CharacterPath); if (GUILayout.Button("Browse", GUILayout.ExpandWidth(false))) { string path = EditorUtility.OpenFolderPanel("", "", ""); if (path != null) { CharacterPath = path.Substring(path.IndexOf("Assets") + 6).Replace("\\", "/") + "/"; UnityEngine.Debug.LogWarning("Select CharacterPath folder = " + CharacterPath); EditorPrefs.SetString(SAVE_Character_PATH, CharacterPath); } } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); facePath = EditorGUILayout.TextField("贴图目录:", facePath); if (GUILayout.Button("Browse", GUILayout.ExpandWidth(false))) { string path = EditorUtility.OpenFolderPanel("", "", ""); if (path != null) { facePath = path.Substring(path.IndexOf("Assets") + 6).Replace("\\", "/") + "/"; UnityEngine.Debug.LogWarning("Select m_BigImgPath folder = " + facePath); EditorPrefs.SetString(SAVE_face_PATH, facePath); } } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("检查时装/贴图资源是否存在缺漏", EditorNormalStyle.ToolbarButton, GUILayout.MaxWidth(200))) { if (CharacterPath != null) { CheckAvatar(true); UnityEngine.Debug.Log("请在" + System.Environment.CurrentDirectory + "这里放你的C#工具路径所在脚本,或者是你想放日志的地方" + "路径获取输出日志"); } } EditorGUILayout.EndHorizontal(); } List<string> CharacterFolderList = new List<string>(); List<string> faceTextrueList = new List<string>(); List<string> PrefabFolderList = new List<string>(); List<string> TextrueFolderList = new List<string>(); List<string> ModelIDList = new List<string>(); Dictionary<string, string> ModelIDDict = new Dictionary<string, string>(); Dictionary<string, string> FaceIDDict = new Dictionary<string, string>(); Dictionary<string, string> PrefabList = new Dictionary<string, string>(); private void WriteLog(string content) { string path = System.Environment.CurrentDirectory + "这里放你的C#工具路径所在脚本,或者是你想放日志的地方"; if (!string.IsNullOrEmpty(path)) { path = path + "\\" + DateTime.Now.ToString("yyyyMMddHHmm") + ".txt"; if (!File.Exists(path)) { FileStream fs = File.Create(path); fs.Close(); } if (File.Exists(path)) { StreamWriter sw = new StreamWriter(path, true, System.Text.Encoding.Default); sw.WriteLine(content); sw.Close(); } } } private void CheckAvatar(bool canBoo) { ModelIDDict.Clear(); ModelIDList.Clear(); FaceIDDict.Clear(); PrefabFolderList.Clear(); TextrueFolderList.Clear(); var cfg = Configs.Avatar; if (cfg != null) { foreach (var item in cfg.Values) { //Debug.Log(item.ModelID); if(item.Part != 2) { if(item.Part != 10 && item.Part != 9 && item.Part != 14)/////剔除了套装、足迹、伪装 { string ModelID = item.ModelID.ToString(); string ID = item.ID.ToString(); ModelIDDict.Add(ID, ModelID); } } else { string ModelID = item.ModelID.ToString(); string ID = item.ID.ToString(); ///UnityEngine.Debug.Log(ID); FaceIDDict.Add(ID, ModelID); } } } string RegexStr = @"\d+"; CharacterFolderList = EditorCommon.IOGetAllFilesInFolderPath(Application.dataPath + CharacterPath); int tempLength = CharacterFolderList.Count; ///Debug.Log(tempLength); for (int i = 0; i < tempLength; i++) { if (canBoo) { ///Debug.Log(CharacterFolderList[i]); string s = CharacterFolderList[i]; Match m1 = Regex.Match(s, RegexStr); Console.WriteLine(m1.Value); if(m1.Value != "") { string ID = m1.Value; PrefabFolderList.Add(ID); } } } faceTextrueList = EditorCommon.IOGetAllFilesInFolderPath(Application.dataPath + facePath); int faceLength = faceTextrueList.Count; for (int i = 0; i < faceLength; i++) { if (canBoo) { ///Debug.Log(CharacterFolderList[i]); string s = faceTextrueList[i]; Match s1 = Regex.Match(s, RegexStr); Console.WriteLine(s1.Value); if(s1.Value != "") { string faceID = s1.Value; ///Debug.Log(faceID); TextrueFolderList.Add(faceID); } } } foreach (KeyValuePair<string, string> kvp in ModelIDDict) { if(!PrefabFolderList.Contains(kvp.Value)) { UnityEngine.Debug.LogError(string.Format("时装中不存在的Prefab文件的ID:{0} 模型ID:{1}", kvp.Key, kvp.Value)); WriteLog(string.Format("时装中不存在的Prefab文件的ID:{0} 模型ID:{1}", kvp.Key, kvp.Value)); } } foreach (KeyValuePair<string, string> fvp in FaceIDDict) { if(!TextrueFolderList.Contains(fvp.Value)) { UnityEngine.Debug.LogError(string.Format("时装中不存在的贴图文件的ID:{0} 贴图ID:{1}", fvp.Key, fvp.Value)); WriteLog(string.Format("时装中不存在的贴图文件的ID:{0} 贴图ID:{1}", fvp.Key, fvp.Value)); } } } } }