Unity: WebGL发布后在浏览器上运行时窗口大小自适应

发布后在浏览器上按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

 

  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity WebGLUnity 引擎的一个功能,它可以将 Unity 游戏发布为网页应用程序。当我们在使用 Unity WebGL 构建网页游戏时,我们是希望游戏可以适应不同类型和尺寸的浏览器。 在 Unity WebGL 中,实现自适应浏览器的关键是使用适当的布局和缩放策略。首先,我们需要确保游戏能够自动适应不同浏览器窗口的尺寸。我们可以使用 CSS 或 JavaScript 代码来检测浏览器窗口的大小并相应地调整 Unity 游戏的大小。 其次,我们还可以使用 CSS 或 JavaScript 来控制 Unity 游戏的缩放。这样可以确保游戏在不同设备上都能够以正确的比例显示,从而避免因屏幕分辨率不同而导致的图像模糊或变形。 另外,Unity WebGL 还支持在游戏加载之前,显示一个适配的预加载画面。这样可以增加用户体验,同时也给游戏加载所需时间提供了一个反馈。 还有一些其他的技巧可以帮助我们实现 Unity WebGL自适应浏览器。例如,使用响应式设计来确保游戏界面在不同屏幕尺寸下的可用性和易用性。此外,使用合适的字体大小和按钮大小也可以提供更好的用户体验。 总的来说,Unity WebGL 可以通过使用适当的布局和缩放策略,以及其他一些技巧,实现游戏在不同浏览器中的自适应。这样,无论用户在使用哪种类型的浏览器,游戏都能够以最佳的方式呈现给他们,提供良好的用户体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值