当前位置: 首页 > news >正文

Unity WebGL交互通信

Unity 调用 H5

本文使用的 unity 版本为:2021.3.3
1.在unity中通过c#的特性DllImport导出外部实现函数

    [DllImport("__Internal")]private static extern void callJsString(string param);[DllImport("__Internal")]private static extern void callJsInt(int param);[DllImport("__Internal")]private static extern void callJsFloat(float param);[DllImport("__Internal")]private static extern void callJsBoolean(bool param);

2.在unity的Plugins文件夹(没有则创建一个)里面创建一个后缀为.jslib的文件
3.在xxx.jslib文件中实现函数,如下

mergeInto(LibraryManager.library, {callJsString: function (param) {console.log(Pointer_stringify(param));},callJsInt: function (param) {console.log(param);},callJsFloat: function (param) {console.log(param);},callJsBoolean: function (param) {console.log(param);},
});

注意: 使用传参字符串的时候,使用Pointer_stringify转下字符串,否则可能会报错或者乱码
4.如果想要调用.js里面的接口或者.html里面的方法,可以通过往window里面注册函数进行调用的方式
例如:在index.html里面注册函数 window.httpPost = httpPost;

<script>function httpPost(url, params) {var xhr = new XMLHttpRequest();xhr.onreadystatechange = () => {if (xhr.readyState === 4 && (xhr.status >= 200 && xhr.status < 300)) {let data = xhr.response;console.log("Status: " + xhr.status + " " + xhr.statusText + "  data: " +         data);success(data);}else if (xhr.status >= 400) {console.log("Status: " + xhr.status + " " + xhr.statusText);fail(xhr.status, xhr.statusText)}}console.log("Status: Send Post Request to " + url);xhr.open("POST", url, true);//xhr.setRequestHeader('Access-Control-Allow-Origin', '*');xhr.setRequestHeader("Content-Type", "application/json");xhr.timeout = 10000;xhr.send(params);}window.httpPost = httpPost;
</script>

5.可以在xxx.jslib文件中调用index.html里面的httpPost函数,当时也可以直接把函数实现直接放在xxx.jslib文件中

mergeInto(LibraryManager.library, {callJsString: function (url,params) {window.httpPost(Pointer_stringify(url),Pointer_stringify(params)); },
});

6.上面的方式可以让我们在案例httpPost调用其他位置的.js代码或者其他位置的插件

H5调用 Unity

1.这里的方式主要是在index.html文件中在创建unityInstance的时候通过缓存unityInstance并向unityInstance发送消息的方式实现
2.在index.html中创建unityInstance的时候缓存unityInstance
这里通过:window.unityInstance = unityInstance; 缓存了unityInstance实例

         var script = document.createElement("script");script.src = loaderUrl;script.onload = () => {createUnityInstance(canvas, config, (progress) => {progressBarFull.style.width = 100 * progress + "%";}).then((unityInstance) => {loadingBar.style.display = "none";fullscreenButton.onclick = () => {unityInstance.SetFullscreen(1);};window.unityInstance = unityInstance;}).catch((message) => {alert(message);});};

3.向unity发送消息
gameobjectName:接收消息的gameobject名
funcName:方法名
param:参数,字符串,可以传json

 function sendUnityMessage(gameobjectName,funcName,param) {if(window.unityInstance){window.unityInstance.SendMessage(gameobjectName,funcName,param);}}
http://www.lryc.cn/news/480040.html

相关文章:

  • 王道考研之数据结构
  • 实习冲刺Day17
  • 我自己nodejs练手时常用的一些库基础用法
  • 岛屿数量问题
  • 智能制造基础- TPM(全面生产维护)
  • C++学习笔记----11、模块、头文件及各种主题(一)---- 模板概览与类模板(4)
  • 【力扣热题100】[Java版] 刷题笔记-160. 相交链表
  • 多线程和线程同步复习
  • 贝式计算的 AI4S 观察:使用机器学习对世界进行感知与推演,最大魅力在于横向扩展的有效性
  • 容器化技术入门:Docker详解
  • 基于SSM(Spring + Spring MVC + MyBatis)框架的药房管理系统
  • 在服务器里安装2个conda
  • web安全漏洞之ssrf入门
  • 《NoSQL 基础知识总结》
  • 高校宿舍信息管理系统小程序
  • 2.索引:MySQL 索引分类
  • sklearn红酒数据集分类器的构建和评估
  • 【IC验证面试常问-4】
  • 【数据集】【YOLO】【目标检测】交通事故识别数据集 8939 张,YOLO道路事故目标检测实战训练教程!
  • 书生浦语第四期基础岛L1G4000-InternLM + LlamaIndex RAG 实践
  • 基于ViT的无监督工业异常检测模型汇总
  • 数据库管理-第258期 23ai:Oracle Data Redaction(20241104)
  • 运放进阶篇-多种波形可调信号发生器-产生方波-三角波-正弦波
  • CSS中的变量应用——:root,Sass变量,JavaScript中使用Sass变量
  • WPF+MVVM案例实战与特效(二十八)- 自定义WPF ComboBox样式:打造个性化下拉菜单
  • 速盾:怎么使用cdn加速?
  • C++ 优先算法 —— 三数之和(双指针)
  • YOLOv7-0.1部分代码阅读笔记-yolo.py
  • 【缓存与加速技术实践】Web缓存代理与CDN内容分发网络
  • MySQL的约束和三大范式