您当前的位置: 首页 >  Java
  • 5浏览

    0关注

    157博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Unity WebGL UnityScript与JavaScript通信

牙膏上的小苏打2333 发布时间:2021-01-26 11:52:47 ,浏览量:5

ʕ๑•ɷ•๑ʔ🌼
  • 原理
  • 工具导入
  • UnityScript(WebGL) ⇐⇒ JavaScript(Web)
    • 一.UnityScript(WebGL)
      • 1. 在场景中添加脚本并初始化
      • 2. 发送消息给Web
      • 3.接收Web消息
    • 二.JavaScript(Web)
      • 1.添加消息监听
      • 2.发送消息给WebGL
在这里插入图片描述

原理

UnityScript(WebGL)与JavaScript(Web)交互

#mermaid-svg-e9DdoePMgSm23B6U .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-e9DdoePMgSm23B6U .label text{fill:#333}#mermaid-svg-e9DdoePMgSm23B6U .node rect,#mermaid-svg-e9DdoePMgSm23B6U .node circle,#mermaid-svg-e9DdoePMgSm23B6U .node ellipse,#mermaid-svg-e9DdoePMgSm23B6U .node polygon,#mermaid-svg-e9DdoePMgSm23B6U .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-e9DdoePMgSm23B6U .node .label{text-align:center;fill:#333}#mermaid-svg-e9DdoePMgSm23B6U .node.clickable{cursor:pointer}#mermaid-svg-e9DdoePMgSm23B6U .arrowheadPath{fill:#333}#mermaid-svg-e9DdoePMgSm23B6U .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-e9DdoePMgSm23B6U .flowchart-link{stroke:#333;fill:none}#mermaid-svg-e9DdoePMgSm23B6U .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-e9DdoePMgSm23B6U .edgeLabel rect{opacity:0.9}#mermaid-svg-e9DdoePMgSm23B6U .edgeLabel span{color:#333}#mermaid-svg-e9DdoePMgSm23B6U .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-e9DdoePMgSm23B6U .cluster text{fill:#333}#mermaid-svg-e9DdoePMgSm23B6U div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-e9DdoePMgSm23B6U .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-e9DdoePMgSm23B6U text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-e9DdoePMgSm23B6U .actor-line{stroke:grey}#mermaid-svg-e9DdoePMgSm23B6U .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-e9DdoePMgSm23B6U .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-e9DdoePMgSm23B6U #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-e9DdoePMgSm23B6U .sequenceNumber{fill:#fff}#mermaid-svg-e9DdoePMgSm23B6U #sequencenumber{fill:#333}#mermaid-svg-e9DdoePMgSm23B6U #crosshead path{fill:#333;stroke:#333}#mermaid-svg-e9DdoePMgSm23B6U .messageText{fill:#333;stroke:#333}#mermaid-svg-e9DdoePMgSm23B6U .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-e9DdoePMgSm23B6U .labelText,#mermaid-svg-e9DdoePMgSm23B6U .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-e9DdoePMgSm23B6U .loopText,#mermaid-svg-e9DdoePMgSm23B6U .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-e9DdoePMgSm23B6U .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-e9DdoePMgSm23B6U .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-e9DdoePMgSm23B6U .noteText,#mermaid-svg-e9DdoePMgSm23B6U .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-e9DdoePMgSm23B6U .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-e9DdoePMgSm23B6U .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-e9DdoePMgSm23B6U .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-e9DdoePMgSm23B6U .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-e9DdoePMgSm23B6U .section{stroke:none;opacity:0.2}#mermaid-svg-e9DdoePMgSm23B6U .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-e9DdoePMgSm23B6U .section2{fill:#fff400}#mermaid-svg-e9DdoePMgSm23B6U .section1,#mermaid-svg-e9DdoePMgSm23B6U .section3{fill:#fff;opacity:0.2}#mermaid-svg-e9DdoePMgSm23B6U .sectionTitle0{fill:#333}#mermaid-svg-e9DdoePMgSm23B6U .sectionTitle1{fill:#333}#mermaid-svg-e9DdoePMgSm23B6U .sectionTitle2{fill:#333}#mermaid-svg-e9DdoePMgSm23B6U .sectionTitle3{fill:#333}#mermaid-svg-e9DdoePMgSm23B6U .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-e9DdoePMgSm23B6U .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-e9DdoePMgSm23B6U .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-e9DdoePMgSm23B6U .grid path{stroke-width:0}#mermaid-svg-e9DdoePMgSm23B6U .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-e9DdoePMgSm23B6U .task{stroke-width:2}#mermaid-svg-e9DdoePMgSm23B6U .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-e9DdoePMgSm23B6U .taskText:not([font-size]){font-size:11px}#mermaid-svg-e9DdoePMgSm23B6U .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-e9DdoePMgSm23B6U .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-e9DdoePMgSm23B6U .task.clickable{cursor:pointer}#mermaid-svg-e9DdoePMgSm23B6U .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-e9DdoePMgSm23B6U .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-e9DdoePMgSm23B6U .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-e9DdoePMgSm23B6U .taskText0,#mermaid-svg-e9DdoePMgSm23B6U .taskText1,#mermaid-svg-e9DdoePMgSm23B6U .taskText2,#mermaid-svg-e9DdoePMgSm23B6U .taskText3{fill:#fff}#mermaid-svg-e9DdoePMgSm23B6U .task0,#mermaid-svg-e9DdoePMgSm23B6U .task1,#mermaid-svg-e9DdoePMgSm23B6U .task2,#mermaid-svg-e9DdoePMgSm23B6U .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-e9DdoePMgSm23B6U .taskTextOutside0,#mermaid-svg-e9DdoePMgSm23B6U .taskTextOutside2{fill:#000}#mermaid-svg-e9DdoePMgSm23B6U .taskTextOutside1,#mermaid-svg-e9DdoePMgSm23B6U .taskTextOutside3{fill:#000}#mermaid-svg-e9DdoePMgSm23B6U .active0,#mermaid-svg-e9DdoePMgSm23B6U .active1,#mermaid-svg-e9DdoePMgSm23B6U .active2,#mermaid-svg-e9DdoePMgSm23B6U .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-e9DdoePMgSm23B6U .activeText0,#mermaid-svg-e9DdoePMgSm23B6U .activeText1,#mermaid-svg-e9DdoePMgSm23B6U .activeText2,#mermaid-svg-e9DdoePMgSm23B6U .activeText3{fill:#000 !important}#mermaid-svg-e9DdoePMgSm23B6U .done0,#mermaid-svg-e9DdoePMgSm23B6U .done1,#mermaid-svg-e9DdoePMgSm23B6U .done2,#mermaid-svg-e9DdoePMgSm23B6U .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-e9DdoePMgSm23B6U .doneText0,#mermaid-svg-e9DdoePMgSm23B6U .doneText1,#mermaid-svg-e9DdoePMgSm23B6U .doneText2,#mermaid-svg-e9DdoePMgSm23B6U .doneText3{fill:#000 !important}#mermaid-svg-e9DdoePMgSm23B6U .crit0,#mermaid-svg-e9DdoePMgSm23B6U .crit1,#mermaid-svg-e9DdoePMgSm23B6U .crit2,#mermaid-svg-e9DdoePMgSm23B6U .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-e9DdoePMgSm23B6U .activeCrit0,#mermaid-svg-e9DdoePMgSm23B6U .activeCrit1,#mermaid-svg-e9DdoePMgSm23B6U .activeCrit2,#mermaid-svg-e9DdoePMgSm23B6U .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-e9DdoePMgSm23B6U .doneCrit0,#mermaid-svg-e9DdoePMgSm23B6U .doneCrit1,#mermaid-svg-e9DdoePMgSm23B6U .doneCrit2,#mermaid-svg-e9DdoePMgSm23B6U .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-e9DdoePMgSm23B6U .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-e9DdoePMgSm23B6U .milestoneText{font-style:italic}#mermaid-svg-e9DdoePMgSm23B6U .doneCritText0,#mermaid-svg-e9DdoePMgSm23B6U .doneCritText1,#mermaid-svg-e9DdoePMgSm23B6U .doneCritText2,#mermaid-svg-e9DdoePMgSm23B6U .doneCritText3{fill:#000 !important}#mermaid-svg-e9DdoePMgSm23B6U .activeCritText0,#mermaid-svg-e9DdoePMgSm23B6U .activeCritText1,#mermaid-svg-e9DdoePMgSm23B6U .activeCritText2,#mermaid-svg-e9DdoePMgSm23B6U .activeCritText3{fill:#000 !important}#mermaid-svg-e9DdoePMgSm23B6U .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-e9DdoePMgSm23B6U g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-e9DdoePMgSm23B6U g.classGroup text .title{font-weight:bolder}#mermaid-svg-e9DdoePMgSm23B6U g.clickable{cursor:pointer}#mermaid-svg-e9DdoePMgSm23B6U g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-e9DdoePMgSm23B6U g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-e9DdoePMgSm23B6U .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-e9DdoePMgSm23B6U .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-e9DdoePMgSm23B6U .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-e9DdoePMgSm23B6U .dashed-line{stroke-dasharray:3}#mermaid-svg-e9DdoePMgSm23B6U #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-e9DdoePMgSm23B6U #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-e9DdoePMgSm23B6U #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-e9DdoePMgSm23B6U #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-e9DdoePMgSm23B6U #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-e9DdoePMgSm23B6U #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-e9DdoePMgSm23B6U #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-e9DdoePMgSm23B6U #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-e9DdoePMgSm23B6U .commit-id,#mermaid-svg-e9DdoePMgSm23B6U .commit-msg,#mermaid-svg-e9DdoePMgSm23B6U .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-e9DdoePMgSm23B6U .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-e9DdoePMgSm23B6U .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-e9DdoePMgSm23B6U g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-e9DdoePMgSm23B6U g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-e9DdoePMgSm23B6U g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-e9DdoePMgSm23B6U g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-e9DdoePMgSm23B6U g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-e9DdoePMgSm23B6U g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-e9DdoePMgSm23B6U .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-e9DdoePMgSm23B6U .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-e9DdoePMgSm23B6U .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-e9DdoePMgSm23B6U .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-e9DdoePMgSm23B6U .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-e9DdoePMgSm23B6U .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-e9DdoePMgSm23B6U .edgeLabel text{fill:#333}#mermaid-svg-e9DdoePMgSm23B6U .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-e9DdoePMgSm23B6U .node circle.state-start{fill:black;stroke:black}#mermaid-svg-e9DdoePMgSm23B6U .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-e9DdoePMgSm23B6U #statediagram-barbEnd{fill:#9370db}#mermaid-svg-e9DdoePMgSm23B6U .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-e9DdoePMgSm23B6U .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-e9DdoePMgSm23B6U .statediagram-state .divider{stroke:#9370db}#mermaid-svg-e9DdoePMgSm23B6U .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-e9DdoePMgSm23B6U .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-e9DdoePMgSm23B6U .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-e9DdoePMgSm23B6U .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-e9DdoePMgSm23B6U .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-e9DdoePMgSm23B6U .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-e9DdoePMgSm23B6U .note-edge{stroke-dasharray:5}#mermaid-svg-e9DdoePMgSm23B6U .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-e9DdoePMgSm23B6U .error-icon{fill:#522}#mermaid-svg-e9DdoePMgSm23B6U .error-text{fill:#522;stroke:#522}#mermaid-svg-e9DdoePMgSm23B6U .edge-thickness-normal{stroke-width:2px}#mermaid-svg-e9DdoePMgSm23B6U .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-e9DdoePMgSm23B6U .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-e9DdoePMgSm23B6U .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-e9DdoePMgSm23B6U .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-e9DdoePMgSm23B6U .marker{fill:#333}#mermaid-svg-e9DdoePMgSm23B6U .marker.cross{stroke:#333} :root { --mermaid-font-family: "trebuchet ms", verdana, arial;} #mermaid-svg-e9DdoePMgSm23B6U { color: rgba(0, 0, 0, 0.75); font: ; }
Web
WebGL
发送消息Send
接收消息onReceivedMsg
发送消息SendMsg2WebGL
接收消息AddRecieveWebGLMsgListener
index.html
WebGLCommunicator.js
ZYF_WebCommunicator.cs
WebCommunicator.jslib
...
工具导入
  1. 下载WebCommunicator工具包👈,导入到Unity中。(另外还需要Odin插件🤫) 在这里插入图片描述

  2. 双击导入WebGLCommunicator(JsTool) 在这里插入图片描述 再把WebGLCommunicator文件夹拖到自己的模板中 在这里插入图片描述

UnityScript(WebGL) ⇐⇒ JavaScript(Web) 一.UnityScript(WebGL) 1. 在场景中添加脚本并初始化

在这里插入图片描述

2. 发送消息给Web

在这里插入图片描述

3.接收Web消息

在这里插入图片描述

二.JavaScript(Web) 1.添加消息监听

打开模板中的index.html,导入工具并监听WebGL消息 在这里插入图片描述





   AddRecieveWebGLMsgListener(function(msg) {
      console.log('处理 WebGL消息。。。:', msg);
   });

2.发送消息给WebGL

在这里插入图片描述


   从Web发送消息给WebGL

关注
打赏
1664520285
查看更多评论
立即登录/注册

微信扫码登录

0.0506s