我使用的是unity 2019.4 LTS版
一共有两步

1 在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 unityInstance = UnityLoader.instantiate("gameContainer", "Build/DMJ_WebApp.json", {onProgress: UnityProgress});</script></head><!-- 调用 OnResize方法, 当用户改变浏览器尺寸时, 屏幕会自适应 --><body style="style="margin:0px ; padding:0px;overflow:hidden" onResize="ChangeCanvas()"><div class="webgl-content"><!-- 设置长宽都是百分百填充 --><div id="unityContainer" 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("unityInstance").style.width = window.innerWidth + "px";--><!-- document.getElementById("unityInstance").style.height = window.innerHeight + "px"; -->document.getElementById("#canvas").style.width = window.innerWidth + "px";document.getElementById("#canvas").style.height = window.innerHeight + "px";}</script></body></html>

2 修改css 的内容 位置在打包出来的文件/TemplateData/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;}

原文连接

unity webgl自适应网页尺寸相关推荐

  1. Unity WebGL打包网页端运行显示Not allowed to access uv4 on mesh

    项目场景:Unity WebGl网页连接后台服务器 提示:这里简述项目相关背景: 该项目实现的是Unity Web端联网后台读取数据主要功能 问题描述 提示:这里描述项目中遇到的问题: 在该项目中在U ...

  2. Unity WebGL 窗口自适应

    unity 打包好WebGL后,用文本编辑器编辑打包生成的 index.html 文件 在生成的html里面修改代码 <script type="text/javascript&quo ...

  3. Unity Webgl内嵌网页页面

    Unity Webgl端有时候会有这样一个需求,在Unity界面上内嵌一个网页,并且可以在界面上把这个网页关掉(不是重新打开新的标签页) 效果如下: 现在来实现这个功能: 1.在Assets文件夹下新 ...

  4. Unity导出的WebGL、网页前端、服务端以及硬件四级交互结构的实现(简单3d建模网页端物联网)

    本文代码基于一个智慧楼宇项目: 前提:(Unity应该使用尽量最新的版本,2020年的应该就行,2019年的未知',2018年的好像就不行了) 模式简介: 1.服务端处理楼宇发送的信息,把处理过的信息 ...

  5. html 电脑页面比例自适应,html网页自适应不同尺寸的电脑屏幕

    随着科技的日新月异,各种移动端层出不求,屏幕的尺寸也大小不一,一个网页要如何才能做到自适应不同尺寸的移动端呢?最简单的方法就是编写不同的网页来应对不同的移动端.但是这种方法太过于浪费资源,而且还会大大 ...

  6. Unity WebGL/WebPlayer与html通信对比,在html添加网页端按钮直接测试

    Unity WebPlayer与html通信,在html添加网页端按钮直接测试 由于现在各大浏览器的升级,UnityWebPlayer的网页方式逐渐被摒弃掉,但是很多老项目或者追求稳定的开发者还在选择 ...

  7. unity WebGL

    自加入Unity WebGL平台以来,Unity的开发团队就一直致力于优化WebGL的内存消耗.我们已经在Unity使用手册上有对于WebGL内存管理的详尽分析,甚至在Unite Europe 201 ...

  8. Unity WebGL开发

    教程来啦 一.WebGL不能干的那些事 二.注意事项 1.播放声音 2.跨域 3.发布http or https? 4.包体大小 5.Odin 6.T序列化失败 三.参考资料 四.Build 主要步骤 ...

  9. 转自CSDNattilax的专栏 :自适应网页设计 跟 响应式 设计的区别跟原理and实践总结...

    响应式Web设计(Responsive Web design)的理念是: 1 #-----------自适应布局VS响应式布局 2 设计思路Mobile First 2 #---手机and平板分辨率  ...

最新文章

  1. 线性回归csv数据集_数据科学的基石:统计学、机器学习、计算机科学(三)——线性回归...
  2. c语言 结构体练习之 实现产品销售记录的相关功能
  3. java内存区_基于jvm java内存区域的介绍
  4. java函数式编程入口_Java中的函数式编程
  5. Xutils使用详解(二)
  6. 如何用IDEA打包jar包
  7. 油猴子 Tampermonkey
  8. windows10去桌面图标小箭头和恢复小箭头
  9. nfc卡模式与标准模式_手机NFC卡模式功能是什么意思
  10. Python 爬取新浪网新闻和存取CSV文件
  11. 【Excel】提取中/英文字符
  12. vue.runtime.esm.js?2b0e:619 [Vue warn]: Duplicate keys detected: ‘xxx‘. This may cause an update err
  13. linux中使用crontab出现crontab: installing new crontab
  14. ida pro 反汇编 Android so 库后修改 arm 汇编指令的方法总结
  15. Lect5_Model_free_Control
  16. Chef 的安装与使用
  17. Channel的定义、写入、读取、关闭与遍历
  18. 魔兽分辨率修改器的C语言实现
  19. GPU直通、GPU共享及GPU虚拟化比较
  20. 一款音频功率放大器芯片

热门文章

  1. 安卓手机:无法安装该证书 因为无法读取该证书文件
  2. stata怎么判断是否存在异常值_利用统计方法,辨别和处理数据中的异常值
  3. Stata outreg2命令中(label)无法正确输出Word/Excel文档
  4. 对记忆化搜索(ms)和动态规划(dp)的深入理解
  5. EfficientNet B0-B7 网络参数
  6. hawkhost 不支持mysql_HawkHost老鹰主机账户管理常用技巧及cPanel面板基础应用
  7. 人工智能遗传算法求函数极值
  8. 金融统计分析与挖掘实战7.6-7.7
  9. Html node.js开源项目,2020年面向开发人员的十大NodeJS框架
  10. 开个贴记录23实习面试