Unity打包WebGL的优化常用操作?

文章探讨了WebGL应用的优化方法,包括贴图格式的简化以减小包体,比较了gzip和Brotli压缩的优缺点及适用场景,提出了在服务器端配置加速解压缩以提升启动速度的方案,并建议调整WEBGLPlayerSetting以降低后台运行需求,从而提高性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.贴图部分优化

如果贴图格式时2048,在不影响画面效果的情况下,改成1024或者5#12,还可以缩小包体。

2.压缩和解压缩问题

WebGL打包的时候分三种压缩情况:
gzip:比Brotli文件打,但打包快,http和https都支持
Brotli:压缩格式最小,打包慢,只有谷歌和火狐支持。
Disabled:不压缩。

3.不使用文件压缩,在 http 协议层使用gzip压缩

直接打包一份不压缩的版本,在Publishing Settings 中的Compression Format选择Disabled。

测试打开本地场景的时间,是否比gzip或brotli压缩快了。

本地不需要下载WebGl的项目,而且跳过了解压的过程,减少了时间消耗。

4.提升解压缩速度

  1. 若使用gzip格式压缩,可以通过配置服务器,来加快解压缩的时间,从而减少启动时间。
    然后创建.htaccess文件,放到Build子文件夹中。
# This configuration file should be uploaded to the server as "<Application Folder>/Build/.htaccess"
# NOTE: "mod_mime" Apache module must be enabled for this configuration to work.
<IfModule mod_mime.c>

# The following lines are required for builds without decompression fallback, compressed with gzip
RemoveType .gz
AddEncoding gzip .gz
AddType application/octet-stream .data.gz
AddType application/wasm .wasm.gz
AddType application/wasm .wasm
AddType application/javascript .js.gz
AddType application/octet-stream .symbols.json.gz

# The following lines are required for builds without decompression fallback, compressed with Brotli
RemoveType .br
RemoveLanguage .br
AddEncoding br .br
AddType application/octet-stream .data.br
AddType application/wasm .wasm.br
AddType application/javascript .js.br
AddType application/octet-stream .symbols.json.br

# The following line improves loading performance for uncompressed builds
AddType application/wasm .wasm

# Uncomment the following line to improve loading performance for gzip-compressed builds with decompression fallback
#如果是gzip就取消注释下面这行
# AddEncoding gzip .unityweb
# AddEncoding gzip .wasm

# Uncomment the following line to improve loading performance for brotli-compressed builds with decompression fallback
#如果是br就取消注释下面这行
# AddEncoding br .unityweb

</IfModule>
  1. 在server.xml端Connector模块内加入以下代码
//启用压缩 默认不启用
 compression="on" 
 //进行压缩的最小值,低于该值的文件不进行压缩(单位B,默认2048,即默认2k)
compressionMinSize="2048"
# 不需要进行压缩的浏览器,当浏览器类型为指定的类型时,不进行压缩
noCompressionUserAgents="gozilla,traviata"
# 需要压缩的文件类型(多个类型以逗号分隔)
compressableMimeType="text/html,text/xml,image/png,text/css"
  1. conf目录下的web.xml文件,如果有些文件,你是希望下载下来,而不是用应用程序来打开,就这样配置
    application/octet-stream

5.设置WEBGL PLayer Setting

如果没有必须后台运行的必要就关闭后台运行P->Resolution and Presentation ->Run In BackGround

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值