Unity-to-WeChat mini-game interacts with JS

WeChat Mini Game JS Interaction Mode:

There is a problem with the WeChat mini-game mechanism. Index.html will not be loaded in the WeChat mini-game, so the functions written in index.html will not be called

1. calljs.jsAdd definitions to the self-created file, and the file will be overwritten every time it is compiled

export const CallJS={
  test:function(){
    console.log("test");
  }
}

2. game.jsIntroduced in

import './weapp-adapter'
import unityNamespace from './unity-namespace'
import './webgl.wasm.framework.unityweb'
import "./unity-sdk/index.js"
import checkVersion, {canUseCoverview} from './check-version'
import "texture-config.js";
import {launchEventType} from './plugin-config'
import {CallJS} from './calljs' // 引入自定义JS调用代码

Mounted GameGlobalon the object, under line 32

GameGlobal.managerConfig = managerConfig;
GameGlobal.CallJS = CallJS; // 挂载

3. Call case, the mini game side does not need to be implemented, it is defined in the glue layer of Unity

Unity's custom glue layer code will be compiled webgl.wasm.framework.unityweb.jsinto

insert image description here

_BuyClick the button to call the function through the glue layer in Unity

insert image description here

JS interaction method of WebGL:

1. Use the same import method as in the WeChat mini-game, define GameGlobaland mount CallJSthe object yourself, and keep the same definition method as in the mini-game. It is called uniformly in the glue layer, and no distinction is made.

insert image description here
insert image description here

Calljs update method suggestion:

It is recommended to use calljs as a template in unity, without modifying index.html and game.js every time. Just replace the calljs.js file.
WebGL template modification WXTemplateor WXTemplate2020
mini game template modificationWX-WASM-SDK\wechat-default

Guess you like

Origin blog.csdn.net/qq_17515481/article/details/125968278
Recommended