// *********************************************************************** // Assembly : Unity // Author : Kimch // Created : // // Last Modified By : Kimch // Last Modified On : // *********************************************************************** // // // *********************************************************************** namespace F { using System.IO; using UnityEditor; /// /// ExcelPostprocessor /// class ExcelPostprocessor : AssetPostprocessor { [MenuItem("Game/生成数据类")] [MenuItem("Assets/Game/生成数据类")] public static void GenerateItemCs() { const string SAVE_PATH = @"Assets\AGame\Scripts\Item\Static"; var selectObj = Selection.activeObject; if (selectObj) { var assetPath = AssetDatabase.GetAssetPath(selectObj); if (File.Exists(assetPath)) { if (!DecidePathName(assetPath)) { if (CheckExcelExtension(assetPath)) { ExcelToCs(assetPath, SAVE_PATH); } } } else if (Directory.Exists(assetPath)) { var files = Directory.GetFiles(assetPath, "*.xlsx"); float progress = 0f; foreach (var file in files) { if (!DecidePathName(file)) { EditorUtility.DisplayProgressBar("生成数据类", file, progress++ / files.Length); ExcelToCs(file, SAVE_PATH); } } EditorUtility.ClearProgressBar(); } else { // 都不是 } AssetDatabase.Refresh(); } } #region STATIC /// /// /// /// /// private static bool DecidePathName(string path) { var fn = Path.GetFileNameWithoutExtension(path); if (fn == "language") { return true; } return false; } /// Checks the sheet. /// The sheet. /// private static bool CheckSheetName(string sheet) { if (!string.IsNullOrEmpty(sheet)) { if (sheet[0] == '_') return false; if (char.IsUpper(sheet[0])) return true; UnityEngine.Debug.LogWarning("Sheet name is not support."); } return false; } /// Gets the file name without extension. /// The path. /// private static string GetFileNameWithoutExtension(string path) { return Path.GetFileNameWithoutExtension(path).ToLower(); } /// Gets the text full path. /// The asset path. /// private static string GetJsonFullPath(string assetPath) { string fileName = Path.GetFileNameWithoutExtension(assetPath); return Path.Combine("Assets/AGame/Configs/Client", fileName + ".json"); } private static string GetJsonFullPath2(string assetPath) { string fileName = Path.GetFileNameWithoutExtension(assetPath); return Path.Combine("Assets/AGame/Configs/Server", fileName + ".json"); } /// Gets the text full path. /// The asset path. /// private static string GetXmlFullPath(string assetPath) { string fileName = Path.GetFileNameWithoutExtension(assetPath); return Path.ChangeExtension(assetPath, ".xml").ToLower(); } private static bool CheckExcelExtension(string fileName) { var fn = Path.GetFileName(fileName); var ext = Path.GetExtension(fn); return ".xlsx" == ext && !fn.StartsWith("~$"); } private static bool CheckJsonExtension(string fileName) { var ext = Path.GetExtension(fileName); return ".json" == ext; } /// /// 对齐列 /// /// private static void ExcelToJson(string fileName) { if (EditorApplication.isPlaying) return; string saveFilePath = GetJsonFullPath(fileName); if (DecidePathName(fileName)) ExcelUtils.ExcelToJson1(fileName, saveFilePath, true, CheckSheetName); else ExcelUtils.ExcelToJson1(fileName, saveFilePath, false, CheckSheetName); AssetDatabase.Refresh(); } /// /// 标准json格式 /// /// private static void ExcelToJson2(string fileName) { string saveFilePath = GetJsonFullPath2(fileName); ExcelUtils.ExcelToJson2(fileName, saveFilePath, CheckSheetName); AssetDatabase.Refresh(); } /// /// xml /// /// public static void ExcelToXml(string fileName) { var saveFilePath = Path.GetDirectoryName(fileName); ExcelUtils.ExcelToXml(fileName, saveFilePath, CheckSheetName); AssetDatabase.Refresh(); } /// /// /// /// /// public static void ExcelToCs(string fileName, string savePath) { ExcelUtils.ExcelToCs1(fileName, savePath, CheckSheetName, "G", "Item", "", "Item, IItem", "", false, false); AssetDatabase.Refresh(); } /// /// /// /// /// public static bool VerificationJson(string fileName) { var jsonText = File.ReadAllText(fileName); try { EditorJsonUtility.FromJsonOverwrite(jsonText, ""); UnityEngine.Debug.Log($"[{Path.GetFileNameWithoutExtension(fileName)}] 格式正确."); } catch (System.Exception) { UnityEngine.Debug.Log($"[{Path.GetFileNameWithoutExtension(fileName)}] 格式错误."); } return true; } #endregion #region UNITY /// Called when [postprocess all assets]. /// The imported assets. /// The deleted assets. /// The moved assets. /// The moved from asset paths. public static void OnPostprocessAllAssets( string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths) { foreach (var importedAsset in importedAssets) { if (CheckExcelExtension(importedAsset)) { ExcelToJson(importedAsset); } else if (CheckJsonExtension(importedAsset)) { VerificationJson(importedAsset); } } foreach (var deletedAsset in deletedAssets) { if (CheckExcelExtension(deletedAsset)) { string txtFile = GetJsonFullPath(deletedAsset); AssetDatabase.DeleteAsset(txtFile); AssetDatabase.Refresh(); } } for (var i = 0; i < movedAssets.Length; i++) { if (CheckExcelExtension(movedAssets[i])) { AssetDatabase.RenameAsset(GetJsonFullPath(movedFromAssetPaths[i]), GetFileNameWithoutExtension(movedAssets[i])); } } } #endregion } }