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

这篇具有很好参考价值的文章主要介绍了Unity: WebGL发布后在浏览器上运行时窗口大小自适应。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

发布后在浏览器上按f11时的效果图

unity webgl 自适应,unity,前端,web,html

 这个效果是根据19:6(也就是1920:1080的页面大小来设计的)

整体来说修改以下两个文件来进行达成效果预览,第一个是index.html、第二个是TemplateData文件下的style.css文件

unity webgl 自适应,unity,前端,web,html

unity webgl 自适应,unity,前端,web,html

修改index.html

修改对比

unity webgl 自适应,unity,前端,web,html

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

unity webgl 自适应,unity,前端,web,html

 上图第一段代码替换

 <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文件

源代码与修改代码对比

unity webgl 自适应,unity,前端,web,html

unity webgl 自适应,unity,前端,web,html

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

 文章来源地址https://www.toymoban.com/news/detail-559161.html

到了这里,关于Unity: WebGL发布后在浏览器上运行时窗口大小自适应的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Unity 2021 打包WebGL之后,用火狐浏览器打开报错,报错信息:Uncaught ReferenceError: Runtime is not defined

    Unity 发布WebGL,使用火狐浏览器打开报错 报错信息:Uncaught ReferenceError: Runtime is not defined 查找了半天的原因,发现是使用网络通讯的问题 参考网址:https://stackoverflow.com/questions/70411564/unity-webgl-throws-error-referenceerror-runtime-is-not-defined 发现如果不是使用WebSocket写的话,会不知道如

    2024年02月05日
    浏览(41)
  • 【Unity开发小技巧】UnityWebGL打包本地浏览器运行查看

    目录 一.前言: 二.WebGL打包 三.配置web.config(重要) 四.部署IIS  五.测试 正常打包WebGL后在浏览器直接运行会报以下这个错: It seems your browser does not support running Unity WebGL content from file:// urls. Please upload it to an http server, or try a different browser. 无法打开本地链接,需把网页发布成

    2024年02月11日
    浏览(65)
  • 启动Tomcat后在浏览器输入http:\\127.0.0.1:8080连接失败。

    1.找到Tomcat文件夹下的bin文件,点击bin下的startup.bat启动Tomcat。在浏览器输入http:\\\\127.0.0.1:8080连接失败。 2.打开运行窗口,查看8080端口的占用情况。 发现8080端口被占用。解决方法一种为将该进程kill掉,这里我选择修改端口。 3. 打开Tomcat目录下的conf文件夹,打开server.xml文件

    2024年02月09日
    浏览(24)
  • 解决selenium4在部分电脑上运行时报异常GPU process launch failed: error_code=18无法打开浏览器窗口

    我在台式windows10上运行的一点问题没有的程序拷贝到同样操作系统的笔记本上运行就会报异常 刚开始一直是怀疑是没有添加禁用gpu加速参数导致,后来加上去--disable-gpu后还是报该异常。 最后将所有常用参数一个个尝试,添加--no-sandbox参数后解决。 示例:

    2024年02月11日
    浏览(33)
  • 如何在浏览器中启用 WebGL 以使用 HTML5 3D 查看器

    WebCenter 中的 HTML5 3D Collada Viewer(自 14.1 以来新增)要求在浏览器中启用 WebGL。较旧的浏览器可能不支持此功能,或者要求用户首先显式启用此功能。本页介绍如何为所有主要浏览器启用此功能。WebGL 3D 查看器 本文是以下超级用户文章的摘要:如何在浏览器中启用 WebGL? 在尝

    2024年02月10日
    浏览(35)
  • selenium+python切换浏览器窗口

    selenium在当前浏览器页面打开了新的窗口,此时如果要聚焦到新开的浏览器窗口,就需要切换到新的窗口,切换窗口的操作,具体selenium+python示例如下:

    2024年02月15日
    浏览(30)
  • Selenium(二)对浏览器窗口的操作

    Selenium可以通过WebDriver控制浏览器窗口的各种操作,包括:

    2024年02月09日
    浏览(42)
  • js获取元素到可视区的距离/浏览器窗口滚动距离/元素距离浏览器顶部距离

    1. js获取元素距离可视区的各种距离 2. js获取浏览器窗口滚动距离 3. js获取元素实际距离页面距离(包括滚动距离) (1).如果父辈元素中有定位的元素,那么就返回距离当前元素最近的定位元素边缘的距离。 (2).如果父辈元素中没有定位元素,那么就返回相对于body边缘距

    2024年02月12日
    浏览(37)
  • 补充:selenium操作已打开的浏览器窗口

    1. 打开浏览器 首先来到安装 chrome浏览器 的文件夹下,例:C:Users\\01AppDataLocalGoogleChromeApplication 在此界面打开 cmd窗口, 然后输入:chrome.exe --remote-debugging-port=9527 --user-data-dir=“F:seleniumAutomationProfile” ,并回车。 这句代码的意思是启动 chrome浏览器 的调试模式, user-data

    2023年04月09日
    浏览(33)
  • selenium+python切换浏览器窗口--详细讲解

    在浏览器页面打开窗口后,有时点击按钮会打开新的页面,我们需要切换到新的窗口才能去定位操作,不然无法操作,切换窗口代码如下

    2024年02月14日
    浏览(37)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包