using System.Collections.Generic; using System.IO; using System.Text; using Sirenix.OdinInspector; using Sirenix.OdinInspector.Editor; using UnityEngine; namespace Editor.GenResourcePathEditor { public class GenResourcePathEditor : OdinEditorWindow { [UnityEditor.MenuItem("Tool/ZC/GenResourcePathEditor")] private static void OpenWindow() { GetWindow().Show(); } [FolderPath] public List assetFolderPaths; [FolderPath] public string scriptGenFolderPath; [Button("GenGen")] public void Gen() { if (assetFolderPaths.Count == 0 || string.IsNullOrEmpty(this.scriptGenFolderPath)) { Debug.LogError("the folder ??"); return; } List pathList = new List(); List nameList = new List(); foreach (var assetFolderPath in this.assetFolderPaths) { var files = Directory.GetFiles(assetFolderPath); foreach (var s in files) { string assetPath = s.Replace("\\", "/"); // 确保路径格式正确 if (assetPath.Contains(".meta")) continue; pathList.Add(assetPath); var names = assetPath.Replace("/", "_").Split("."); nameList.Add(names[0]); } } GenScript(pathList, nameList); } private void GenScript(List pathList, List nameList) { var path = this.scriptGenFolderPath + "/AssetPathConst.cs"; using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate)) { StringBuilder sb = new StringBuilder(); sb.AppendLine("public class AssetConstPath\n {"); for (var i = 0; i < pathList.Count; i++) { string s = @"public const string NAME = ""PATH"";"; var nameRe = s.Replace("NAME", nameList[i]).Replace("PATH", pathList[i]); sb.AppendLine(nameRe); Debug.Log(nameRe); } sb.AppendLine("}"); string context = sb.ToString(); var bytes = Encoding.UTF8.GetBytes(context); fs.Write(bytes); fs.Dispose(); } } } }