Unity webgl 嵌入Vue实现过程

需求分析:

Unity webgl嵌入到前端网页中,前端通过调用Unity webgl内方法实现需要展示的功能,前端点击Unity webgl内的交互点,Unity webgl返回给前端一些需要的数据。

例如:当我们需要在三维场景中展示库区中一些监控设备的部署位置,通过点击三维场景中的监控按钮打开当前监控设备的实时画面,一般情况下打开监控需要传递当前监控的IP或者通道号,这时Unity webgl向前端返回数据就用到了。

实现过程:

1、Unity webgl向Vue发送数据

首先,Unity webgl向前端发送数据需要定义一个.jslib格式文件作为转接,文件名自取(建议不要用中文)文件内容如下:

mergeInto(LibraryManager.library, {

 UnitySendMessage: function (eventname, data)
{
    window.ReportReady(UTF8ToString(eventname), UTF8ToString(data));
 } //如果多个方法需要使用逗号结尾(在此大括号后加逗号),只有一个方法不需要使用逗号


});

到此,转接文件已经定义好了

接着在Unity脚本中添加 using System.Runtime.InteropServices; 引用(用于COM互操作),

代码如下:

using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class Test: MonoBehaviour
{
    
    [DllImport("__Internal")]
    private static extern void UnitySendMessage(string eventname, string data);//方法名及参数和转接的.jslib文件中的一样

    private Button button;

    
    private void Awake()
    {
        button.onClick.AddListener(SendMessage);
    }

    private void SendMessage()
    {
       UnitySendMessage("getbuttonname",GetButtonName());//事件名自己命名即可,前端在监听时使用
    }

  
   private string GetButtonName()
    {
        string name  = EventSystem.current.currentSelectedGameObject.name;
        return name;
    }


}

打包后打开index.html文件加入此段代码:

  window.ReportReady = function (eventname, data) {
            var initD = { detail: { data } }
            var evt = new CustomEvent(eventname, initD)
            window.top.dispatchEvent(evt)
        }

如图:

最后在Vue mounted中加入即可

window.addEventListener('getbuttonname', this.uinityEvent, false)//getbuttonname对应Unity内定义的事件名

2、Vue向Unity发送数据

首先,Vue调用Unity的方法就比较简单了,在Unity内定义带参数的方法如:

using UnityEngine;

public class Test: MonoBehaviour
{
    
   private void GetVueData(string value)
    {
        Debug.Log(value);
    }

}

接着打开打包后的index.html文件,在里面加入供前端调用的方法:

  function GetVueMessage(obj) {
            UnityInstanceV.SendMessage('MainSenceScript', 'GetVueData', JSON.stringify(obj))
//对应的参数分别为:"Unity场景内挂载脚本的物体名字","方法名",最后一个参数复制粘贴即可
        }

最后只需Vue调用此方法并传递参数就可以了,如果这篇文章帮助到你,就点个赞吧!

  • 43
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity WebGLVue 可以通过以下方式进行通信: 1. 使用 postMessage() 方法 在 Unity WebGL 中,可以使用 Unity 的 SendMessage() 方法将消息发送到 Vue 应用程序中。在 Vue 应用程序中,可以使用 window.addEventListener() 监听消息事件,然后根据消息内容进行相应的操作。 Unity 中的代码示例: ``` function sendMessageToVue(message) { var iframe = window.parent.document.getElementById('your-iframe-id'); iframe.contentWindow.postMessage(message, '*'); } ``` 在 Vue 应用程序中的代码示例: ``` mounted() { window.addEventListener('message', this.handleUnityMessage); }, methods: { handleUnityMessage(event) { if (event.origin !== 'https://your-unity-webgl-domain.com') { return; } const message = event.data; // 根据消息内容进行相应的操作 } } ``` 2. 使用 Vue.js Unity Web Player 插件 Vue.js Unity Web Player 插件提供了一种更方便的方式来在 Vue 应用程序中嵌入 Unity WebGL 游戏,并与之进行通信。该插件可以在 Vue 应用程序中直接使用 Unity API。 首先,需要安装 vue-unity-webgl 插件: ``` npm install vue-unity-webgl --save ``` 然后,在 Vue 应用程序中使用该插件: ``` <template> <div> <unity-webgl :game-width="800" :game-height="600" src="https://your-unity-webgl-domain.com/your-game.json" @unityMessage="handleUnityMessage"></unity-webgl> </div> </template> <script> import UnityWebgl from 'vue-unity-webgl'; export default { components: { UnityWebgl, }, methods: { handleUnityMessage(message) { // 根据消息内容进行相应的操作 }, }, }; </script> ``` 在 Unity 中,需要使用 Unity 的 SendMessage() 方法来发送消息到 Vue 应用程序中: ``` function sendMessageToVue(message) { unityInstance.SendMessage('VueProxy', 'ReceiveMessage', message); } ``` 在 Vue 应用程序中,需要在组件中定义一个名为 ReceiveMessage 的方法来接收 Unity 发送的消息。插件会自动将接收到的消息作为参数传入该方法中: ``` <template> <div></div> </template> <script> export default { methods: { ReceiveMessage(message) { // 根据消息内容进行相应的操作 }, }, }; </script> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值