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

Logo

前往低代码交流专区

更多推荐