Tutorial
- When building a WeChat mini-game,
原生代码打包模式
chooseasmJS
not to choose wasm or wasm+asmJS - Download the plugin provided by Bi Station
- Pack
Okay, all the processes are completed, and the work is over~
However, you who love to think will ask, if I can’t choose wasm, won’t it be more stuck when I do more physics?
Yes, according to the documentation, there is indeed no way, but after further communication with the official (Battle), I found that the Android version of the B station mini game actually supports wasm. If it supports Android, then let’s go to Android first. Next, we will slightly modify it to make Android support wasm and iOS will not be affected
Support wasm
Reference article: How to handle RunoseAI 3.8 when it does not support the wasm platform but relies on WeChat mini-games , Here too, we need to modify Cocos’s wasm judgment of the WeChat mini-game platform and continue to modify this fileresources/3d/engine/pal/system-info/minigame/system-info.ts
// 添加一个判断条件
if (WECHAT && typeof WXWebAssembly === 'object') {
return true;
}
After the modification is completed, follow the original steps and select the native code packaging mode as wasm+asmJS (the name is both in 3.8.5), so that the package that supports B station mini-games is ready.
But someone (yes, it was me) discovered that there is no order in which plugins are executed, causing plugin incompatibility (each of them modified the same file). What should I do?
With a little thought, you can find that the easiest way is to combine the two plug-ins into one, so let’s do it.
Transformation plugin
After testing (in fact, I directly looked at the source code of the B station plug-in, the conscientious plug-in, the source code is also provided~), I found that the adaptation of the B station plug-in actually did three things:
- Add adaptation file
- Modify the reference of the web-adapter file
- Join Startup Report
So we just need to implement these three things in our own plug-in, for example
- Modify the game.js of the main package
const dealBilibili = async function (dest: string) {
// 修改 game.js
const gameJsPath = join(dest, "game.js");
let gameJsContent = readFileSync(gameJsPath, "utf-8");
// 注释web-adapter
const start = gameJsContent.indexOf("require('./web-adapter");
if (start !== -1) {
// 向前找到前一个分号
let prevSemicolon = gameJsContent.lastIndexOf(';', start) + 1;
// 向后找到后一个分号
let nextSemicolon = gameJsContent.indexOf(';', start) + 1;
if (prevSemicolon !== -1 && nextSemicolon !== -1) {
// 删除这个区间的内容
gameJsContent = gameJsContent.slice(0, prevSemicolon) + gameJsContent.slice(nextSemicolon);
}
}
const jsPath = join(__dirname, '..', 'static', 'adapters', 'biligame', 'js');
// 添加两个文件,原插件有更新机制,请注意
copySync(join(jsPath, 'blapp-adapter-wasm-for-cocos-v3.js'), join(dest, 'blapp-adapter-wasm-for-cocos-v3.js'));
copySync(join(jsPath, 'blapp-adapter-downgrade-for-cocos-v3.6-and-above.js'), join(dest, 'blapp-adapter-downgrade-for-cocos-v3.6-and-above.js'));
gameJsContent = `
require('./blapp-adapter-wasm-for-cocos-v3.js');
require('./blapp-adapter-downgrade-for-cocos-v3.6-and-above.js');
` + gameJsContent;
writeFileSync(gameJsPath, gameJsContent, 'utf-8');
const files = readdirSync(dest);
for (let i = 0; i < files.length; i++) {
const file = files[i];
if (file.startsWith('web-adapter.') && file.endsWith('.js')) {
if ('web-adapter.js' == file) {
break;
}
// 如果加了md5,文件名就要修改为web-adapter.js
renameSync(join(dest, file), join(dest, 'web-adapter.js'));
break;
}
}
}
- Because I redefined game.js, I need to add startup judgment to the real game.js
if ('biligame' === platform) {
// 下方添加launchSuccess();
// 修改 game.js
const gameJsPath = join(dest, "game.js");
let gameJsContent = readFileSync(gameJsPath, "utf-8");
gameJsContent += `
if(bl){bl.launchSuccess()};`;
writeFileSync(gameJsPath, gameJsContent, 'utf-8');
}
Well, at this point, this article has been successfully completed.