当前位置:   article > 正文

UnityWebGL 设置启动后自动网页全屏2020.3.40f1版本_unity webgl全屏

unity webgl全屏

Unity版本2020.3.40f1

这是原版:

开始修改:

1.  打开html修改此处代码    数字改为auto,风格改为100%

     可直接粘贴代码如下 :

  1. <div id="unity-container" style="width: 100%;height:100%">
  2. <canvas id="unity-canvas" width=auto height=auto></canvas>

 2.  修改此处width 、height为100%

     可直接粘贴如下代码代码

  1. if (/iPhone|iPad|iPod|Android/i.test(navigator.userAgent)) {
  2. var meta = document.createElement('meta');
  3. meta.name = 'viewport';
  4. meta.content = 'width=device-width, height=device-height, initial-scale=1.0, user-scalable=no, shrink-to-fit=yes';
  5. document.getElementsByTagName('head')[0].appendChild(meta);
  6. container.className = "unity-mobile";
  7. canvas.style.width = window.innerWidth + 'px';
  8. canvas.style.height = window.innerHeight + 'px';
  9. unityShowBanner('暂不支持移动端...');
  10. } else {
  11. canvas.style.width = "100%";
  12. canvas.style.height = "100%";
  13. }

 以下是完整代码

  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 | WebGL测试工程</title>
  7. <link rel="shortcut icon" href="TemplateData/favicon.ico">
  8. <link rel="stylesheet" href="TemplateData/style.css">
  9. </head>
  10. <body>
  11. <div id="unity-container" style="width: 100%;height:100%">
  12. <canvas id="unity-canvas" width=auto height=auto></canvas>
  13. <div id="unity-loading-bar">
  14. <div id="unity-logo"></div>
  15. <div id="unity-progress-bar-empty">
  16. <div id="unity-progress-bar-full"></div>
  17. </div>
  18. </div>
  19. <div id="unity-warning"> </div>
  20. <div id="unity-footer">
  21. <div id="unity-webgl-logo"></div>
  22. <div id="unity-fullscreen-button"></div>
  23. <div id="unity-build-title">WebGL测试工程</div>
  24. </div>
  25. </div>
  26. <script>
  27. var container = document.querySelector("#unity-container");
  28. var canvas = document.querySelector("#unity-canvas");
  29. var loadingBar = document.querySelector("#unity-loading-bar");
  30. var progressBarFull = document.querySelector("#unity-progress-bar-full");
  31. var fullscreenButton = document.querySelector("#unity-fullscreen-button");
  32. var warningBanner = document.querySelector("#unity-warning");
  33. // Shows a temporary message banner/ribbon for a few seconds, or
  34. // a permanent error message on top of the canvas if type=='error'.
  35. // If type=='warning', a yellow highlight color is used.
  36. // Modify or remove this function to customize the visually presented
  37. // way that non-critical warnings and error messages are presented to the
  38. // user.
  39. function unityShowBanner(msg, type) {
  40. function updateBannerVisibility() {
  41. warningBanner.style.display = warningBanner.children.length ? 'block' : 'none';
  42. }
  43. var div = document.createElement('div');
  44. div.innerHTML = msg;
  45. warningBanner.appendChild(div);
  46. if (type == 'error') div.style = 'background: red; padding: 10px;';
  47. else {
  48. if (type == 'warning') div.style = 'background: yellow; padding: 10px;';
  49. setTimeout(function() {
  50. warningBanner.removeChild(div);
  51. updateBannerVisibility();
  52. }, 5000);
  53. }
  54. updateBannerVisibility();
  55. }
  56. var buildUrl = "Build";
  57. var loaderUrl = buildUrl + "/WebGL02.loader.js";
  58. var config = {
  59. dataUrl: buildUrl + "/WebGL02.data.unityweb",
  60. frameworkUrl: buildUrl + "/WebGL02.framework.js.unityweb",
  61. codeUrl: buildUrl + "/WebGL02.wasm.unityweb",
  62. streamingAssetsUrl: "StreamingAssets",
  63. companyName: "DefaultCompany",
  64. productName: "WebGL测试工程",
  65. productVersion: "0.1",
  66. showBanner: unityShowBanner,
  67. };
  68. // By default Unity keeps WebGL canvas render target size matched with
  69. // the DOM size of the canvas element (scaled by window.devicePixelRatio)
  70. // Set this to false if you want to decouple this synchronization from
  71. // happening inside the engine, and you would instead like to size up
  72. // the canvas DOM size and WebGL render target sizes yourself.
  73. // config.matchWebGLToCanvasSize = false;
  74. if (/iPhone|iPad|iPod|Android/i.test(navigator.userAgent)) {
  75. var meta = document.createElement('meta');
  76. meta.name = 'viewport';
  77. meta.content = 'width=device-width, height=device-height, initial-scale=1.0, user-scalable=no, shrink-to-fit=yes';
  78. document.getElementsByTagName('head')[0].appendChild(meta);
  79. container.className = "unity-mobile";
  80. canvas.style.width = window.innerWidth + 'px';
  81. canvas.style.height = window.innerHeight + 'px';
  82. unityShowBanner('WebGL builds are not supported on mobile devices.');
  83. } else {
  84. canvas.style.width = "100%";
  85. canvas.style.height = "100%";
  86. }
  87. loadingBar.style.display = "block";
  88. var script = document.createElement("script");
  89. script.src = loaderUrl;
  90. script.onload = () => {
  91. createUnityInstance(canvas, config, (progress) => {
  92. progressBarFull.style.width = 100 * progress + "%";
  93. }).then((unityInstance) => {
  94. loadingBar.style.display = "none";
  95. fullscreenButton.onclick = () => {
  96. unityInstance.SetFullscreen(1);
  97. };
  98. }).catch((message) => {
  99. alert(message);
  100. });
  101. };
  102. document.body.appendChild(script);
  103. </script>
  104. </body>
  105. </html>

然后右边还有那个框框

 修改style就行了,添加如下代码

可直接粘贴如下代码:

  1. html,body{width:100%;height:100%;margin:0;padding:0;overflow:hidden;}
  2. .webgl-content{width: 100%; height: 100%;}
  3. .unityContainer{width: 100%; height: 100%;}

成功“”

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

闽ICP备14008679号