ʕ๑•ɷ•๑ʔ🌼
原理
UnityScript(WebGL)与JavaScript(Web)交互
工具导入
-
下载WebCommunicator工具包👈,导入到Unity中。(另外还需要Odin插件🤫)

-
双击导入
WebGLCommunicator(JsTool)

再把WebGLCommunicator文件夹拖到自己的模板中

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

2. 发送消息给Web

3.接收Web消息

二.JavaScript(Web)
1.添加消息监听
打开模板中的index.html,导入工具并监听WebGL消息

<!-- webGL与web互相通信 -->
<script type="text/javascript" src="WebGLCommunicator/WebGLCommunicator.js"></script>
<!-- 监听WebGL消息 -->
<script type="text/javascript">
AddRecieveWebGLMsgListener(function(msg) {
console.log('处理 WebGL消息。。。:', msg);
});
</script>
2.发送消息给WebGL

<div style="position:absolute;width: 200px; height: 100px;z-index:2;">
<button type="button" onclick="SendMsg2WebGL('Hi i am web')">从Web发送消息给WebGL</button>
</div>
错误处理
gameInstance is not defined

1.先确定打包后index.html中Unity示例名称

2. 修改Unity编辑器中的WebGLCommunicator.js


改成对应的unityInstance,重新打包,清除浏览器缓存运行

本文介绍如何使用WebCommunicator工具包实现Unity WebGL项目与Web页面之间的消息传递,包括UnityScript与JavaScript交互的具体步骤及错误处理方法。
1066

被折叠的 条评论
为什么被折叠?



