Hi Cocos Team。
I need help with the following.
I found a bug-like behavior in the behavior of an editor extension.
The editor extension that I created exposes the message acceptance and echoes the received value.
The operation check showed that 3 did not work.
- check if the editor extension works from CocosCreator’s Editor.
- Build from the build panel and call the editor extension in the hook of the build template.
- build from the command line and call the editor extension with the hook in the build template.
When I build from the build panel, the echo contents are displayed in the Console.
However, when building from the command line, the echo contents are not displayed on the terminal.
In the case of building from the command line, I was able to confirm that everything from the build template to the editor extension browser.js was working properly.
However, when calling scene.js from browser.js from browser.js does not work properly and is not echoed.
I think that building from the command line will start CocosCreator headless, so the script for scene will not work properly.
The following are the commands for building.
/Applications/CocosCreator/Creator/3.3.1/CocosCreator.app/Contents/MacOS/CocosCreator --project [project path] --build "configPath=[build-config.json];"
・Specify the path to the project in [project path].
・Specify the settings exported from the build panel in [build-config.json].
The following are the details of the editor extension used for testing and the build template.
The editor extension was created using the following procedure.
・Go to [Extension] > [Create Extension] and create a template for an editor extension that belongs to ‘project’.
・Add scene.js.
・Modify package.json to embed scene.js and set a public message.
The following is the configuration of the contributions section in package.json.
"contributions": {
"menu": [
{
"path": "Test",
"label": "Echo test",
"message": "open"
}
],
"messages": {
"open": {
"methods": [
"fromMenu"
]
},
"fromMessage": {
"public": true,
"description": "Echo test.",
"doc": "",
"methods": [
"fromMessage"
]
}
},
"scene": {
"script": "./scene.js"
}
}
The following is the contents of browser.js.
const packageJSON = require('./package.json');
exports.methods={
async fromMenu() {
const result = await Editor.Message.request('scene', 'execute-scene-script', {
name: packageJSON.name,
method: `echo`,
args: [`fromMenu`,]
});
console.log(`******** Echo : ${result} ********`);
},
async fromMessage(value) {
const result = await Editor.Message.request('scene', 'execute-scene-script', {
name: packageJSON.name,
method: `echo`,
args: [value,]
});
return result;
},
};
The following is the content of scene.js.
'use strict';
exports.methods = {
async echo(arg) {
const sleep = new Promise(resolve => {
const timeoutID = setTimeout(() => {
clearTimeout(timeoutID);
resolve(arg);
}, 1000);
});
const result = await sleep;
return result;
},
};
Details of Operation
- browser.js receives a message and passes the value to scene.js.
- scene.js echoes the message and returns the value to browser.js.
- browser.js returns the value returned from scene.js as the return value of the message.
The builder plate was created by the following procedure.
・Add it by [Project] > [Generate Builder Extention].
・Rewrite onBeforeBuild in hooks.ts.
Rewrite onBeforeBuild in hooks.ts as follows.
export async function onBeforeBuild(options: ITaskOptions) {
const result = await Editor.Message.request('simple-1638839972442', 'fromMessage', [`fromMessage`]);
console.log(`******** Echo : ${result} ********`);
}
Finally, we need a Scene to build, so add an empty scene under assets.