vue3引入并加载unity工程的两种方式

1、使用unity-webgl插件

javascript 复制代码
npm i unity-webgl

unity打包后的build文件夹是这样的

需要手动删除.unityweb这个后缀,完成后放在vue3项目的根目录下的public文件夹下。

下面是引入unity的vue组件,其中实例化UnityWebgl时的参数地址直接引用上面的对应文件地址

javascript 复制代码
<script setup>
import UnityWebgl from 'unity-webgl';
import UnityVue from 'unity-webgl/vue';

const unityContext = new UnityWebgl({
  loaderUrl: '/unity/Build/test.loader.js',
  dataUrl: '/unity/Build/test.data',
  frameworkUrl: '/unity/Build/test.framework.js',
  codeUrl: '/unity/Build/test.wasm',
  companyName: 'test',
  productName: 'test',
});
</script>

<template>
  <div style="width: 1900px; height: 900px; border: 1px solid #f00">
    <UnityVue :unity="unityContext" />
  </div>
</template>

2、使用iframe引入

首先将 unity工程打包后放在服务器上,我是使用的nginx,因为比较方便,自己就能测试。

然后将iframe的地址填写为在nginx的配置文件中配置的地址就行了

nginx配置:

javascript 复制代码
server {
        listen       9081;
        server_name  localhost;
		add_header Access-Control-Allow-Origin *;
		add_header Access-Control-Allow-Headers X-Requested-With;
		add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
        location / {
            root   html/unityTest;
			try_files $uri $uri/  /index.html;
			add_header Cache-Control no-store;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

所以我这里就是127.0.0.1:9081 简单测试一下,此时直接浏览器访问该地址也能看到unity项目正常运行

在vue中:

我是用了一个外部配置文件:

javascript 复制代码
const configUrl = {
    baseURL: 'http://192.168.2.116:8004', //资源服务接口地址
    unityURL: 'http://192.168.2.116:9081', //unity地址
}
javascript 复制代码
<script setup lang="ts">
const iframeSrc = configUrl.unityURL;
</script>
<template>
  <iframe :src="iframeSrc" class="iframe" frameborder="0"></iframe>
</template>

调整一下css就行了

相关推荐
微三云生态系统架构师-彭丹10 小时前
任务卷轴积分系统架构设计:从任务状态机到合规风控的完整实现
unity·系统架构·游戏引擎
JavaEdge.11 小时前
scoped 样式与 data-v-* 属性选择器(写给 Javaer)
开发语言·vue
XR技术研习社11 小时前
从 PICO 文档看未来短期内的 Unity 版本选择
unity·游戏引擎·xr·vr
玖玥拾14 小时前
Unity 热更新(一)
unity·游戏引擎
软件黑马王子14 小时前
1.non-MonoBehaviour 单例模式泛型基类
unity·前端框架·c#
sunphp开发者14 小时前
明灯智慧地图导览系统制作教程
vue·php·uniapp·js·贴图
KhalilRuan1 天前
Unity性能优化
unity·性能优化·游戏引擎
ellis19701 天前
u3d IMGUI[二] 编辑器扩展
unity
南城以南溫暖如初1471 天前
社交裂变商城系统设计与实战开发指南
java·spring boot·mysql·vue·mybatis
南城以南溫暖如初1471 天前
分享返现商城系统开发实战:技术架构与运营经验指南
java·spring boot·mysql·架构·vue·mybatis