Recently Protobuf too many people to ask questions of, and this again to move out!

pb killer

Let me introduce myself pbkiller

pbkiller : I am a professional fight on the issue protobuf trained killer, I can easily get protobuf many problems in Cocos Creaotr development for you, and lets you freely once tortured tortured your protbuf problem.

One, Shinpuku

Proto are all surrender at the foot of the master

I pbkiller most powerful skills can be quickly arrested proto file, so that they surrender at the foot of the owner, whether it is in the browser, phone, desktop environment.

1. Calling

Qing Yin only whispered my name I'll be there by your side, for my master I was very gentle.

let pbkiller = require('pbkiller');

2. Let proto kneeling in front of the master file

For the proto file I was cruel, but please the owner in advance to all proto files in assets/resources/pbthe directory, call for me preload, I immediately went to you and then use loadAll () command, I will catch proto file, so that they all kneel in your front.

//所有proto对象都集中在返回的pb对象上
pbkiller.preload(() => {
    let pb = pbkiller.loadAll(); 
});

3. Let the same JSON format kneel

If the owner likes proto json file format, now need to trouble the owner to use pbjs tool protobufjs incidental to convert the file into a proto json format stored in the assets / resources / pb directory, I have it in the same knees.

//所有proto对象都集中在返回的pb对象上
let pb = pbkiller.loadAll('json'); 

4. arrested proto file with the name of the

If the owner just arrest one or more proto file, you can use loadFromFile.


let pb = pbkiller.loadFromFile('xxx.proto');

let pb = pbkiller.loadFromFile(['xxx.proto', 'yyy.proto']);

let pb = pbkiller.loadFromFile(['xxx.json', 'yyy.json']);

5. arrested in different regions of proto file

Master, I default path is arrested Assets / Resources / pb , you can modify the path through the property below.


//使用root属性修改路径
pbkiller.root = 'my-pb-path';

//此时逮捕路径为:assets/resources/my-pb-path
pbkiller.loadAll();

Second, torture

Although our name in with the killer, but most happy not immediately kill the enemy, but it is torture, probably because he had tortured my master, so you can not let it die too simple, ha ha ha.

1. proto uncovered a master to wrestle (instantiated)

Master, I give you the proto captured, you can use the new keyword, the proto random scuffle.

//逮捕所有proto文件,并指定编译grace.proto.msg包路径下的所有对象
let pb = pbkiller.loadAll('proto', 'grace.proto.msg');

let player = new pb.Player();    //扭打Player
player.name = 'ShawnZhang';      //再踹一脚

2. suffer a slap in the face and whispered cry (serialization and de-serialization)

Master, let proto endure a slap in the face to use toArrayBuffer, if you feel a little tired and could use toBuffer their effect is entirely different.

let player = new pb.Player();  //先扭打
let data = player.toBuffer();  //再来一巴掌

We play into the player above data, it now fight back, let him look like a cry, but just quietly cry, please answer decode the next instruction.

 //主人允许你小声哭,回来吧
let player = pb.Player.decode(data);

Third, infighting

I: "pbkiller did not think your words so much, let me introduce yourself down, talk so big pile of proto so cruel treatment, and the owner of the owner, particularly nauseating, disgusting." .

pbkiller: "This year, do you think the killer is so easy when Mody, the killer must learn self-marketing, focus on user experience, that you've introduced a way out."

Me: "Do you actually say I'm out, and I carefully put you up to kill."

pbkiller: "you are the boss, you have the final say, I see how many people like the way you talk" pbkiller a look of disdain on his face.

Fourth, the official explanation

pbkiller plugin library can help you simplify the use protobufjs in Cocos Creator, the Creator and is compatible with all platforms and versions!

1. Install

pbkiller plug-in installed in two modes:

  • install-src: source mode , this mode will protobufjs original code into the project, not dependent on any external file. Npm not familiar to users recommend using this mode installation.
  • install-lite: simplified mode , this mode requires the installation in advance protobufjs npm module, the install command: npm install protobufjs @ 5

After installation is complete, the file is imported as follows:

  1. protobufjs Source (catalog no such simplified model)
  2. pbkiller source, and fs / path masquerading module
  3. Simple test scenarios and code
  4. Test proto file

Here is the import file and directory structure:

pbkiller
├── protobuf    protobufjs源码
│  ├── bytebuffer.js
│  ├── long.js
│  └── protobufjs.js
├── src     pbkiller源码
│  ├── fs.js    fs伪装
│  ├── path.js  path伪装
│  └── pbkiller.js  pbkillers核心代码
└── test
    ├── test-pbkiller.fire  测试场景
    └── test-pbkiller.js    测试组件代码
resources           resource/pb是默认的proto文件存放的根目录
└── pb              以下文件为测试用proto文件,可以自行删除
    ├── ActionCode.proto   
    ├── ChatMsg.proto       
    ├── Player.json
    └── Player.proto

2. Quick

Import module

let pbkiller = require('pbkiller');

Load all proto file resources / pb directory

//加载所有proto文件
let pb = pbkiller.loadAll();

//实例化proto中的Player对象
let player = new pb.grace.proto.msg.Player();

File format: [proto | json] default proto

//注意json文件是由protobufjs提供的pbjs工个生成
let pb = pbkiller.loadAll('json');

Specified compiled object path

let pb = pbkiller.loadAll('proto', 'grace.proto.msg');
cc.log(new pb.Player());

3. Special attention

When loading proto can be used to expand? Exhibition called .proto and .json files, pbkiller supports two mix, but need special attention if there is a dependent relationship between proto, please make sure the file format is the same between the dependent files .

file

file

Guess you like

Origin www.cnblogs.com/creator-star/p/11578252.html