Unity WebGL 項(xiàng)目, 屏幕自適應(yīng)

Unity WebGL 項(xiàng)目, 屏幕自適應(yīng)

Unity 2017.4.0f1

2018.4.10

Snip20180410_8.png

如圖所示, 使用火狐瀏覽器打開 Unity 編譯的 Webgl 工程, 發(fā)現(xiàn)Unity沒有做屏幕適配(我最初的尺寸是(375x1334).

在網(wǎng)上尋找了許久才找到 比較有用的文章: http://www.manew.com/thread-113162-1-1.html

雖然這篇文章不是十分符合我的需求, 但是它讓我了解了 webgl 項(xiàng)目中這些文件之間的關(guān)系. 最好根據(jù)自己的一點(diǎn)點(diǎn)html的知識,自己總結(jié)如下

index.html

<!DOCTYPE html>
<html lang="en-us">

  <head>
    <meta charset="utf-8">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Unity WebGL Player | DMJ</title>
    <link rel="shortcut icon" href="TemplateData/favicon.ico">
    <link rel="stylesheet" href="TemplateData/style.css">
    <script src="TemplateData/UnityProgress.js"></script>  
    <script src="Build/UnityLoader.js"></script>

    <script>
      var gameInstance = UnityLoader.instantiate("gameContainer", "Build/DMJ_WebApp.json", {onProgress: UnityProgress});
    </script>

  </head>

  <!-- body 中調(diào)用 OnResize方法, 這樣在PC端使用瀏覽器查看時, 當(dāng)用戶改變?yōu)g覽器尺寸時, 屏幕自適應(yīng) -->
  <body onResize="ChangeCanvas()">
    <div class="webgl-content">

      <!-- 在這里設(shè)置游戲的 整個界面是100%填充設(shè)備 -->
      <div id="gameContainer" style="width:100%; height:100%"></div>

      <!-- 如果你不需要顯示底部的, 直接注釋掉就好了 -->
      <div class="footer">
        <div class="webgl-logo"></div>
        <div class="fullscreen" onclick="gameInstance.SetFullscreen(1)"></div>
        <div class="title">DMJ</div>
      </div>
      
    </div>

    <script type="text/javascript">
      function ChangeCanvas()
      {
         document.getElementById("gameContainer").style.width = window.innerWidth + "px";
         document.getElementById("gameContainer").style.height = window.innerHeight + "px";
         document.getElementById("#canvas").style.width = window.innerWidth + "px";
         document.getElementById("#canvas").style.height = window.innerHeight + "px";
      }
    </script>
  </body>

</html>

style.css

.webgl-content * {border: 0; margin: 0; padding: 0}

/*添加 width: 100%; height: 100%;*/
.webgl-content {position: absolute; top: 50%; left: 50%; width: 100%; height: 100%; -webkit-transform: translate(-50%, -50%); transform: translate(-50%, -50%);}

.webgl-content .logo, .progress {position: absolute; left: 50%; top: 50%; -webkit-transform: translate(-50%, -50%); transform: translate(-50%, -50%);}
.webgl-content .logo {background: url('progressLogo.Light.png') no-repeat center / contain; width: 154px; height: 130px;}
.webgl-content .progress {height: 18px; width: 141px; margin-top: 90px;}
.webgl-content .progress .empty {background: url('progressEmpty.Light.png') no-repeat right / cover; float: right; width: 100%; height: 100%; display: inline-block;}
.webgl-content .progress .full {background: url('progressFull.Light.png') no-repeat left / cover; float: left; width: 0%; height: 100%; display: inline-block;}

.webgl-content .logo.Dark {background-image: url('progressLogo.Dark.png');}
.webgl-content .progress.Dark .empty {background-image: url('progressEmpty.Dark.png');}
.webgl-content .progress.Dark .full {background-image: url('progressFull.Dark.png');}

/*如果你要保存 footer模塊, 然后又要讓footer顯示在最頂部,這樣處理*/
.webgl-content .footer {margin-top: -45px; margin-left: 5px; margin-right: 5px; z-index: 1; position: relative; height: 38px; line-height: 38px; font-family: Helvetica, Verdana, Arial, sans-serif; font-size: 18px;} 
.webgl-content .footer .webgl-logo, .title, .fullscreen {height: 100%; display: inline-block; background: transparent center no-repeat;} 
.webgl-content .footer .webgl-logo {background-image: url('webgl-logo.png'); width: 204px; float: left;}
.webgl-content .footer .title {margin-right: 10px; float: right;}
.webgl-content .footer .fullscreen {background-image: url('fullscreen.png'); width: 38px; float: right;}
Snip20180410_9.png

其他需要注意的

index.html 路徑: webgl項(xiàng)目文件中

style.css 路徑: webgl項(xiàng)目/TemplateData/style.css

如果你需要修改Logo, 滑動條等, 也在 webgl項(xiàng)目/TemplateData路徑下

瀏覽器打不開 webgl項(xiàng)目, 你可能需要下載個火狐瀏覽器, 直接把本地的webgl項(xiàng)目中的index.html文件拖到服務(wù)器里就可以播放了

如果想把 webgl項(xiàng)目方到網(wǎng)上去, 那么需要一個服務(wù)器(比如阿里云), 這些不是一句話可以說清楚的, 你需要自己慢慢摸索,慢慢積累.

good luck!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容