【无标题】cocos与外部laya或者web交互

一.电脑:

电脑网页:cocos --->laya

this.webview._impl._iframe.contentWindow.postMessage("cocosToLaya", "*");//laya

//这里是浏览器环境下, 接收web传过来的消息//cocos

window.addEventListener("message", function(event){

console.log( '这里是接收到的消息,消息内容在event.data属性中1111', event )

console.log(event.data);

}, false)

电脑网页:laya --->cocos

if (window.isNative) {//laya

//android or ios

document.location = 'testkey://a=1&b=2'

} else {

//browser 浏览器下,向cocos发送消息

parent.postMessage("------------hello!-----cocos---------", "*")

}

if (cc.sys.isBrowser) {//cocos

//这里是浏览器环境下, 接收web传过来的消息

window.addEventListener('message', function (e) {

console.log("----cocos---", e.data);

test();

})

}

二.手机

手机安卓:cocos --->laya

this.webview.evaluateJS('window.test()');//cocos

let data = {id:123456}

data = JSON.stringify(data); //注意这里需要把参数序列化

this.webView.evaluateJS("setBackgroundColor(" + data + ")");

function test() { //laya

console.log("-------laya--------data-------" + data)

}

window.test = test;

手机安卓:laya --->cocos

if (window.isNative) { //laya

//android or ios

document.location = 'testkey://a=1&b=2'

} else {

//browser 浏览器下,向cocos发送消息

parent.postMessage("------------hello!-----cocos---------", "*")

}

window.addEventListener("message", function(event){ //laya,注意:这个会收到上面的 parent.postMessage("------------hello!-----cocos---------", "*")消息

console.log( '这里是接收到的消息,消息内容在event.data属性中1111', event )

console.log(event.data);

}, false)

//laya

document.location = 'testkey://a=1&b=2';

传递参数

var id=111;

var name="ganme"

document.location = 'testkey://webview?id='+id+'&name='+name;

//cocos

onLoad() {

// 这里是与内部页面约定的关键字,请不要使用大写字符,会导致 location 无法正确识别。

let scheme = "testkey";

function jsCallback (target, url) {

// 这里的返回值是内部页面的 URL 数值,需要自行解析自己需要的数据。

let str = url.replace(scheme + '://', ''); // str === 'a=1&b=2'

console.log("------1testkeytestkey-1----target:"+JSON.stringify(target));//{"__nativeRefs":{},"src":"http://tan-xin-game.mytingyou.com/client/Disucz/test/bin/index.html?sdk=3\&ip=47.92.24.73:12347\&debug=true"}

console.log("------1testkeytestkey-2----str:"+str);//a=1&b=2

console.log("------1testkeytestkey-3----url:"+url);//testkey://a=1&b=2

}

this.webview.setJavascriptInterfaceScheme(scheme);

this.webview.setOnJSCallback(jsCallback);

commonUtils.initALLRES();

},

相关推荐
麦麦麦当劳大王6 小时前
【油猴脚本】Boss直聘 岗位职责复制
开发语言·前端·javascript
只睡四小时7 小时前
Three.js Sub-tick:漏判 60.7%→0
开发语言·javascript·性能优化·ecmascript·游戏开发·three.js·碰撞检测
excel8 小时前
基于 GPT 的自动发布文章接口方案:从一句话或一张图片生成并发布内容
前端·javascript·后端
OpsEye14 小时前
大模型 Token 计量不准,是什么原因,怎么解决??
javascript·ai编程
独隅16 小时前
WebStorm Windows 版 React / Next.js 实战替换指南
javascript·windows·webstorm
冷眸_16 小时前
同名食材不能直接相减:一个 JavaScript 用量模型的边界
javascript
apollo_qwe16 小时前
接口超时了,前端为什么不能直接重试?
javascript·http
toooooop821 小时前
CRMEB 故障排查笔记:安全网关只放行 GET/POST 怎么办
前端·javascript·vue.js·thinkphp6·crmeb
软件技术NINI21 小时前
基于SpringBoot和Vue的项目评审系统
javascript·vue.js·spring boot·后端·spring·毕业设计·毕设
vipxieliang21 小时前
JavaScript 模板字符串与标签模板:不只是拼接
前端·javascript