当前位置:   article > 正文

Unity打包WebGL后 自适应网页分辨率_unity 2021webgl 窗口化自适应打包

unity 2021webgl 窗口化自适应打包

使用版本:Unity2022.3.14

效果: 可自适应 ,留白为黑色

视频效果:

2024-03-21-16-46-25-CSDN直播

坑1

top left 无效,无法居中显示游戏内容

原因; position 不能是 static 改为absolute

  1. <canvas id="unity-canvas"
  2. style=" position: absolute; width: 1920px; height: 1080px;">
  3. </canvas>

坑2

留白改为黑色

  1. <style>
  2. html
  3. {
  4. background-color: black;
  5. }
  6. </style>

修改步骤

1 自适应代码块('unity-canvas'拼写需要和上发一样 不然无效)

  1. <script>
  2. var unityContainer = document.getElementById('unity-canvas');
  3. function unityContainerResize()
  4. {
  5. var w = window.innerWidth || document.body.clientWidth,
  6. h = window.innerHeight || document.body.clientHeight,
  7. ratio = 16 / 9,
  8. r = w/h;
  9. var setW, setH, setTop, setLeft;
  10. if (r >= ratio) { // 高度撑满
  11. setW = h * ratio;
  12. setLeft = (w - setW) / 2;
  13. } else { // 宽度撑满
  14. setH = w / ratio;
  15. setTop = (h - setH) / 2;
  16. }
  17. unityContainer.style.width = (setW || w) + 'px';
  18. unityContainer.style.height = (setH || h) + 'px';
  19. unityContainer.style.top = (setTop || 0) + 'px';
  20. unityContainer.style.left = (setLeft || 0) + 'px';
  21. }
  22. window.addEventListener('resize', unityContainerResize)
  23. unityContainerResize()
  24. </script>

2 修改了positon属性为absolute

  1. <canvas id="unity-canvas"
  2. style=" position: absolute; width: 1920px; height: 1080px;">
  3. </canvas>

3 修改默认 留白为留黑

  1. <style>
  2. html
  3. {
  4. background-color: black;
  5. }
  6. </style>

完整代码

在unity打包后的文件夹下面名字是index.html

按照上方修改步骤进行

  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 | TestWebGL</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" style=" position: absolute; width: 1920px; height: 1080px;"></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. // 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 + "/TestWebGL.loader.js";
  58. var config = {
  59. dataUrl: buildUrl + "/TestWebGL.data",
  60. frameworkUrl: buildUrl + "/TestWebGL.framework.js",
  61. codeUrl: buildUrl + "/TestWebGL.wasm",
  62. streamingAssetsUrl: "StreamingAssets",
  63. companyName: "DefaultCompany",
  64. productName: "TestWebGL",
  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. // Mobile device style: fill the whole browser client area with the game canvas:
  76. var meta = document.createElement('meta');
  77. meta.name = 'viewport';
  78. meta.content = 'width=device-width, height=device-height, initial-scale=1.0, user-scalable=no, shrink-to-fit=yes';
  79. document.getElementsByTagName('head')[0].appendChild(meta);
  80. }
  81. loadingBar.style.display = "block";
  82. var script = document.createElement("script");
  83. script.src = loaderUrl;
  84. script.onload = () => {
  85. createUnityInstance(canvas, config, (progress) => {
  86. progressBarFull.style.width = 100 * progress + "%";
  87. }).then((unityInstance) => {
  88. loadingBar.style.display = "none";
  89. }).catch((message) => {
  90. alert(message);
  91. });
  92. };
  93. document.body.appendChild(script);
  94. </script>
  95. <script>
  96. // 自适应代码块
  97. var unityContainer = document.getElementById('unity-canvas');
  98. function unityContainerResize()
  99. {
  100. var w = window.innerWidth || document.body.clientWidth,
  101. h = window.innerHeight || document.body.clientHeight,
  102. ratio = 16 / 9,
  103. r = w/h;
  104. var setW, setH, setTop, setLeft;
  105. if (r >= ratio) { // 高度撑满
  106. setW = h * ratio;
  107. setLeft = (w - setW) / 2;
  108. } else { // 宽度撑满
  109. setH = w / ratio;
  110. setTop = (h - setH) / 2;
  111. }
  112. unityContainer.style.width = (setW || w) + 'px';
  113. unityContainer.style.height = (setH || h) + 'px';
  114. unityContainer.style.top = (setTop || 0) + 'px';
  115. unityContainer.style.left = (setLeft || 0) + 'px';
  116. }
  117. window.addEventListener('resize', unityContainerResize)
  118. unityContainerResize()
  119. </script>
  120. </body>
  121. <style>
  122. html
  123. {
  124. background-color: black;
  125. }
  126. </style>
  127. </html>

声明

代码借鉴的不同帖子的代码非完全原创
仅仅作为记录!

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

闽ICP备14008679号