相关插件及版本:
“dependencies”: {
“google-protobuf”: “3.15.5”,
“ts-protoc-gen”: “~0.15.1-pre.a71b34e”,
“@types/google-protobuf”: “3.15.12”
}
cocoscreator 3.8.2,生成命令:
set PROTOC_GEN_TS_PATH="CD/node_modules/.bin/protoc-gen-ts.cmd"
set OUT_DIR=“assets/Script/proto”
protoc.exe -I=proto --plugin=protoc-gen-ts=PROTOC_GEN_TS_PATH --ts_out=OUT_DIR proto/*.proto
protoc.exe的版本是"protoc-3.15.8-win64"。
test.proto:
syntax = “proto3”;
message TestInfo {
int32 accountType = 1;
}
assets/Script/proto/test_pb.d.ts:
// package:
// file: test.proto
import * as jspb from “google-protobuf”;
export class TestInfo extends jspb.Message {
getAccounttype(): number;
setAccounttype(value: number): void;
serializeBinary(): Uint8Array;
toObject(includeInstance?: boolean): TestInfo.AsObject;
static toObject(includeInstance: boolean, msg: TestInfo): TestInfo.AsObject;
static extensions: {[key: number]: jspb.ExtensionFieldInfo<jspb.Message>};
static extensionsBinary: {[key: number]: jspb.ExtensionFieldBinaryInfo<jspb.Message>};
static serializeBinaryToWriter(message: TestInfo, writer: jspb.BinaryWriter): void;
static deserializeBinary(bytes: Uint8Array): TestInfo;
static deserializeBinaryFromReader(message: TestInfo, reader: jspb.BinaryReader): TestInfo;
}
export namespace TestInfo {
export type AsObject = {
accounttype: number,
}
}
使用处的代码:
import { IProtocolHelper, NetData } from ‘./NetInterface’;
//import { TestInfo } from ‘…/proto/test.d’;
import { TestInfo } from ‘…/proto/test_pb.d’;
export class ProtobufProtocolHelper implements IProtocolHelper {
private typeMessageParser: Map<number, any> = new Map();
constructor() {
console.log('constructor');
let testInfo = new TestInfo();
//testInfo.accountType = 1;
console.log(testInfo);
testInfo.setAccounttype(1);
console.log(JSON.stringify(testInfo));
}
编辑器环境运行报错:
2024-2-5 15:54:25 - info: [PreviewInEditor] Forward render pipeline initialized.
2024-2-5 15:54:26 - error: [PreviewInEditor] {hidden(::SceneExecutorImportExceptionHandler::)} TypeError: Class extends value undefined is not a constructor or null
at Object.execute (file:///G:/mini/cocos_creator_framework-creator3.0/temp/programming/packer-driver/targets/editor/chunks/5f/file:/G:/mini/cocos_creator_framework-creator3.0/assets/Script/proto/test_pb.d.ts36)
at doExec (C:\ProgramData\cocos\editors\Creator\3.8.2\resources\app.asar\node_modules@editor\lib-programming\static\executor\systemjs-bridge\out\index.js:517:30)
at postOrderExec (C:\ProgramData\cocos\editors\Creator\3.8.2\resources\app.asar\node_modules@editor\lib-programming\static\executor\systemjs-bridge\out\index.js:508:21)
at C:\ProgramData\cocos\editors\Creator\3.8.2\resources\app.asar\node_modules@editor\lib-programming\static\executor\systemjs-bridge\out\index.js:493:28
at Array.forEach ()
at postOrderExec (C:\ProgramData\cocos\editors\Creator\3.8.2\resources\app.asar\node_modules@editor\lib-programming\static\executor\systemjs-bridge\out\index.js:491:10)
at C:\ProgramData\cocos\editors\Creator\3.8.2\resources\app.asar\node_modules@editor\lib-programming\static\executor\systemjs-bridge\out\index.js:493:28
at Array.forEach ()
at postOrderExec (C:\ProgramData\cocos\editors\Creator\3.8.2\resources\app.asar\node_modules@editor\lib-programming\static\executor\systemjs-bridge\out\index.js:491:10)
at C:\ProgramData\cocos\editors\Creator\3.8.2\resources\app.asar\node_modules@editor\lib-programming\static\executor\systemjs-bridge\out\index.js:440:12Error: [PreviewInEditor] {hidden(::SceneExecutorImportExceptionHandler::)} TypeError: Class extends value undefined is not a constructor or null
at C:\ProgramData\cocos\editors\Creator\3.8.2\resources\app.asar\node_modules@editor\lib-programming\static\executor\systemjs-bridge\out\index.js:493:28
at Array.forEach ()
at postOrderExec (C:\ProgramData\cocos\editors\Creator\3.8.2\resources\app.asar\node_modules@editor\lib-programming\static\executor\systemjs-bridge\out\index.js:491:10)
at C:\ProgramData\cocos\editors\Creator\3.8.2\resources\app.asar\node_modules@editor\lib-programming\static\executor\systemjs-bridge\out\index.js:493:28
at Array.forEach ()
at postOrderExec (C:\ProgramData\cocos\editors\Creator\3.8.2\resources\app.asar\node_modules@editor\lib-programming\static\executor\systemjs-bridge\out\index.js:491:10)
at C:\ProgramData\cocos\editors\Creator\3.8.2\resources\app.asar\node_modules@editor\lib-programming\static\executor\systemjs-bridge\out\index.js:440:12
at Logger._logHandler (C:\ProgramData\cocos\editors\Creator\3.8.2\resources\app.asar\builtin\scene\dist\script\3d\manager\startup\log.ccc:1:492)
at Logger.record (C:\ProgramData\cocos\editors\Creator\3.8.2\resources\app.asar\node_modules@base\electron-logger\lib\renderer.ccc:1:458)
at console.error (C:\ProgramData\cocos\editors\Creator\3.8.2\resources\app.asar\node_modules@base\electron-logger\lib\renderer.ccc:1:1414)
at ScriptManager._handleImportException (C:\ProgramData\cocos\editors\Creator\3.8.2\resources\app.asar\builtin\scene\dist\script\3d\manager\scripts.ccc:1:5358)
at Executor.importExceptionHandler [as _importExceptionHandler] (C:\ProgramData\cocos\editors\Creator\3.8.2\resources\app.asar\builtin\scene\dist\script\3d\manager\scripts.ccc:1:3452)
at Executor._onModuleLoaded (C:\ProgramData\cocos\editors\Creator\3.8.2\resources\app.asar\node_modules@editor\lib-programming\src\executor\index.ts:359:22)
at SystemJS.onload (C:\ProgramData\cocos\editors\Creator\3.8.2\resources\app.asar\node_modules@editor\lib-programming\src\executor\index.ts:87:18)
at triggerOnload (C:\ProgramData\cocos\editors\Creator\3.8.2\resources\app.asar\node_modules@editor\lib-programming\static\executor\systemjs-bridge\out\index.js:270:10)
at doExec (C:\ProgramData\cocos\editors\Creator\3.8.2\resources\app.asar\node_modules@editor\lib-programming\static\executor\systemjs-bridge\out\index.js:538:7)
at postOrderExec (C:\ProgramData\cocos\editors\Creator\3.8.2\resources\app.asar\node_modules@editor\lib-programming\static\executor\systemjs-bridge\out\index.js:508:21)