/// <summary> /// 生成CS类 /// </summary> private static void GenerateClassCode(string name, DataTable table) { if (data_list.Count <= 0) { Debug.LogError(string.Format("{0}----------->没读到数据", name)); return; } //生成文件 string className = string.Format("Config{0}", ExeclUtil.FirstCharToUpper(name)); StringBuilder classsb = new StringBuilder(); classsb.Append(classTemplate); StringBuilder property = new StringBuilder(); property.Append("\n"); StringBuilder read = new StringBuilder(); read.Append("//read"); read.Append("\n"); StringBuilder write = new StringBuilder(); write.Append("//write"); write.Append("\n"); for (int i = 0; i < data_list.Count; i++) { property.Append("\t\t"); property.Append("/// <summary>\n"); property.Append("\t\t"); property.AppendFormat("///{0}\n", data_list[i].desc); property.Append("\t\t"); property.Append("/// </summary>\n"); property.Append("\t\t"); property.AppendFormat("public {0} {1};\n", data_list[i].type, data_list[i].key); read.Append("\t\t\t"); read.AppendFormat("{0} = info.{1}(\"{2}\");\n", data_list[i].key, ExeclUtil.GetMethod(data_list[i].type), data_list[i].key); write.Append("\t\t\t"); write.AppendFormat("info.AddValue(\"{0}\", {1});\n", data_list[i].key, data_list[i].key); } ; classsb.Replace("%CLASSNAME%", className); classsb.Replace("%PROPERTY&", property.ToString()); classsb.Replace("%READ%", read.ToString()); classsb.Replace("%WRITE%", write.ToString()); string file_path = PathUtil.GetRegularPath(string.Format("{0}{1}.cs", class_path, className)); if (File.Exists(file_path)) { File.Delete(file_path); } File.WriteAllText(file_path, classsb.ToString(), Encoding.UTF8); Debug.Log("生成代码文件 ----->" + file_path); }
/// <summary> /// 生成打包代码 /// </summary> private static void GenerateCreatorCode(string name, DataTable table) { if (data_list.Count <= 0) { Debug.LogError(string.Format("{0}----------->没读到数据", name)); return; } //生成文件 string className = string.Format("{0}Creator", ExeclUtil.FirstCharToUpper(name)); string configName = string.Format("Config{0}", ExeclUtil.FirstCharToUpper(name)); StringBuilder classsb = new StringBuilder(); classsb.Append(creatorTemplate); StringBuilder read = new StringBuilder(); read.Append("\n"); for (int i = 0; i < data_list.Count; i++) { ExcelData data = data_list[i]; read.Append("\t\t\t"); read.AppendFormat("c.{0} = {1}(\"{2}\", i);\n", data.key, ExeclUtil.GetMethod(data_list[i].type), data.key); } classsb.Replace("%READ%", read.ToString()); classsb.Replace("%CLASSNAME%", className); classsb.Replace("%CONFIGNAME%", configName); string file_path = PathUtil.GetRegularPath(string.Format("{0}{1}.cs", creator_path, className)); if (File.Exists(file_path)) { File.Delete(file_path); } File.WriteAllText(file_path, classsb.ToString(), Encoding.UTF8); Debug.Log("生成打包文件 ----->" + file_path); }
/// <summary> /// 打包所有配置文件 /// </summary> public void PackAll() { Debug.Log("准备打包所有配置表"); DirectoryInfo info = new DirectoryInfo(excelPath); FileInfo[] files = info.GetFiles("*.xlsx", SearchOption.TopDirectoryOnly); Assembly a = Assembly.Load("Assembly-CSharp"); for (int i = 0; i < files.Length; i++) { FileInfo file = files[i]; using (FileStream stream = File.Open(file.FullName, FileMode.Open, FileAccess.Read)) { //获取配置表名字 string name = file.Name.Substring(0, file.Name.Length - 5); //读取配置表数据 IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream); DataSet result = excelReader.AsDataSet(); DataTableCollection tables = result.Tables; for (int j = 0; j < tables.Count; j++) { DataTable table = tables[j]; if (table.TableName == "data") { string creatorName = string.Format("{0}Creator", ExeclUtil.FirstCharToUpper(name)); string configName = string.Format("Config{0}", ExeclUtil.FirstCharToUpper(name)); string configPath = "Game.Core.Config." + configName; MethodInfo mi = this.GetType().GetMethod("Pack"); MethodInfo miGen = mi.MakeGenericMethod(new Type[] { a.GetType(configPath, true, true) }); miGen.Invoke(this, new object[] { table, creatorName, configName }); break; } } } } AssetDatabase.Refresh(); Debug.Log("所有配置表打包成功"); }