将Excel表格数据转成Protobuf V3【Unity】

>版权声明:本文为Jumbo原创文章,采用[知识共享 署名-非商业性使用-禁止演绎 4.0 国际 许可协议],转载前请保证理解此协议

原文出处:https://www.jianshu.com/p/747c63d5d56b

Excel表格数据转成Protobuf V3Unity

作者:Jumbo @2015/12/30

感谢:DonaldWjameyli

工作原理流程:

1、xls2protobuf_v3.py 将表格数据转成:*.proto *.bin *.txt等

2、protoc.exe 通过读取*.proto生成*.cs脚本解析类

3、Unity通过protobuf-V3.0\csharp_unity\Google.Protobuf插件读取*.bin文件

准备工作:

1、安装Python2.7,配置系统环境变量,如下图:


2、切换到目录setuptools-18.7,执行 python setup.py install

3、切换到目录xlrd-0.9.4,执行 python setup.py install

4、切换到目录protobuf-V3.0\python,执行 python setup.py install

开始转换:

1、转换工具目录结构:

-ConvertTools

-xls表格数据

-xls2proto通过表格数据生成*.proto(描述文件) *.bin(二进制) *.txt(明文)等

-proto2cs生成的*.cs文件

-ConvertList.txt批处理数据转换列表,配合ResConvertAll.bat

-ResConvertAll.bat 执行批处理转换所有数据,读取ConverList.txt

-ResConvert.bat 单个表格数据处理命令格式:ResConvert [表格名] [xls文件名]

-xls2protobuf_v3.py 表格数据转换工具,支持proto3最新格式

2、数据表格制作: 参考xls2protobuf_v3.py定义描述

3、protoc.exe相关命令参数 : protoc --help查看 需要配置环境变量,在Path后面加上;{protobuf-V3.0\src\protoc.exe 路径目录}

Unity使用:

1、protobuf支持Unity的CSharp库:将protobuf-V3.0\csharp_unity\Google.Protobuf文件夹拷贝到Unity项目Plugins下面

2、proto生成的*.cs代码目录:Scripts\ResData

3、表格生成的*.bin数据目录:StreamingAssets\DataConfig

4、参考代码示例:

usingSystem;

usingSystem.IO;

usingUnityEngine;

usingGoogle.Protobuf;

namespaceAssets.Scripts.ResData

{

classResDataManager

{

privatestaticResDataManagersInstance;

publicstaticResDataManagerInstance

{

get

{

if(null== sInstance)

{

sInstance =newResDataManager();

}

returnsInstance;

}

}

publicbyte[] ReadDataConfig(stringFileName)

{

FileStreamfs = GetDataFileStream(FileName);

if(null!= fs)

{

byte[] bytes =newbyte[(int)fs.Length];

fs.Read(bytes, 0, (int)fs.Length);

fs.Close();

returnbytes;

}

returnnull;

}

privateFileStreamGetDataFileStream(stringfileName)

{

stringfilePath = GetDataConfigPath(fileName);

if(File.Exists(filePath))

{

FileStreamfs =newFileStream(filePath,FileMode.Open);

returnfs;

}

returnnull;

}

privatestringGetDataConfigPath(stringfileName)

{

returnApplication.streamingAssetsPath +"/DataConfig/"+ fileName;

}

}

}

读取二进制数据示例:

//GOODS_INFO_ARRAY对应的结构:GOODS_INFO

GOODS_INFO_ARRAYarr =GOODS_INFO_ARRAY.Parser.ParseFrom(ResDataManager.Instance.ReadDataConfig("Goods.bin"));

GitHub:https://github.com/JumpWu/xls2protobuf

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容