• 【无标题】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();
          },

  • 相关阅读:
    LC1798. 你能构造出连续值的最大数目(JAVA)
    手撕红黑树
    http之basic authentication浅析
    Linux系统LVM操作
    说说你这个项目的架构情况吧?
    希尔排序算法(思路分析) [数据结构][Java]
    Python pandas.isna实例讲解
    为了 Vue 组件测试,你需要为每个事件绑定的方法加上括号吗?
    CAN总线协议的理解以及移植stm32代码并使用
    Spring MVC文件上传
  • 原文地址:https://blog.csdn.net/alengan/article/details/137961716