• Vue 中引入 Unity3D


    一、前言

    在一个新的项目中,要在页面中嵌入三维部门用 Unity3D 做的场景。在摸索中使用了一些方法。下面是对这些总结。

    二、Iframe 嵌入

    这种做法是,先写一个简单的 HTML ,把 Unity3D 包含进来。再在项目中用 Iframe 嵌入。

    HTML 代码:

      <body>
        <div id="gameContainer"></div>
        <div id="loader">
          <img class="logo" src="logo.png">
          <div class="spinner"></div>
          <div class="progress"><div class="full"></div></div>
        </div>
      </body>
    
      <script src="Build/UnityLoader.js"></script>
      <script>
      var gameInstance = UnityLoader.instantiate("gameContainer", "Build/8.json", {onProgress: UnityProgress});
      function UnityProgress(gameInstance, progress) {
        if (!gameInstance.Module) {
          return;
        }
        const loader = document.querySelector("#loader");
        if (!gameInstance.progress) {
          const progress = document.querySelector("#loader .progress");
          progress.style.display = "block";
          gameInstance.progress = progress.querySelector(".full");
          loader.querySelector(".spinner").style.display = "none";
        }
        gameInstance.progress.style.transform = `scaleX(${progress})`;
        if (progress === 1 && !gameInstance.removeTimeout) {
          //gameInstance.removeTimeout = setTimeout(function() {
              loader.style.display = "none";
          //}, 2000);
        }
      }
      function UnitySceneIsReadyJSNative() {
          //alert("UnitySceneIsReady JS Native Here ");
          gameInstance.SendMessage("JSManager", "SendData", "Come From JS Message");
      }
      </script>

    上面代码中使用到的文件是:

    UnityLoader :Unity 加载用的

    8.json : Unity3D 文件

    把这个文件用 Nginx 代理下,在项目中就可以直接使用了:

    <iframe id="unity-infame" :src="unityUrl" frameborder="0" width="100%" height="100%" scrolling="no" />

    unityUrl 是代理后的地址

    这样对于只是看一看没有问题,但是当需要和 Unity3D 进行通信就不行了。

    三、第三方组件

    遇到上面的问题后,想应该会有开源的组件可以用的。

    于是搜索了是否有在 Vue 中使用的 Unity3D 的包。

    搜索到了几个:unity-packman、vue-unity-webgl(这些都可以在 npm 网站搜索到),对比着两个后,发现 vue-unity-webgl 更适合。

    安装完成,按照示例写了如下代码:

    <template>
      <div class="flood-plan-page">
        <unity ref="unityPlan" src="static/Build/8.json" class="unity-box" unity-loader="static/Build/UnityLoader.js" />
      </div>
    </template>
    
    <script>
    import Unity from 'vue-unity-webgl'
    export default {
      components: {
        Unity
      },
      methods: {
        alarmClick() {
        // 和 Unity 通信
          this.$refs.unityPlan.message('JSManager', 'SendData', '发送内容')
        }
      }
    
    }
    </script>

    对于样式这一块来说,官方说的的是直接 .unity 就是对应的样式包含块,但是不生效。在看了渲染后的代码后,使用如下样式

      .webgl-content {
        position: absolute;
         965px;
        height: calc(100% - 212px);
        left: 368px;
        top: 10px;
    
        #unity-container {
          height: 100%;
           100%;
        }
    
        .footer {
          position: absolute;
          left: 5px;
          bottom: 5px;
        }
      }

    .webgl-content 是包含块。

    四、和 Unity 通信

    通信主要有:主动和被动

    主动通信:直接调取 Unity 里面的方法等,具体操作:

    // 这个是在 Vue 里面,使用上面的 vue-unity-webgl
    // unityPlan 是组件暴露的实例
    // 参数前面两个是 unity 中的类、方法,最后一个是参数
    this.$refs.unityPlan.message('JSManager', 'SendData', '环丁水系')

    被动通信:unity 调用本地代码,具体操作:

    // 需要把要被调用的方法挂载在 window 下面  
    // 放在 mounted 钩子里面
    mounted() {
        // 播放监控(和 unity 交互)
        window['UnityCallMonitorJSNative'] = (code) => {
          this.monitorCode = code
        }
      }

    问题:

    在简单测了几遍后,发现再全屏返回时控制台出现很多报错信息。后面打算再做调整

  • 相关阅读:
    如何找按钮数组在布局中的横竖坐标位置?
    java中使用rmi进行远程方法调用
    测试视频集,各种测试用的视频文件
    如何彻底禁用VS 2008的智能感知功能
    JavaScript秘密花园 Type Casting,undefined,eval,setTimeout,Auto Semicolon Insertion
    JavaScript秘密花园 scope, namespace, constructor, equality and comparisons
    造成内存位置访问无效的一个原因
    depends在VS2008消失了
    12月10日晚的月全食照片
    JavaScript秘密花园 Array, Array Constructor, for in loop, typeof, instanceOf
  • 原文地址:https://www.cnblogs.com/zhurong/p/13094214.html
Copyright © 2020-2023  润新知