Vue中引入Unity WebGL打包程序

Unity WebGL 打包引入Vue项目中

1、将unity WebGL 打包文件放到vue项目中public中
在这里插入图片描述
2、打开webbuild下index.html,将body里的代码复制到需要Vue文件中,

(1)webbuild下文件

<!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 | parkcar</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"></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-footer">
        <div id="unity-webgl-logo"></div>
        <div id="unity-fullscreen-button"></div>
        <div id="unity-build-title">parkcar</div>
      </div>
    </div>
    <script>
      var buildUrl = "Build";
      var loaderUrl = buildUrl + "/webbuild.loader.js";
      var config = {
        dataUrl: buildUrl + "/webbuild.data",
        frameworkUrl: buildUrl + "/webbuild.framework.js",
        codeUrl: buildUrl + "/webbuild.wasm",
        streamingAssetsUrl: "StreamingAssets",
        companyName: "DefaultCompany",
        productName: "parkcar",
        productVersion: "1.0",
      };

      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");

      if (/iPhone|iPad|iPod|Android/i.test(navigator.userAgent)) {
        container.className = "unity-mobile";
        config.devicePixelRatio = 1;
      } else {
        canvas.style.width = "1024px";
        canvas.style.height = "768px";
      }
      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";
          fullscreenButton.onclick = () => {
            unityInstance.SetFullscreen(1);
          };
        }).catch((message) => {
          alert(message);
        });
      };
      document.body.appendChild(script);
    </script>
  </body>
</html>

(2)Veu文件

<template>
<!-- webbuild里body里的代码,追重要的是canvas标签里的内容 -->
    <div id="unity-container" class="unity-desktop" style="width: 99vm;height: 100%;">
      <canvas id="unity-canvas" style="width: 1920px;height: 100%;"></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>

</template>
<script>
  export default {
    mounted(){
      this.init()
    },
    methods: {
      init() {//webuild里js的代码
        var buildUrl = "webbuild/Build";
        var loaderUrl = buildUrl + "/webbuild.loader.js";
        var config = {
          dataUrl: buildUrl + "/webbuild.data",
          frameworkUrl: buildUrl + "/webbuild.framework.js",
          codeUrl: buildUrl + "/webbuild.wasm",
          streamingAssetsUrl: "StreamingAssets",
          companyName: "DefaultCompany",
          productName: "parkcar",
          productVersion: "1.0",
        };

        var container = document.querySelector("#unity-container");
        var canvasx = 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");//被删除了

        if (/iPhone|iPad|iPod|Android/i.test(navigator.userAgent)) {
          container.className = "unity-mobile";
          config.devicePixelRatio = 1;
        } else {
          canvasx.style.width = "1920px";//显示大小
          canvasx.style.height = "1080px";
        }
        loadingBar.style.display = "block";

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

3、样式直接全局引用

真实在项目中采用的方式——webGL引入vue项目

将webGL部署在IIS上,再通iframe 标签引入webGL发布地址。

src是iis发布地址

<iframe src="http://localhost:7100/" width="1920" height="1080" webkitallowfullscreen="true" mozallowfullscreen="true" allowfullscreen="true" frameborder="0"></iframe>

webGL部署iis配置,MIME配置
.wasm application/wasm
.data application/data

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值