当前位置:网站首页>Unity: WebGL发布后在浏览器上运行时窗口大小自适应
Unity: WebGL发布后在浏览器上运行时窗口大小自适应
2022-07-17 07:13:00 【Uu_hua】
发布后在浏览器上按f11时的效果图

这个效果是根据19:6(也就是1920:1080的页面大小来设计的)
整体来说修改以下两个文件来进行达成效果预览,第一个是index.html、第二个是TemplateData文件下的style.css文件


修改index.html
修改对比
其中上图一段注释的脚本是webgl进入全屏状态的的脚本,就是显示下图的脚本(去掉就不显示下面的东西,不去掉就显示下面的东西),要不要注释都一样没有什么影响。

上图第一段代码替换
<div id="unityContainer" style="width: 100%; height: 100%;"></div>第二段代码替换
<script>
var gameContainer=document.getElementById('unityContainer');
function gameContainerResize(){
var w=window.innerWidth || document.body.clientWidth,
h=window.innerHeight || document.body.clientHeight,
ratio = 16/9,
r=w/h;
var setW,setH,setTop,setLeft;
if(r>=ratio){
setW=h*ratio;
setLeft=(w-setW)/2;
}
else{
setH=w/ratio;
setTop=(h-setH)/2;
}
gameContainer.style.width=(setW || w)+'px';
gameContainer.style.height=(setH || h)+'px';
gameContainer.style.top=(setTop || 0)+'px';
gameContainer.style.left=(setLeft || 0)+'px';
}
window.addEventListener('resize',gameContainerResize);
gameContainerResize();
</script>修改style.css文件
源代码与修改代码对比


html.body{height: 100%; width:100%;margin:0;}
.webgl-content * {border: 0; margin: 0; padding: 0}
.webgl-content {position: relative; height: 100%; width:100%; overflow:hidden;background-color:#333}
.webgl-content canvas{height:100%!important;width:100%!important}
.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');}
.webgl-content .footer{display:none}
.webgl-content .footer {margin-top: 5px; 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;}
以上就是webgl自适应浏览器大小的全过程了
总结
以上作为笔记作用,方便以后忘记了可以作为参考!!!
参考文献https://blog.csdn.net/weixin_43392473/article/details/124553861
边栏推荐
- Ccf-csp "202206-2 - treasure hunt! Adventure!"
- 通过Dao投票STI的销毁,SeekTiger真正做到由社区驱动
- [C language] user defined type details: structure, enumeration, union
- Database review -- database recovery technology
- From the casino logic, analyze the investment value of platform currency 2020-03-03
- CCF-CSP《202206-2—寻宝!大冒险!》
- 数据库写入优化:分库分表及相关问题
- Real case: how to check the soaring usage of CPU after the system goes online?
- redis6新功能
- What if the user information in the website app database is leaked and tampered with
猜你喜欢

Jira --- workflow call external api
![[kernel] character device that drives development and learning](/img/99/2eaed37078c3245be29d82382cfd59.png)
[kernel] character device that drives development and learning

一款关于日常习惯打卡的小程序

redis事务

数据库复习--数据库恢复技术

Detailed explanation of type, user-defined type, preliminary understanding of structure

Redis master-slave replication

V8 引擎如何进行垃圾内存的回收?
![[C # variable constant keyword] - variable constants and keywords in C #](/img/9b/433f9110d9c7599d8beac8288ea409.png)
[C # variable constant keyword] - variable constants and keywords in C #

【flask入门系列】异常处理
随机推荐
从 B 站崩溃报告看分布式系统的技术栈
代码学习(DeamNet)CVPR | Adaptive Consistency Prior based Deep Network for Image Denoising
Bean、
DP动态规划企业级模板分析(数字三角,上升序列,背包,状态机,压缩DP)
Precautions for MySQL statements
The website vulnerability repair service provider analyzes the ultra vires caused by controllable parameters
Discussion sur la technologie RISC - V
Ku115 FPGA high performance 10G Optical fiber network hardware accelerator card / 2-way 10G Optical fiber data accelerator card
RISC-V技術雜談
[C# Console]-C# 控制臺類
Discussion on risc-v Technology
真实案例:系统上线后Cpu使用率飙升如何排查?
Can seaport and erc-4907 become new ways to release NFT liquidity| Tokenview
地址监控API:如何追溯与监控Uniswap黑客地址
總結的太好了!終於有人把SQL的各種連接Join都講明白了
Jira --- workflow call external api
Detailed explanation of type, user-defined type, preliminary understanding of structure
Local storage sessionstorage
Classic general pbootcms flower website template source code, adaptive mobile terminal, with background management
Do online usdt and usdc want to be short? Take you to find out | tokenview
