孙广东 2016.11.30
首先要 感谢:DonaldW、jameyli 、JumboWu 他们的分享。
以下内容是在 根据他们的 文章 弄。
反正我 并没有成功, 弄了一下午, 我今天的心情 就和 北京的天气一样。
http://blog.csdn.net/u010019717
工作原理流程:
1、xls2protobuf_v3.py将表格数据转成:*.proto(描述文件) *.bin(二进制,实际存储数据) *.txt(明文)等
2、protoc.exe 通过读取*.proto生成*.cs脚本解析类(protobuf 自带的功能)
3、Unity通过protobuf-V3.0\csharp_unity\Google.Protobuf插件读取*.bin文件
准备工作:
1、安装Python2.7,配置系统环境变量,如下图:
我电脑 安装了两个版本, 如果默认是 2.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、转换工具目录结构:
2、数据表格制作: 参考 xls2protobuf_v3.py 定义描述
3、protoc.exe 相关命令参数 : protoc --help查看 需要配置环境变量,在Path后面加上; {protobuf-V3.0\src\protoc.exe 路径目录} ( 注: 这步别忘了, 还有如果你要使用Protobuff.net 版本,或者Protobuff-V2.x 换成对应的 protoc.exe , 也没有那么简单)
(一)、可以先执行很简单的命令试试:
E:\Desktop\xls2protobuf-master\ConvertTools>xls2protobuf_v3.pyGOODS_INFO goods_info.xls
如果 提示环境变量没有配置的话, 就配置环境变量!
如果 以上操作没有成功, 就不要往下走, 解决问题再说。
(二)、在试一试 批处理脚本 ResConvert.bat
为了程序正常跑起来, 还要创建一个路径:
mkdir E:\project\uframwork\client\Assets\StreamingAssets\DataConfig
mkdir E:\project\uframwork\client\Assets\Scripts\ResData
然后执行 命令:
ResConvertgoods_info GOODS_INFO
然后自己查看 创建的四个文件夹下面都有什么吧! 就能了解到 批处理做了什么
(三)、在试一试 批处理脚本 ResConvertAll.bat
有了 ResConvert.bat 测试作为基础了, 这就好办了。
默认的 下面文本中有内容:
直接双击 ResConvertAll.bat 跑程序就行了! 能正常执行功能, 剩下的修改 就简单了。
Unity使用:
1、 protobuf支持Unity的CSharp库:将protobuf-V3.0\csharp_unity\Google.Protobuf 文件夹拷贝到 Unity项目Plugins下面(注意,不知道有没有 在IOS上运行问题,如果有的话,就将 这个dll 和 生成 cs 在打成Dll 导出使用)
(默认在Unity中使用不了,解决办法是使用 .netframework 3.5 重新编译DLL, 只能使用这个,因为前面生成的Proto文件使用的是3.x) 3.x 的下载位置: https://www.nuget.org/packages/google.protobuf
2.x 的下载位置: https://www.nuget.org/packages/Google.ProtocolBuffersLite/
https://www.nuget.org/packages/Google.ProtocolBuffers/
-net 的下载位置: https://www.nuget.org/packages/protobuf-net/
2、proto生成的 *.cs代码目录:Scripts\ResData
3、表格生成的 *.bin数据目录:StreamingAssets\DataConfig
4、参考代码示例:
using System; using System.IO; using UnityEngine; usingGoogle.Protobuf; namespaceAssets.Scripts.ResData { class ResDataManager { private static ResDataManagersInstance; public static ResDataManager Instance { get { if(null== sInstance) { sInstance =newResDataManager(); } return sInstance; } } public byte[] ReadDataConfig(stringFileName) { FileStream fs =GetDataFileStream(FileName); if(null!= fs) { byte[] bytes =newbyte[(int)fs.Length]; fs.Read(bytes, 0,(int)fs.Length); fs.Close(); return bytes; } return null; } private FileStreamGetDataFileStream(string fileName) { string filePath =GetDataConfigPath(fileName); if(File.Exists(filePath)) { FileStream fs =newFileStream(filePath, FileMode.Open); return fs; } return null; } private string GetDataConfigPath(stringfileName) { returnApplication.streamingAssetsPath +"/DataConfig/"+ fileName; } } }
读取二进制数据示例:
using UnityEngine; using System.Collections; using UFramework; using Assets.Scripts.ResData; public class Test : MonoBehaviour { void Start () { //GOODS_INFO_ARRAY对应的结构:GOODS_INFO var bytes = ResDataManager.Instance.ReadDataConfig("goods_info.bin"); Debug.Log(GOODS_INFO_ARRAY.ItemsFieldNumber); //GOODS_INFO_ARRAY arr = GOODS_INFO_ARRAY.Parser.ParseFrom(bytes); //Debug.Log("Length: " + arr.Items.Count); var bytes2 = ResDataManager.Instance.ReadDataConfig("person_info.bin"); PERSON_INFO_ARRAY arr2 = PERSON_INFO_ARRAY.Parser.ParseFrom(bytes2); Debug.Log("Length: " + arr2.Items.Count); for (int i = 0; i < arr2.Items.Count; i++) { PERSON_INFO personInfo = arr2.Items[i]; Debug.Log("Name: " + personInfo.Name /*+ " " + personInfo.*/); } } }
需要指出的问题 , xls2protobuf_v3.py , 把id这列数据都丢了? 怎么回事
还有就是 goods_info.xls 生成 bin 在Unity 中不能解析,会报错, 但是 person.xls 没有问题!
唉, 有时间 在研究一下吧。。。。。。。。。。。。。。。。。。。
我前面说了, 没有真正的成功!
感兴趣的可以下载 运行一下: https://github.com/SunGuangdong/xls2protobuf
推荐阅读: 再次 感谢他们的分享!
将Excel表格数据转成Protobuf V3【Unity】
来自<http://www.jianshu.com/p/747c63d5d56b>