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就行了

相关推荐
weixin_443566983 小时前
天学网面试 —— 中级前端开发岗位
vue
YGY Webgis糕手之路11 小时前
OpenLayers 综合案例-轨迹回放
前端·经验分享·笔记·vue·web
aiguangyuan14 小时前
前端开发 Vue 组件优化
系统架构·vue·前端开发
YGY Webgis糕手之路16 小时前
OpenLayers 快速入门(九)Extent 介绍
前端·经验分享·笔记·vue·web
16 小时前
3D碰撞检测系统 基于SAT算法+Burst优化(Unity)
算法·3d·unity·c#·游戏引擎·sat
pengzhuofan18 小时前
Web开发系列-第13章 Vue3 + ElementPlus
前端·elementui·vue·web
dzj202121 小时前
Unity是如何把3D场景显示到屏幕上的——Unity的渲染过程
3d·unity·游戏引擎·渲染·图形学
死也不注释2 天前
【鸡零狗碎记录】
unity·c#
★YUI★2 天前
学习游戏制作记录(剑投掷技能)7.26
学习·游戏·unity·c#
伍哥的传说3 天前
CSS+JavaScript 禁用浏览器复制功能的几种方法
前端·javascript·css·vue.js·vue·css3·禁用浏览器复制