index.html 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <!DOCTYPE html>
  2. <html lang="en-us">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  6. <title>Unity WebGL Player | HeNanMuseum</title>
  7. <link rel="shortcut icon" href="TemplateData/favicon.ico">
  8. <link rel="stylesheet" href="TemplateData/style.css">
  9. <link rel="manifest" href="manifest.webmanifest">
  10. </head>
  11. <body>
  12. <div id="unity-container">
  13. <canvas id="unity-canvas" width=960 height=600 tabindex="-1"></canvas>
  14. <div id="unity-loading-bar">
  15. <div id="unity-logo"></div>
  16. <div id="unity-progress-bar-empty">
  17. <div id="unity-progress-bar-full"></div>
  18. </div>
  19. </div>
  20. <div id="unity-warning"> </div>
  21. </div>
  22. <script>
  23. window.addEventListener("load", function () {
  24. if ("serviceWorker" in navigator) {
  25. navigator.serviceWorker.register("ServiceWorker.js");
  26. }
  27. });
  28. var container = document.querySelector("#unity-container");
  29. var canvas = document.querySelector("#unity-canvas");
  30. var loadingBar = document.querySelector("#unity-loading-bar");
  31. var progressBarFull = document.querySelector("#unity-progress-bar-full");
  32. var warningBanner = document.querySelector("#unity-warning");
  33. let unityInstance = null
  34. // Shows a temporary message banner/ribbon for a few seconds, or
  35. // a permanent error message on top of the canvas if type=='error'.
  36. // If type=='warning', a yellow highlight color is used.
  37. // Modify or remove this function to customize the visually presented
  38. // way that non-critical warnings and error messages are presented to the
  39. // user.
  40. function unityShowBanner(msg, type) {
  41. function updateBannerVisibility() {
  42. warningBanner.style.display = warningBanner.children.length ? 'block' : 'none';
  43. }
  44. var div = document.createElement('div');
  45. div.innerHTML = msg;
  46. warningBanner.appendChild(div);
  47. if (type == 'error') div.style = 'background: red; padding: 10px;';
  48. else {
  49. if (type == 'warning') div.style = 'background: yellow; padding: 10px;';
  50. setTimeout(function () {
  51. warningBanner.removeChild(div);
  52. updateBannerVisibility();
  53. }, 5000);
  54. }
  55. updateBannerVisibility();
  56. }
  57. var buildUrl = "Build";
  58. var loaderUrl = buildUrl + "/Build13.loader.js";
  59. var config = {
  60. dataUrl: buildUrl + "/Build13.data.unityweb",
  61. frameworkUrl: buildUrl + "/Build13.framework.js.unityweb",
  62. codeUrl: buildUrl + "/Build13.wasm.unityweb",
  63. streamingAssetsUrl: "StreamingAssets",
  64. companyName: "DefaultCompany",
  65. productName: "HeNanMuseum",
  66. productVersion: "0.1",
  67. showBanner: unityShowBanner,
  68. };
  69. // By default Unity keeps WebGL canvas render target size matched with
  70. // the DOM size of the canvas element (scaled by window.devicePixelRatio)
  71. // Set this to false if you want to decouple this synchronization from
  72. // happening inside the engine, and you would instead like to size up
  73. // the canvas DOM size and WebGL render target sizes yourself.
  74. // config.matchWebGLToCanvasSize = false;
  75. if (/iPhone|iPad|iPod|Android/i.test(navigator.userAgent)) {
  76. // Mobile device style: fill the whole browser client area with the game canvas:
  77. var meta = document.createElement('meta');
  78. meta.name = 'viewport';
  79. meta.content = 'width=device-width, height=device-height, initial-scale=1.0, user-scalable=no, shrink-to-fit=yes';
  80. document.getElementsByTagName('head')[0].appendChild(meta);
  81. }
  82. loadingBar.style.display = "block";
  83. var script = document.createElement("script");
  84. script.src = loaderUrl;
  85. script.onload = () => {
  86. createUnityInstance(canvas, config, (progress) => {
  87. progressBarFull.style.width = 100 * progress + "%";
  88. }).then((instance) => {
  89. loadingBar.style.display = "none";
  90. unityInstance = instance
  91. }).catch((message) => {
  92. alert(message);
  93. });
  94. };
  95. document.body.appendChild(script);
  96. // 新添加(以下全是)
  97. window.onShareImage = (img) => {
  98. console.log('result:', img);
  99. window.parent.postMessage({
  100. source: 'shareImg',
  101. data: img,
  102. },
  103. "*")
  104. }
  105. // 禁用UI交互
  106. window.disableUIInteract = () => {
  107. // console.log('禁用ui:111');
  108. window.parent.postMessage({
  109. source: 'disableUIInteract',
  110. data: true
  111. },
  112. "*")
  113. }
  114. // 启用UI交互
  115. window.enableUIInteract = () => {
  116. // console.log('禁用ui:111');
  117. window.parent.postMessage({
  118. source: 'enableUIInteract',
  119. data: true
  120. },
  121. "*")
  122. }
  123. // 拼接模型成功,接受 unity的消息 取消 模型的选中ui效果
  124. window.modelSuccItem = () => {
  125. window.parent.postMessage({
  126. source: 'modelSuccItem',
  127. data: true
  128. },
  129. "*")
  130. }
  131. window.Internal_ShowTouchErrorTips = (data) => {
  132. console.log('result:', data);
  133. }
  134. window.showTouchErrorTips = (data) => {
  135. console.log('result:', data);
  136. }
  137. window._Internal_ShowTouchErrorTips = (data) => {
  138. console.log('result:', data);
  139. }
  140. window.addEventListener('message', (res) => {
  141. console.log('result:', unityInstance);
  142. if (!unityInstance) return
  143. if (Object.prototype.toString.call(res.data) == "[object Object]") {
  144. let data = res.data.data;
  145. // 切换模块
  146. if (res.data.source === "changeBlock") {
  147. console.log('result:changeBlock', data);
  148. unityInstance.SendMessage('Main', 'OnClickModule', data);
  149. }
  150. // 切换类型
  151. else if (res.data.source === "clickTypeBtn") {
  152. unityInstance.SendMessage('Main', 'OnClickType', data === 'block' ? 0 : 1);
  153. console.log('result:OnClickType', data === 'block' ? 0 : 1);
  154. }
  155. // 点击子项
  156. else if (res.data.source === "clickItemBtn") {
  157. console.log('选中了子选项', data);
  158. unityInstance.SendMessage('Main', 'OnClickItem', data);
  159. }
  160. // 保存
  161. else if (res.data.source === "Save") {
  162. unityInstance.SendMessage('Main', 'Save');
  163. }
  164. // 取消
  165. else if (res.data.source === "Cancel") {
  166. unityInstance.SendMessage('Main', 'Cancel');
  167. }
  168. // 图片回调
  169. else if (res.data.source === "CallShareImage") {
  170. unityInstance.SendMessage('Main', 'CallShareImage');
  171. }
  172. // 到处逛逛
  173. else if (res.data.source === "ShowUserData") {
  174. console.log('到处逛逛,用户id:', data);
  175. unityInstance.SendMessage('Main', 'ShowUserData', data);
  176. }
  177. // 点击编辑
  178. else if (res.data.source === "OnClickEdit") {
  179. // console.log('----------------');
  180. unityInstance.SendMessage('Main', 'OnClickEdit');
  181. }
  182. }
  183. })
  184. </script>
  185. </body>
  186. </html>