解析ET6框架的配置表的原理和使用

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
 {
       //扩展方法
}

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>