using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.IO; using Excel; using UnityEditor; using UnityEngine; using Sirenix.OdinInspector; using Sirenix.OdinInspector.Editor; using System.Linq; using Stary.Evo.TableTextConversion; namespace Stary.Evo.TableTextConversion { public class ConvertedExslDataMessage : OdinEditorWindow { private static ConvertedExslDataMessage window; // 读取的位置 private static string DocFolderPath; // 生成配置文件的位置 private static string Assetpath; // 转化表的名称 private static string newDataName; [MenuItem("Exsl/信息表转换")] static void ShowWindows() { window = (ConvertedExslDataMessage)EditorWindow.GetWindow(typeof(ConvertedExslDataMessage)); window.Show(); } [Title("转化配置的Exsl文件:", titleAlignment: TitleAlignments.Centered)] [HorizontalGroup("BuildPipeline"), HideLabel] [ValueDropdown("GetBuildExslNames")] [OnValueChanged("SetBuildExslNames")] public string selectedExslNames; [Title("转化文件存储的作用域:", titleAlignment: TitleAlignments.Centered)] [HorizontalGroup("BuildPipeline"), HideLabel] [ValueDropdown("GetCreatDomainAllName")] [OnValueChanged("SetCurrentDomainName")] public string selectedDomainNames; #region 信息表转存 //[MenuItem("Exsl/信息表转换")] [Button("转化Exsl表格", ButtonSizes.Large)] public static void CrtateMessage() { #region 读取Excel表格并转成DataSet类型的数据 if (DocFolderPath == null) { Debug.LogError("UnityEvo:请先选择Exsl文件"); return; } if (Assetpath == null) { Debug.LogError("UnityEvo:请先选择生成配置文件的位置"); return; } /*if (!Directory.Exists(DocFolderPath)) { Debug.LogError("UnityEvo:Exsl文件不存在"); return; }*/ if (!Directory.Exists(Assetpath)) { Debug.LogError("UnityEvo:生成配置文件的位置不存在"); return; } FileStream stream = File.Open(DocFolderPath, FileMode.Open, FileAccess.Read); IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream); //获取结果 DataSet result = excelReader.AsDataSet(); int oneRows = result.Tables[0].Rows.Count; //行数 Dictionary dict_Question = new Dictionary(); Dictionary dict_Menu = new Dictionary(); #endregion #region 通过遍历一遍表格所有行的第一列【序号】不为空 int VoiceCount = 0; for (int i = 0; i < oneRows; i++) { string key = FilterTo(result.Tables[0].Rows[i][0].ToString()); if (!string.IsNullOrEmpty(key)) { VoiceCount++; } } #endregion // 条件判断 /*if (newDataName == null) { return; } else if(newDataName == "") { }*/ #region AudioTableData #region 遍历表格相应的数据转换成数据 List messageInfos = new List(); for (int i = 1; i < VoiceCount; i++) { AudioTableData.MessageInfo messageInfo = new AudioTableData.MessageInfo(); //获取对话块 messageInfo.index = i - 1; //文件名称 messageInfo.name = FilterTo(result.Tables[0].Rows[i][0].ToString()); //获取内容 messageInfo.nameMessage = FilterTo(result.Tables[0].Rows[i][1].ToString()); //添加数据 messageInfos.Add(messageInfo); } #endregion ScriptObjectSave("AudioTableData", (data) => { data.infos = new List(); data.infos = messageInfos; }); #endregion } /// /// 过滤:去掉首尾空格和换行符【\n】 /// private static string FilterTo(string self) { self = self.TrimStart(); self = self.TrimEnd(); self = self.Replace("\n", ""); return self; } /// /// ScriptObject数据保存类 /// /// /// 写入数据的一个回调 /// private static void ScriptObjectSave(string assetName, Action action) where T : ScriptableObject { string jsonPath = $"{Assetpath}{assetName}.asset"; if (File.Exists(jsonPath)) { EditorFrameworkUtils.DeleteFile(Assetpath, assetName + ".asset"); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); } jsonPath = jsonPath.Substring(jsonPath.IndexOf("Assets")); // Debug.Log($"要创建的为{jsonPath}"); var data = ScriptableObject.CreateInstance(); //放写入数据操作 action.Invoke(data); AssetDatabase.CreateAsset(data, jsonPath); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); } #endregion /// /// 获取Doc文件夹的路径 /// /// private string GetDocFolderPath() { // 获取 Assets 文件夹的路径 string assetsPath = Application.dataPath; // 获取项目的根目录路径 string projectRootPath = Path.GetDirectoryName(assetsPath); // 构造 Doc 文件夹的路径 string docFolderPath = Path.Combine(projectRootPath, "Doc"); return docFolderPath; } /// /// 获取Doc文件夹下所有的Exsl名 /// /// private List GetBuildExslNames() { string docFolderPath = GetDocFolderPath(); // 检查 Doc 文件夹是否存在 if (!Directory.Exists(docFolderPath)) { Debug.LogError($"UnityEvo:Doc 文件夹不存在,请检查路径:{docFolderPath}"); return null; } List result = new List(); // 遍历 Doc 文件夹中的所有 .xlsx 文件 string[] xlsxFiles = Directory.GetFiles(docFolderPath, "*.xlsx"); result = xlsxFiles.Select(file => Path.GetFileNameWithoutExtension(file)).ToList(); return result; } private void SetBuildExslNames(string newValue) { DocFolderPath = Path.Combine(GetDocFolderPath(), newValue + ".xlsx"); newDataName = newValue; } /// /// 获取全部作用域名 /// public static List GetCreatDomainAllName() { string domainPath = $"{Application.dataPath}/Domain"; string[] domains; // 新增目录获取代码 if (Directory.Exists(domainPath)) { var dirInfo = new DirectoryInfo(domainPath); // 获取直接子目录(不递归) domains = dirInfo.GetDirectories("*", SearchOption.TopDirectoryOnly) .Select(d => d.Name) .ToArray(); } else { domains = Array.Empty(); } return domains.Select(file => Path.GetFileNameWithoutExtension(file)).ToList(); } private void SetCurrentDomainName(string newValue) { Assetpath = Application.dataPath + "/Domain/" + newValue + "/AddressableRes/Config/"; } } }