using System; using System.IO; using System.Text; using Sirenix.OdinInspector; using Sirenix.OdinInspector.Editor; using UnityEditor; using UnityEngine; namespace ZEditor { public class GenAssetConstEditor : Editor { // [MenuItem("Tool/自动生成资源常量代码")] // static void Open() // { // GetWindow().Show(); // } public static string csName = "AssetConst"; public static string loaderCsName = "LoaderAssetConst"; [FolderPath] public static string folderPath = "Assets/DemoGame/GameScript/Hotfix/Const"; [FolderPath] public static string loaderFolderPath = "Assets/DemoGame/GameScript/Loader/Const"; [FolderPath] public static string resPath = "Assets/DemoGame/GameRes"; [Sirenix.OdinInspector.FilePath] public static string template = "Assets/DemoGame/GameRes/AssetsConstTemplate.txt"; [MenuItem("Tool/自动生成资源常量代码")] static void GG() { Gen(); } [Button] public static void Gen() { string path = $"{folderPath}/{csName}.cs"; string loaderPath = $"{loaderFolderPath}/{loaderCsName}.cs"; StringBuilder sb = new StringBuilder(); Bridging(ref sb, resPath); var readAllText = File.ReadAllText(template); var content = readAllText.Replace("#CONSTCONTENT#", sb.ToString()).Replace("\\", "/").Replace("#CLASSNAME#", csName); var loaderContent = readAllText.Replace("#CONSTCONTENT#", sb.ToString()).Replace("\\", "/").Replace("#CLASSNAME#", loaderCsName); if (!File.Exists(path)) { File.Create(path).Dispose(); } if (!File.Exists(loaderPath)) { File.Create(loaderPath).Dispose(); } File.WriteAllText(path, content); File.WriteAllText(loaderPath, loaderContent); Debug.Log("生成完成!"); } static void Bridging(ref StringBuilder sb, string path) { DirectoryInfo info = new DirectoryInfo(path); var fileInfos = info.GetFiles(); var directoryInfos = info.GetDirectories(); foreach (var fileInfo in fileInfos) { if (fileInfo.Name.Contains(".meta")) continue; string filePath = fileInfo.FullName.Substring(Application.dataPath.Length - 6); var name = filePath.Replace("\\", "_") .Replace(".", "_") .Replace(" ", String.Empty) .Replace("+", String.Empty); sb.AppendLine($"public const string {name} = @\"{filePath}\";"); } foreach (var directoryInfo in directoryInfos) { Bridging(ref sb, directoryInfo.FullName); } } } }