ProtoBuff簡介
用於序列化結構數據的技術。使用特定的生成的代碼,用不同的語言讀寫不同的數據流。
資源下載
https://github.com/protocolbuffers/protobuf
學習資料
https://developers.google.com/protocol-buffers/docs/overview?hl=zh-CN
工具準備
win64的電腦
protobuf源碼
對應版本的visual studio
python環境,本文用python做批處理。因爲我要編譯一個目錄下所有的文件,並且保持目錄結構輸出編譯後的文件
下載編譯工具 https://github.com/protocolbuffers/protobuf/releases
生成unity使用protobuff的DLL
使用visual studio打開對應語言的工程,比如我的地址是 protobuf\csharp\src\Google.Protobuf.sln
打開這個目錄下面的visual studio解決方案,生成解決方案
我用的net45版本的DLL
編譯出csharp源碼
編寫proto的配置文件,proto的語法在學習資料裏面有
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")
我寫的調用python腳本的bat腳本,proto_exe_path是下載的編譯程序所在位置
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
在Unity中使用
複製編譯出DLL到unity下的Assets/Plugings/裏面
複製編譯出的源碼到untiy工程,可以用腳本做自動拷貝操作
demo代碼如下
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);
}
}
工程下載地址
https://download.csdn.net/download/ak47007tiger/12033042
完