当前位置:网站首页>unity webgl自适应网页尺寸
unity webgl自适应网页尺寸
2022-07-06 18:43:00 【吴梓穆】
我使用的是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 onResize="ChangeCanvas()">
<div class="webgl-content">
<!-- 设置长宽都是百分百填充 -->
<div id="gameContainer" 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;}
边栏推荐
- leetcode:736. LISP syntax parsing [flowery + stack + status enumaotu + slots]
- The mega version model of dall-e MINI has been released and is open for download
- 张平安:加快云上数字创新,共建产业智慧生态
- Argo workflows source code analysis
- Infrared camera: juge infrared mag32 product introduction
- String or binary data will be truncated
- Douban average 9 x. Five God books in the distributed field!
- C#/VB.NET 删除Word文档中的水印
- C#/VB.NET 删除Word文檔中的水印
- postgresql之整體查詢大致過程
猜你喜欢

The mega version model of dall-e MINI has been released and is open for download

Jacob Steinhardt, assistant professor of UC Berkeley, predicts AI benchmark performance: AI has made faster progress in fields such as mathematics than expected, but the progress of robustness benchma

猿桌派第三季开播在即,打开出海浪潮下的开发者新视野

Blackfly s usb3 industrial camera: buffer processing

C#/VB. Net to delete watermarks in word documents

【服务器数据恢复】raid损坏导致戴尔某型号服务器崩溃的数据恢复案例

How to build a 32core raspberry pie cluster from 0 to 1

New generation cloud native message queue (I)

leetcode:736. Lisp 语法解析【花里胡哨 + 栈 + 状态enumaotu + slots】

Web3对法律的需求
随机推荐
建議收藏!!Flutter狀態管理插件哪家强?請看島上碼農的排行榜!
Lombok同时使⽤@Data和@Builder 的坑
Decryption function calculates "task state and lifecycle management" of asynchronous task capability
传感器:DS1302时钟芯片及驱动代码
Recent applet development records
组合导航:中海达iNAV2产品描述及接口描述
FLIR blackfly s usb3 industrial camera: white balance setting method
Blackfly s usb3 industrial camera: buffer processing
Web3的先锋兵:虚拟人
ZABBIX 5.0: automatically monitor Alibaba cloud RDS through LLD
Integrated navigation: product description and interface description of zhonghaida inav2
张平安:加快云上数字创新,共建产业智慧生态
Argo workflows source code analysis
postgresql之整體查詢大致過程
老板被隔离了
The cities research center of New York University recruits master of science and postdoctoral students
张平安:加快云上数字创新,共建产业智慧生态
解密函数计算异步任务能力之「任务的状态及生命周期管理」
【Unity】升级版·Excel数据解析,自动创建对应C#类,自动创建ScriptableObject生成类,自动序列化Asset文件
Processus général de requête pour PostgreSQL