使用pb-egret处理protobuf

it2026-02-09  0

参考:https://github.com/WanderWang/protobuf-egret

查看最近版本protobufjs

$ npm view protobufjs maintainers: - dcode <dcode@dcode.io> - protobufjs <dcode+npm-protobufjs@dcode.io> dist-tags: latest: 6.10.1 next: 6.10.1-beta.0

安装最近版本:

$ npm install protobufjs@6.10.1 -g

安装pb-egret

$ npm install @egret/protobuf -g C:\Users\andrew\AppData\Roaming\npm\pb-egret -> C:\Users\andrew\AppData\Roaming\npm\node_modules\@egret\protobuf\out\cli.js + @egret/protobuf@1.2.1 added 10 packages from 5 contributors in 2.039s

进入建立好的egret工程,在工程下生成protobuf-egret文件夹protobuf

$ pb-egret add

 

写Login.proto配置文件:

syntax = "proto3"; package LoginPb; option optimize_for = LITE_RUNTIME; //玩家角色信息 message UserInfo { int32 role_id = 1; string name = 2; int64 age = 3; int32 score = 4; } // 房间信息 message Room { int32 id = 1; string name = 2; repeated UserInfo users = 3; }

将proto文件放到protobuf/protofile文件夹中,然后执行pb-egret generate命令

set CUR_PATH=%cd% echo "CUR_PATH = "%CUR_PATH% xcopy ..\..\protocol\*.proto .\protobuf\protofile\ /Y pb-egret generate pause

在protobuf/bundles中生成proto对应的文件 protobuf-bundles.d.ts、protobuf-bundles.js、protobuf-bundles.min.js

 

最新回复(0)