1.介紹
在游戲開發中,為了方便策劃程式分工合作,使用.xlsx配置表在所難免,接下來我就決議下ET6框架的配置表的原理和使用,
2.原理
1.ExcelExporter.cs
匯出.xlsx為模板類,并生成json然后動態編譯成protobuf使用
public static void Export()
{
try
{
//讀取bin目錄下的Template.txt
template = File.ReadAllText("Template.txt");
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
//清空clientClassDir,serverClassDir下的檔案
if (Directory.Exists(clientClassDir))
{
Directory.Delete(clientClassDir, true);
}
if (Directory.Exists(serverClassDir))
{
Directory.Delete(serverClassDir, true);
}
//讀取excle檔案
foreach (string path in Directory.GetFiles(excelDir))
{
string fileName = Path.GetFileName(path);
if (!fileName.EndsWith(".xlsx") || fileName.StartsWith("~$"))
{
continue;
}
using Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
using ExcelPackage p = new ExcelPackage(stream);
string name = Path.GetFileNameWithoutExtension(path);
//讀取第一個sheet的(1,1)單元文本,#就不生成,包含c就客戶端生成,包含s就服務端生成,""也都生成
string cs = p.Workbook.Worksheets[0].Cells[1, 1].Text.Trim();
if (cs.Contains("#"))
{
continue;
}
if (cs == "")
{
cs = "cs";
}
if (cs.Contains("c"))
{
//匯出配置類
ExportExcelClass(p, name, ConfigType.c);
}
if (cs.Contains("s"))
{
//匯出配置類
ExportExcelClass(p, name, ConfigType.s);
}
}
//動態編譯把json轉成protobuf
ExportExcelProtobuf(ConfigType.c);
ExportExcelProtobuf(ConfigType.s);
Console.WriteLine("導表成功!");
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
2.ConfigComponentSystem.cs
加載反序列化所有配置表protobuf
public static void Load(this ConfigComponent self)
{
self.AllConfig.Clear();
//獲取所有打上config標簽的
HashSet<Type> types = Game.EventSystem.GetTypes(typeof (ConfigAttribute));
//將每個檔案的位元組流保存到configBytes
Dictionary<string, byte[]> configBytes = new Dictionary<string, byte[]>();
self.ConfigLoader.GetAllConfigBytes(configBytes);
//將位元組流資訊反序列化成物件存在AllConfig字典
foreach (Type type in types)
{
self.LoadOneInThread(type, configBytes);
}
}
3.使用
1.配置excel規則:第二行第二列的#代表忽略,c代表客戶端生成,s代表服務端生成,默認代表兩端都生成,同一個excel的sheet分頁可以組合資料,sheet的名字前#則忽略

2.將配置表放進excel目錄,并運行win_startExcelExport.bat
3.直接使用生成類單例呼叫表資料
await ResourcesComponent.Instance.LoadBundleAsync("config.unity3d");
Game.Scene.AddComponent<ConfigComponent>();
ConfigComponent.Instance.Load();
ResourcesComponent.Instance.UnloadBundle("config.unity3d");
//使用
Log.Error(AIConfigCategory.Instance.Get(201).Order.ToString() + "====================================");
4.技巧:如果想擴展讀表的方法可以單獨寫個檔案擴展,不要寫在自動生成的里面
public partial class AIConfigCategory : ProtoObject
{
//擴展方法
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/376043.html
標籤:其他
