Unity WebGL 项目, 屏幕自适应

https://www.jianshu.com/p/cb3189cce7e8

Unity WebGL 项目, 屏幕自适应

96 tackor 关注

2018.04.10 17:27 字数 297 阅读 1278评论 0喜欢 2

Unity WebGL 项目, 屏幕自适应

Unity 2017.4.0f1

2018.4.10

Snip20180410_8.png

如图所示, 使用火狐浏览器打开 Unity 编译的 Webgl 工程, 发现Unity没有做屏幕适配(我最初的尺寸是(375x1334).

在网上寻找了许久才找到 比较有用的文章: http://www.manew.com/thread-113162-1-1.html

虽然这篇文章不是十分符合我的需求, 但是它让我了解了 webgl 项目中这些文件之间的关系. 最好根据自己的一点点html的知识,自己总结如下

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 中调用 OnResize方法, 这样在PC端使用浏览器查看时, 当用户改变浏览器尺寸时, 屏幕自适应 -->
  <body onResize="ChangeCanvas()">
    <div class="webgl-content">

      <!-- 在这里设置游戏的 整个界面是100%填充设备 -->
      <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项目文件中

style.css 路径: webgl项目/TemplateData/style.css

如果你需要修改Logo, 滑动条等, 也在 webgl项目/TemplateData路径下

浏览器打不开 webgl项目, 你可能需要下载个火狐浏览器, 直接把本地的webgl项目中的index.html文件拖到服务器里就可以播放了

如果想把 webgl项目方到网上去, 那么需要一个服务器(比如阿里云), 这些不是一句话可以说清楚的, 你需要自己慢慢摸索,慢慢积累.

good luck!

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值