unity-loader.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. var container = document.querySelector("#unity-container");
  2. var canvas = document.querySelector("#unity-canvas");
  3. var loadingBar = document.querySelector("#unity-loading-bar");
  4. var progressBarFull = document.querySelector("#unity-progress-bar-full");
  5. var warningBanner = document.querySelector("#unity-warning");
  6. function unityShowBanner(msg, type) {
  7. function updateBannerVisibility() {
  8. warningBanner.style.display = warningBanner.children.length ? 'block' : 'none';
  9. }
  10. var div = document.createElement('div');
  11. div.innerHTML = msg;
  12. warningBanner.appendChild(div);
  13. if (type == 'error') div.style = 'background: red; padding: 10px;';
  14. else {
  15. if (type == 'warning') div.style = 'background: yellow; padding: 10px;';
  16. setTimeout(function() {
  17. warningBanner.removeChild(div);
  18. updateBannerVisibility();
  19. }, 5000);
  20. }
  21. updateBannerVisibility();
  22. }
  23. /*
  24. var buildUrl = unityName + "/Build";
  25. var loaderUrl = buildUrl + "/" + unityName +".loader.js";
  26. var config = {
  27. dataUrl: buildUrl + "/" + unityName + ".data.unityweb",
  28. frameworkUrl: buildUrl + "/" + unityName + ".framework.js.unityweb",
  29. codeUrl: buildUrl + "/" + unityName + ".wasm.unityweb",
  30. streamingAssetsUrl: "StreamingAssets",
  31. companyName: "DefaultCompany",
  32. productName: "C3Monitor",
  33. productVersion: "0.1",
  34. showBanner: unityShowBanner,
  35. };
  36. */
  37. var buildUrl = "/BIM-Monitor/assets/AllSitesWebGL/Build";
  38. var loaderUrl = buildUrl + "/AllSitesWebGL.loader.js";
  39. var config = {
  40. dataUrl: buildUrl + "/AllSitesWebGL.data.unityweb",
  41. frameworkUrl: buildUrl + "/AllSitesWebGL.framework.js.unityweb",
  42. codeUrl: buildUrl + "/AllSitesWebGL.wasm.unityweb",
  43. streamingAssetsUrl: "StreamingAssets",
  44. companyName: "DefaultCompany",
  45. productName: "C3Monitor",
  46. productVersion: "0.1",
  47. showBanner: unityShowBanner,
  48. };
  49. if (/iPhone|iPad|iPod|Android/i.test(navigator.userAgent)) {
  50. // Mobile device style: fill the whole browser client area with the game canvas:
  51. var meta = document.createElement('meta');
  52. meta.name = 'viewport';
  53. meta.content = 'width=device-width, height=device-height, initial-scale=1.0, user-scalable=no, shrink-to-fit=yes';
  54. document.getElementsByTagName('head')[0].appendChild(meta);
  55. container.className = "unity-mobile";
  56. // To lower canvas resolution on mobile devices to gain some
  57. // performance, uncomment the following line:
  58. // config.devicePixelRatio = 1;
  59. canvas.style.width = '100%';
  60. canvas.style.height = '610px';
  61. unityShowBanner('WebGL builds are not supported on mobile devices.');
  62. } else {
  63. // Desktop style: Render the game canvas in a window that can be maximized to fullscreen:
  64. //canvas.style.width = '600px';
  65. canvas.style.height = '610px';
  66. canvas.style.width = '100%';
  67. }
  68. loadingBar.style.display = "block";
  69. var myGameInstance = null;
  70. var script = document.createElement("script");
  71. var isLoad = false;
  72. script.src = loaderUrl;
  73. script.onload = () => {
  74. createUnityInstance(canvas, config, (progress) => {
  75. progressBarFull.style.width = Math.round(100 * progress) + "%";
  76. progressBarFull.innerHTML = Math.round(100 * progress) + "%";
  77. }).then((unityInstance) => {
  78. loadingBar.style.display = "none";
  79. myGameInstance = unityInstance;
  80. isLoad = true;
  81. }).catch((message) => {
  82. alert(message);
  83. });
  84. };
  85. document.body.appendChild(script);