一、后端nodejs Express
1、 安装应用生成器npm install express-generator –g 2、 生成项目Express --view=ejs TestProtobuf 3、 进入项目安装依赖包 npm install 4、 安装热更新 npm install -g nodemon :配置package.json:“start”: “nodemon ./bin/www” 5、 npm install google-protobuf --save
D:\WorkSpace\NodeProjects\TestProtobuf
目录结构 protos目录下存放proto消息格式Asset.proto文件
protobuf.js相关语法参考 https://github.com/protobufjs/protobuf.js#examples
syntax ="proto3";
package protobuf;
message Asset{
string code =1;
string name =2;
double px =3;
double py =4;
double pz =5;
double rx =6;
double ry =7;
double rz =8;
double sx =9;
double sy =10;
double sz =11;
string location =12;
string model =13;
bool isDraw =14;
string vecs =15;
string color =16;
}
message AssetList {
repeated Asset list = 1;
}
router下的assets.js,写一个get方法,请求message二进制文件
var express = require('express');
var router = express.Router();
var protobuf = require("protobufjs");
router.get('/', function(req, res, next) {
protobuf.load("./protos/Asset.proto", function (err, root) {
var arr =[];
for(i=0;i<300;i++)
{
for(j=0;j<300;j++)
{
var obj = {code:'a'+i,name:'a'+i,px:i*1.1,py:0,pz:j*1.1,rx:0,ry:0,rz:0,sx:1,sy:1,sz:1,location:'root',model:'cube',isDraw:false,vecs:null,color:null}
arr.push(obj);
}
}
var payload = {list:arr};
var msg = root.lookupType("protobuf.AssetList");
var message = msg.create(payload);
// res.send(message);
var buffer = msg.encode(message).finish();
res.send(buffer);
});
});
module.exports = router;
二、 前端Unity 2019.4.5f1 WebGL .NET4.x
C:\Users\PC-user\Documents\Visual Studio 2015\Projects\ConsoleApplication2
使用Visual Studio2015 新建控制台程序,右键解决方案-属性,修改为.NET FRAMEWORK4.6.1
NuGet安装Package获取相关dll
Install-Package System.Buffers -Version 4.5.1 Install-Package System.Numerics.Vectors -Version 4.5.0 Install-Package System.Runtime.CompilerServices.Unsafe -Version 6.0.0 Install-Package System.Memory -Version 4.5.4 Install-Package Google.Protobuf -Version 3.19.1
拷贝项目packages下lib的dll
packages\System.Buffers.4.5.1\lib\net461 packages\System.Numerics.Vectors.4.5.0\lib\net46 packages\System.Runtime.CompilerServices.Unsafe.6.0.0\lib\net6.0 packages\System.Memory.4.5.4\lib\net461 packages\Google.Protobuf.3.19.1\lib\net45
DLL插件放在Asset/Plugins下
下载protoc-3.19.1-win64,protoc.exe生成c#文件
1、cd 到 protoc-3.19.1-win64\bin目录下 2、protoc.exe生成c#文件
> protoc.exe --proto_path=./ asset.proto --csharp_out=./
asset.cs文件拖到Unity项目中
D:\WorkSpace\UnityProjects2021\Unity2019.4.5f1\TestProtobuffer
public IEnumerator GetData()
{
var url = string.Format("http://localhost:3000/assets");
UnityWebRequest _request = new UnityWebRequest(url, UnityWebRequest.kHttpVerbGET);
_request.downloadHandler = new DownloadHandlerBuffer();
_request.SetRequestHeader("Content-Type", "application/octet-stream;charset=utf-8");
yield return _request.SendWebRequest();
if (_request.isHttpError || _request.isNetworkError)
{
Debug.LogError(_request.error);
}
else
{
IMessage IMasset = new AssetList();
AssetList data = new AssetList();
data = (AssetList)IMasset.Descriptor.Parser.ParseFrom(_request.downloadHandler.data);
Debug.Log(data.List.Count);
count_t = data.List.Count;
CreateManyAsset(data);
}
}
|