Unity打包WebGL: 导入Vue

news/2025/2/14 0:33:55/

Unity打包WebGL: 导入Vue

1. 介绍

1.1 任务

记录将Unity项目打包成WebGL,并集成到Vue项目中的过程。

1.2 环境

  • Unity:2021.3
  • Vue: 2

2. Unity项目

2.1 UI界面

在这里插入图片描述

2.2 添加插件

构建WebGL项目需要添加一个.jslib文件,用于Unity脚本函数与JavaScript函数交互
详细内容参考:Unity(WebGL)与JS通讯2022最新姿势
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
web.jslib文件内容

mergeInto(LibraryManager.library, {SayHello: function () {window.alert("hello vue");},ReportReady: function() {window.ReportReady();}
})

2.3 添加脚本CBtn.cs

为按钮添加脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Runtime.InteropServices;public class CBtn : MonoBehaviour
{[DllImport("__Internal")]private static extern void SayHello();[DllImport("__Internal")]private static extern string ReportReady();public Text text;// Start is called before the first frame updatevoid Start(){ReportReady();}// Update is called once per framevoid Update(){if (Input.GetKeyUp(KeyCode.H)) {SayHello();}}public void SetToken(string token) {this.text.text = token;}
}

2.4 打包WebGL

在这里插入图片描述
在这里插入图片描述
一些设置参考:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
构建:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2.5 修改index.html

html文件引入其他js文件的格式,
具体参考: webGl使用jsLib与Js交互
在这里插入图片描述

<!DOCTYPE html>
<html lang="en-us"><head><meta charset="utf-8"><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Unity WebGL Player | Web with Vue</title><link rel="shortcut icon" href="TemplateData/favicon.ico"><link rel="stylesheet" href="TemplateData/style.css"></head><body><div id="unity-container" class="unity-desktop"><canvas id="unity-canvas" width=960 height=600></canvas><div id="unity-loading-bar"><div id="unity-logo"></div><div id="unity-progress-bar-empty"><div id="unity-progress-bar-full"></div></div></div><div id="unity-warning"> </div><div id="unity-footer"><div id="unity-webgl-logo"></div><div id="unity-fullscreen-button"></div><div id="unity-build-title">Web with Vue</div></div></div><script>var container = document.querySelector("#unity-container");var canvas = document.querySelector("#unity-canvas");var loadingBar = document.querySelector("#unity-loading-bar");var progressBarFull = document.querySelector("#unity-progress-bar-full");var fullscreenButton = document.querySelector("#unity-fullscreen-button");var warningBanner = document.querySelector("#unity-warning");// Shows a temporary message banner/ribbon for a few seconds, or// a permanent error message on top of the canvas if type=='error'.// If type=='warning', a yellow highlight color is used.// Modify or remove this function to customize the visually presented// way that non-critical warnings and error messages are presented to the// user.function unityShowBanner(msg, type) {function updateBannerVisibility() {warningBanner.style.display = warningBanner.children.length ? 'block' : 'none';}var div = document.createElement('div');div.innerHTML = msg;warningBanner.appendChild(div);if (type == 'error') div.style = 'background: red; padding: 10px;';else {if (type == 'warning') div.style = 'background: yellow; padding: 10px;';setTimeout(function() {warningBanner.removeChild(div);updateBannerVisibility();}, 5000);}updateBannerVisibility();}var buildUrl = "Build";var loaderUrl = buildUrl + "/Web.loader.js";var config = {dataUrl: buildUrl + "/Web.data",frameworkUrl: buildUrl + "/Web.framework.js",codeUrl: buildUrl + "/Web.wasm",streamingAssetsUrl: "StreamingAssets",companyName: "DefaultCompany",productName: "Web with Vue",productVersion: "0.1",showBanner: unityShowBanner,};// By default Unity keeps WebGL canvas render target size matched with// the DOM size of the canvas element (scaled by window.devicePixelRatio)// Set this to false if you want to decouple this synchronization from// happening inside the engine, and you would instead like to size up// the canvas DOM size and WebGL render target sizes yourself.// config.matchWebGLToCanvasSize = false;if (/iPhone|iPad|iPod|Android/i.test(navigator.userAgent)) {// Mobile device style: fill the whole browser client area with the game canvas:var meta = document.createElement('meta');meta.name = 'viewport';meta.content = 'width=device-width, height=device-height, initial-scale=1.0, user-scalable=no, shrink-to-fit=yes';document.getElementsByTagName('head')[0].appendChild(meta);container.className = "unity-mobile";canvas.className = "unity-mobile";// To lower canvas resolution on mobile devices to gain some// performance, uncomment the following line:// config.devicePixelRatio = 1;unityShowBanner('WebGL builds are not supported on mobile devices.');} else {// Desktop style: Render the game canvas in a window that can be maximized to fullscreen:canvas.style.width = "960px";canvas.style.height = "600px";}loadingBar.style.display = "block";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";// 绑定unityInstancewindow.unityInstance = unityInstancefullscreenButton.onclick = () => {unityInstance.SetFullscreen(1);};}).catch((message) => {alert(message);});};document.body.appendChild(script);// unity调用函数window.ReportReady = () => {// window.top.dispatchEvent(new CustomEvent())send({id: 1,value: 2})}function send(obj) {unityInstance.SendMessage('button', 'SetToken', JSON.stringify(obj))}</script></body>
</html>

3. Vue项目

3.1 项目初始化

在这里插入图片描述

3.2 引入WebGL项目

在这里插入图片描述

3.3 监听WebGL事件

  1. 修改App.vue
<template><div id="app"><HelloWorld msg="Welcome to Your Vue.js App"/></div>
</template><script>
import HelloWorld from './components/HelloWorld.vue'export default {name: 'App',components: {HelloWorld}
}
</script><style>
#app {font-family: Avenir, Helvetica, Arial, sans-serif;-webkit-font-smoothing: antialiased;-moz-osx-font-smoothing: grayscale;text-align: center;color: #2c3e50;margin-top: 60px;
}
</style>
  1. 修改HelloWorld.vue
<template><div><div @click="send">尝试发送数据</div><iframeref="iframe"width="1200"height="800"scrolling="no"src="/Unity/index.html"frameborder="0"></iframe></div>
</template><script>
export default {name: 'HelloWorld',props: {msg: String},data () {return {}},mounted () {window.addEventListener('fn', this.unityWatch)this.$once('hook:beforeDestroy', () => {window.removeEventListener('fn', this.unityWatch)})},methods: {send () {// 发送数据this.$refs.iframe.contentWindow.send({id: 12,value: 34})},unityWatch (e) {alert(e.detail)}}
}
</script><!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped></style>

4. 测试

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

X. 参考

  1. 与Vue结合
    【vue】vue与unity交互(不需要任何第三方插件)
    unity发布webgl平台结合vue,并实现事件交互
    vue项目接入unity3D模块并进行数据通信
    vue与unity进行交互,包括unity发收,unity自动获取数据等
    Unity3D(2021版)打包成webgl和前端vue交互

  2. JS创建和触发 events, window.top.dispatchEvent
    创建和触发 events


http://www.ppmy.cn/news/46137.html

相关文章

ai智能写作软件哪个好-ai智能写作免费

人工智能自动写作软件 人工智能自动写作软件是如今数字营销领域中备受瞩目的一种工具。无论是网络文章、博客、报告、新闻稿或者其他一些营销内容&#xff0c;人工智能自动写作软件可以以相当高的速度和质量将其生成&#xff0c;从而释放人类营销人员的时间和精力。 尽管自动写…

【数据挖掘与商务智能决策】第十二章 XGBoost算法和LightGBM算法

12.1.3 XGBoost算法的简单代码实现 XGBoost模型既可以做分类分析&#xff0c;也可以做回归分析&#xff0c;分别对应的模型为XGBoost分类模型&#xff08;XGBClassifier&#xff09;及XGBoost回归模型&#xff08;XGBRegressor&#xff09;。 XGBoost模型的安装办法可以采用P…

Idea启动运行报错:Error:java: 无效的源发行版: 13

最近在做Springboot项目时&#xff0c;常常出现上述错误&#xff0c;小编也不知道怎么回事&#xff0c;到网上找了这个方面的解决办法&#xff0c;但是却发现根本解决不了&#xff0c;最终通过小编多次尝试&#xff0c;终于发现&#xff0c;为什么会报这个错误。(应该是Java版本…

javaweb830在线答疑系统dzkfA1A5程序

2&#xff0e;系统用户管理&#xff1a;不管是超级管理员还是普通管理员都需要管理系统用户&#xff0c;包括普通管理员的添加、删除、修改、查询&#xff0c;修改管理员的登录密码&#xff0c;新添加的管理员用户可以登录系统。 3&#xff0e;注册用户管理&#xff1a;游客在前…

安全加固服务是什么?哪些行业需要做?

安全加固服务是什么&#xff1f;安全加固服务是一种针对企业信息系统、网络设备、应用程序等进行安全加固和优化的服务。安全加固服务的主要目的是保障企业信息系统的安全性和稳定性&#xff0c;有效防范各类网络攻击和安全威胁。 安全加固服务是什么&#xff1f;通常包括以下…

网络中的阻塞与非阻塞以及reactor模型

文章目录 一、网络IO的职责操作IOIO的操作方式**阻塞与非阻塞IO的具体差别&#xff1a;**阻塞IO在系统调用中的流程非阻塞IO在系统调用中的流程 网络编程系统调用具备检测和操作的功能accept&#xff1a;read&#xff1a;write&#xff1a; 二、系统调用在调用非阻塞IO的具体处…

Centos7.6集群部署海豚调度3.1.5

目录 前置准备工作&#xff08;所有机器&#xff09;主机规划数据库规划用户规划目录规划配置/etc/hostsjdk安装进程树分析配置ssh免密部署zookeeper启动zookeeper下载DolphinScheduler 二进制包修改install_env.sh配置修改dolphinscheduler_env.sh配置文件 安装&#xff08;ty…

毕业2年,跳槽到下一个公司就25K了,厉害了···

本人本科就读于某普通院校&#xff0c;毕业后通过同学的原因加入软件测试这个行业&#xff0c;角色也从测试小白到了目前的资深工程师&#xff0c;从功能测试转变为测试开发&#xff0c;并顺利拿下了某二线城市互联网企业的Offer&#xff0c;年薪 30W 。 选择和努力哪个重要&a…