当前位置:网站首页>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;}
边栏推荐
- Stm32f4 --- PWM output
- Tiflash source code reading (IV) design and implementation analysis of tiflash DDL module
- 3D激光SLAM:Livox激光雷达硬件时间同步
- The boss is quarantined
- How to build a 32core raspberry pie cluster from 0 to 1
- [paper reading | deep reading] anrl: attributed network representation learning via deep neural networks
- Integrated navigation: product description and interface description of zhonghaida inav2
- Argo workflows source code analysis
- Recommended collection!! Which is the best flutter status management plug-in? Please look at the ranking list of yard farmers on the island!
- Blackfly S USB3工业相机:缓冲区处理
猜你喜欢

Data connection mode in low code platform (Part 1)

解密函数计算异步任务能力之「任务的状态及生命周期管理」

Big guys gather | nextarch foundation cloud development meetup is coming!

强化学习如何用于医学影像?埃默里大学最新《强化学习医学影像分析》综述,阐述最新RL医学影像分析概念、应用、挑战与未来方向

3--新唐nuc980 kernel支持jffs2, Jffs2文件系统制作, 内核挂载jffs2, uboot网口设置,uboot支持tftp

Blackfly S USB3工业相机:缓冲区处理

#夏日挑战赛#数据库学霸笔记(下)~

云原生混部最后一道防线:节点水位线设计

fiddler的使用

新一代云原生消息队列(一)
随机推荐
C#/VB. Net to delete watermarks in word documents
#yyds干货盘点# 解决名企真题:最大差值
将截断字符串或二进制数据
Zhang Ping'an: accelerate cloud digital innovation and jointly build an industrial smart ecosystem
[paper reading | deep reading] anrl: attributed network representation learning via deep neural networks
低代码平台中的数据连接方式(上)
Draco - gltf model compression tool
【LeetCode】Day97-移除链表元素
压缩 js 代码就用 terser
MFC Windows 程序设计[147]之ODBC数据库连接(附源码)
激光雷达:Ouster OS产品介绍及使用方法
Detailed explanation of line segment tree (including tested code implementation)
FLIR blackfly s industrial camera: explanation and configuration of color correction and code setting method
Argo workflows source code analysis
使用Ceres进行slam必须要弄清楚的几个类和函数
1500万员工轻松管理,云原生数据库GaussDB让HR办公更高效
Schedulx v1.4.0 and SaaS versions are released, and you can experience the advanced functions of cost reduction and efficiency increase for free!
张平安:加快云上数字创新,共建产业智慧生态
4 -- Xintang nuc980 mount initramfs NFS file system
[C # notes] reading and writing of the contents of text files