赞
踩
用于序列化结构数据的技术。使用特定的生成的代码,用不同的语言读写不同的数据流。
syntax="proto3";
package protobuf;
message Person {
int32 id = 1;
string name = 2;
string email = 3;
}
syntax="proto3";
package protobuf;
message Hello {
int32 id = 1;
string name = 2;
string email = 3;
}
# -*- coding: utf-8 -*- import subprocess, os, sys """ example protoc.exe -I=%srcDir% --csharp_out=%dstDir% %srcDir%/Person.proto 保持src目录中的文件目录结构编译到dst中 """ proto_exe_path = sys.argv[1] src_dir = sys.argv[2] dst_dir = sys.argv[3] def getOutputDir(srcDir,dstDir,filePath): outputPath = filePath.replace(srcDir, dstDir).replace("\\","/") outDir = outputPath[:outputPath.rfind('/')] return outDir def getRelativePath(srcDir, filePath): rPath = filePath[len(srcDir) + 1:] return rPath def getAllProtoFiles(dir, extension): pathList = [] for file in os.listdir(dir): childFile = os.path.join(dir,file) if os.path.isdir(childFile): pathList.extend(getAllProtoFiles(childFile, extension)) elif os.path.splitext(childFile)[1] == extension: pathList.append(childFile.replace('\\','/')) return pathList def complieProto(srcDir,dstDir,filePath): outputDir = getOutputDir(srcDir, dstDir, filePath) if not os.path.exists(outputDir): os.makedirs(outputDir) cmdStr = '{0} -I={1} --csharp_out={2} {3}'.format(proto_exe_path, srcDir, outputDir, filePath).replace('/','\\') return cmdStr files = getAllProtoFiles(src_dir,".proto") print "count src files: ", len(files) cmdFile = open("generate_complie.bat","w") for filePath in files: cmdStr = complieProto(src_dir, dst_dir, filePath) # os.system(cmdStr) cmdFile.write(cmdStr) cmdFile.write('\n') cmdFile.write("pause\n") cmdFile.close() print("generate complete!") subprocess.call("generate_complie.bat")
set proto_exe_path="D:/hpl_projects/github_projects/protoc-3.9.1-win64/bin/protoc.exe"
set src_dir="D:/hpl_projects/github_projects/prototest/src"
set dst_dir="D:/hpl_projects/github_projects/prototest/output"
python proto_batch.py %proto_exe_path% %src_dir% %dst_dir%
pause
using Google.Protobuf; using Protobuf; using UnityEngine; public class TestProtoBuf : MonoBehaviour { void Start() { Person p = new Person(); p.Id = 1; p.Name = "hello"; p.Email = "xx@gmail"; var byteArray = p.ToByteArray(); var p2 = (Person)Person.Descriptor.Parser.ParseFrom(byteArray); Debug.Log(p2.Id); Debug.Log(p2.Name); Debug.Log(p2.Email); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。