当前位置:   article > 正文

unity发布webgl平台结合vue,并实现事件交互_unity webgl封装vue组件

unity webgl封装vue组件

1.初始化vue项目(以vscode中搭建为例,默认已经安装好vscode和nodejs)

执行以下命令下载依赖包

 

 输入命令:vue init webpack myvue(初始化项目)

之后输入命令:cd myvue   定位到项目工程目录

再输入命令:npm run dev  启动项目

如果启动时出现下面的错误

 需要打开webpack.base.conf.js文件注释以下这一行代码,关闭代码规范检测

 然后重新 npm run dev  启动项目就OK了

2.unity打包文件导入vue项目中

打包unity项目,将unity发布出来的文件夹导入到vue项目中的static文件夹下

             

然后需要下载vue-unity-webgl组件,否则会报错

 然后在vue中调用unity组件

  

 调整样式

事件交互

unity调用vue页面方法:

unity向vue组建发送数据 :Application.ExternalCall("enter",param);

vue组建监听事件:

       

vue调用unity内部方法:

 在Unity中新建一个gameobject,命名为JSManager,该物体上挂一个脚本,脚本中定义一个方法名为SendData;

3.自定义vue-unity-webgl组件(不使用vue-unity-webgl插件)

   我们可以自己写类似于vue-unity-webgl的组件

  1. <template>
  2. <div class="webgl-content">
  3. <div
  4. id="unityContainer"
  5. :style="{
  6. width: width + 'px',
  7. height: height + 'px',
  8. }"
  9. ></div>
  10. <div
  11. v-if="showFullScreen"
  12. class="fullscreen"
  13. @click="showFullScreenFunc"
  14. ></div>
  15. </div>
  16. </template>
  17. <script>
  18. export default {
  19. name: "vue-unity-webgl",
  20. props: [
  21. "width",
  22. "height",
  23. "config",
  24. "unityLoader",
  25. "onProgress",
  26. "showFullScreen",
  27. ],
  28. data() {
  29. return {
  30. unityInstance: null,
  31. loaded: false,
  32. };
  33. },
  34. mounted() {
  35. if (typeof UnityLoader === "undefined") {
  36. const script = document.createElement("script");
  37. script.setAttribute("src", this.unityLoader);
  38. document.body.appendChild(script);
  39. script.addEventListener("load", () => {
  40. this.instantiate();
  41. });
  42. }
  43. },
  44. methods: {
  45. message(object, method, params) {
  46. if (params === null) {
  47. params = "";
  48. }
  49. if (this.unityInstance !== null) {
  50. this.unityInstance.SendMessage(object, method, params);
  51. } else {
  52. return Promise.reject(
  53. "vue-unity-webgl: 你给 Unity 发送的信息, 没有被接收到"
  54. );
  55. }
  56. },
  57. showFullScreenFunc() {
  58. this.instance.SetFullscreen(1);
  59. // document.documentElement.webkitRequestFullScreen();
  60. },
  61. instantiate() {
  62. if (typeof UnityLoader === "undefined") {
  63. return Promise.reject("UnityLoader 未定义");
  64. }
  65. if (!this.config) {
  66. return Promise.reject("config 未定义配置");
  67. }
  68. let params = {};
  69. params.onProgress = (instance, progress) => {
  70. this.onProgress && this.onProgress(instance, progress);
  71. this.loaded = progress === 1;
  72. };
  73. this.unityInstance = UnityLoader.instantiate(
  74. "unityContainer",
  75. this.config,
  76. params
  77. );
  78. },
  79. },
  80. };
  81. </script>
  82. <style scoped>
  83. .webgl-content {
  84. width: 1280px;
  85. height: 720px;
  86. }
  87. .fullscreen {
  88. height: 38px;
  89. width: 38px;
  90. float: right;
  91. background-image: url("../assets/fullscreen.png");
  92. }
  93. </style>

然后再自己的代码中调用该组件

  1. <template>
  2. <div id="webgl-box-warpper">
  3. <vue-unity-webgl
  4. v-if="is_show_webgl"
  5. ref="vueUnityWebgl"
  6. :config="webglConfig"
  7. :unity-loader="webglUnityLoader"
  8. :width="1280"
  9. :height="720"
  10. :on-progress="listenWebglProgress"
  11. :showFullScreen="true"
  12. ></vue-unity-webgl>
  13. <div class="loadmask" v-show="showp"></div>
  14. <el-progress
  15. v-show="showp"
  16. class="progress"
  17. :text-inside="true"
  18. :stroke-width="16"
  19. :percentage="percent"
  20. :color="customColor"
  21. ></el-progress>
  22. </div>
  23. </template>
  24. <script>
  25. import VueUnityWebgl from "@/components/vue-unity-webgl";
  26. export default {
  27. components: {
  28. VueUnityWebgl,
  29. },
  30. data() {
  31. return {
  32. showp: true,
  33. customColor: "#ffbb34",
  34. percent: 0,
  35. is_show_webgl: true,
  36. webglConfig: "static/Build/demo.json",
  37. webglUnityLoader: "static/Build/UnityLoader.js",
  38. webglWith: 1000,
  39. webglHeight: 600,
  40. };
  41. },
  42. created() {
  43. // window.toVueWebgl = (action) => {
  44. // action && this.sendWebglMessage("{}", "第二次发送JSON数据");
  45. // };
  46. },
  47. mounted() {
  48. // window.ReportReady = () => {
  49. // this.sendWebglMessage("1", "第一次发送内容标识");
  50. // };
  51. window.enter=(data)=>{
  52. console.log("从unity发过来的消息"+data);
  53. }
  54. },
  55. methods: {
  56. listenWebglProgress(instance, progress) {
  57. this.percent = progress * 100;
  58. if (progress === 1) {
  59. this.showp = false;
  60. }
  61. },
  62. sendWebglMessage(message,info) {
  63. console.log(info);
  64. this.$refs.vueUnityWebgl.message("JsTalker", "toUnityWebgl", message);
  65. },
  66. },
  67. };
  68. </script>
  69. <style scoped>
  70. .loadmask {
  71. width: 1280px;
  72. height: 720px;
  73. position: absolute;
  74. top: 8px;
  75. left: 8px;
  76. background-image: url("../assets/logo1.png");
  77. background-size: 100%;
  78. background-repeat: no-repeat;
  79. }
  80. .progress {
  81. width: 800px;
  82. position: absolute;
  83. top: 600px;
  84. left: 240px;
  85. }
  86. #webgl-box-warpper {
  87. width: 1280px;
  88. height: 720px;
  89. }
  90. </style>

自定义的组件包含了加载进度条的监听和全屏按钮以及事件交互

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/94936
推荐阅读
相关标签
  

闽ICP备14008679号