安装ProtoBuf , 网络上的方法五花八门 . 但是很多都不是那么正规 . 自己通过NPM官网 ( , 总结了一套方法.

一 : 先要安装node.js 和 npm . 没有安装的 , 可以度娘,亦可以参考 : https://blog.51cto.com/aonaufly/1954296    Blog:"TypeScript 体验"

01.png

二:安装Protobuf基础库

npm install protobufjs@6.8.4 -g

npm install @egret/protobuf -g

① : protobufjs@6.8.4

02.png

② : @egret/protobuf

03.png

三 : 将ProtoBuf相关库注入到Egret项目之中

①,新建一个Egret(Eui)项目  (ProtobufNpmDemo)  (各种方法可以建立 , 这里不讲了)

②,我们到项目目录里面(important)

04.png

③,在此资源管理器打开资源管理器

05.png

④,使用命令 : pb-egret add 注入ProtoBuf

06.png

我们再看看我们的项目 , 多了个protobuf文件夹

07.png

四 : 生成protobuf-bundles(实际是将proto文件JS化,有利于OOP思想)

ps : 可以看到目前的bundles文件夹中无任何的资源

①,新建test.proto资源(在protobuf\protofile中)

②,使用pb-egret generate命令

08.png

我们再看看bundles , 已经有文件了

09.png

这些都是根据test.proto生成的类

test.proto:

package Test;message Login{    required string userName = 1;    required string password = 2;    optional int32 sex = 3;    required bool isFirstLogin = 4;    repeated string param = 5;}

生成的protobuf-bundles.d.ts(其一)如下:

type Long = protobuf.Long;/** Namespace Test. */declare namespace Test {    /** Properties of a Login. */    interface ILogin {        /** Login userName */        userName: string;        /** Login password */        password: string;        /** Login sex */        sex?: (number|null);        /** Login isFirstLogin */        isFirstLogin: boolean;        /** Login param */        param?: (string[]|null);    }    /** Represents a Login. */    class Login implements ILogin {        /**         * Constructs a new Login.         * @param [properties] Properties to set         */        constructor(properties?: Test.ILogin);        /** Login userName. */        public userName: string;        /** Login password. */        public password: string;        /** Login sex. */        public sex: number;        /** Login isFirstLogin. */        public isFirstLogin: boolean;        /** Login param. */        public param: string[];        /**         * Creates a new Login instance using the specified properties.         * @param [properties] Properties to set         * @returns Login instance         */        public static create(properties?: Test.ILogin): Test.Login;        /**         * Encodes the specified Login message. Does not implicitly {@link Test.Login.verify|verify} messages.         * @param message Login message or plain object to encode         * @param [writer] Writer to encode to         * @returns Writer         */        public static encode(message: Test.ILogin, writer?: protobuf.Writer): protobuf.Writer;        /**         * Encodes the specified Login message, length delimited. Does not implicitly {@link Test.Login.verify|verify} messages.         * @param message Login message or plain object to encode         * @param [writer] Writer to encode to         * @returns Writer         */        public static encodeDelimited(message: Test.ILogin, writer?: protobuf.Writer): protobuf.Writer;        /**         * Decodes a Login message from the specified reader or buffer.         * @param reader Reader or buffer to decode from         * @param [length] Message length if known beforehand         * @returns Login         * @throws {Error} If the payload is not a reader or valid buffer         * @throws {protobuf.util.ProtocolError} If required fields are missing         */        public static decode(reader: (protobuf.Reader|Uint8Array), length?: number): Test.Login;        /**         * Decodes a Login message from the specified reader or buffer, length delimited.         * @param reader Reader or buffer to decode from         * @returns Login         * @throws {Error} If the payload is not a reader or valid buffer         * @throws {protobuf.util.ProtocolError} If required fields are missing         */        public static decodeDelimited(reader: (protobuf.Reader|Uint8Array)): Test.Login;        /**         * Verifies a Login message.         * @param message Plain object to verify         * @returns `null` if valid, otherwise the reason why it is not         */        public static verify(message: { [k: string]: any }): (string|null);    }}

五:运用

①,代码:

    /**     * 创建场景界面     * Create scene interface     */    protected createGameScene(): void {        let $login : Test.ILogin = new Test.Login({userName:"Aoanufly",password:"123456", sex:1, isFirstLogin:false, param:["test", "array", "param"]});        console.log(`loginName : ${$login.userName}`);    }

②,结果

10.png

补充 --- 

① , Egret官方ProtoBuf:

②:利用命令将template.proto生成JS

pbjs -t static-module -w commonjs -o template.js template.proto

pbts -o template.d.ts template.js