当前位置:网站首页>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
边栏推荐
- WPF 三维应用搭建(基础)
- Paddleserving服务化部署 tensorrt报错, shape of trt subgraph is [-1,-1,768],
- The website vulnerability repair service provider analyzes the ultra vires caused by controllable parameters
- 深度学习之 7 深度前馈网络
- [C# 变量常量关键字]- C# 中的变量常量以及关键字
- 如何将读取列表中的str转化为float
- 812. 最大三角形面积
- Redis transaction
- 深度学习之 7 深度前馈网络2
- By voting for the destruction of STI by Dao, seektiger is truly community driven
猜你喜欢
Excellent résumé! Enfin quelqu'un a compris toutes les connexions SQL
Code learning (deamnet) CVPR | adaptive consistency prior based deep network for image learning
凭借左程云(左神)的这份 “程序员代码面试指南”我入职了字节
才意识到自己“奇葩”的360,会不会有些晚?
DP动态规划企业级模板分析(数字三角,上升序列,背包,状态机,压缩DP)
“韭菜”是怎么把钱送给“镰刀”的? 2020-03-07
《牛客刷题》sql错题集
一款关于日常习惯打卡的小程序
数组习题三
Redis distributed lock
随机推荐
OI回忆录
Look back at the paper after reading the code: yolov3 reread
redis分布式锁
mysql语句的注意事项
Seaport 以及 ERC-4907 能否成为释放NFT流动性的新途径?| Tokenview
一款关于日常习惯打卡的小程序
C # read and write txt files
SCA在得物DevSecOps平台上应用
Junit5
从赌场逻辑,分析平台币的投资价值 2020-03-03
[C # variable constant keyword] - variable constants and keywords in C #
Redis data persistence
凭借左程云(左神)的这份 “程序员代码面试指南”我入职了字节
Error received from peer ipv4/Connection reset by peer Paddleserving服务化部署后报错
High performance integrated video image processing board based on ultrascale FPGA + Huawei Hisilicon arm / fpga+arm
深度学习之 7 深度前馈网络2
地址监控API:如何追溯与监控Uniswap黑客地址
DP dynamic planning enterprise level template analysis (Digital triangle, rising sequence, knapsack, state machine, compressed DP)
總結的太好了!終於有人把SQL的各種連接Join都講明白了
redis集群