Unity中ab包压缩方案 LZMA 和LZ4

文章对比了LZMA和LZ4两种压缩算法在Unity中的应用。LZMA提供高压缩率但可能导致加载时卡顿和内存浪费,而LZ4以更快的读取速度和高效内存管理为优势。通过切换到LZ4并利用块压缩,可以实现更流畅的游戏体验,同时通过弱引用管理减少资源浪费。建议使用BuildAssetBundleOptions.ChunkBasedCompression进行配置,并利用AssetBundle.LoadFromFileAsync优化加载过程。

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

LZMA


1.LZMA采用流压缩方式(stream-based),压缩率会比LZ4更高,体现在包体更小,但是问题也很严重。LZMA只支持顺序读取,所以加载AB包时,需要将整个包解压,会造成卡顿和额外内存占用。这也是为什么在有些复杂UI上首次打开会造成卡顿。

2.加载AB包后将所有资源进行了缓存,导致了如果AB包资源利用率在短时间利于率不高的时候,造成了很高的内存浪费。

3.一套引用计数规则非常复杂,当资源过多的时候建立引用关系都是很费时的,其中的常驻包的设置逻辑也是非常具有不确定性。

LZ4优化


1.LZ4采用块压缩方式(chunk-based),块压缩的数据被分为大小相同的块,被分别压缩,虽然压缩率不及LZMA,但是读取效率比LZMA高非常多

2.LZ4压缩的AB包,使用LoadFromFile()或LoadFromStream()只会加载AB包的Header,相比于直接加载解压整块AB包,效率更进一步提高。另外一个很重要的点,由于可以只加载Header,因此AB包可以做到一旦加载到内存后就再不卸载,此时只需要管理从AB包中读取出来的资源的生命周期。

3.对于之前使用引用计数的优化,由于Unity原本资源管理就是使用引用计数去维护,这里再建立一套内部的引用计数,不仅多余而且很浪费CPU资源,而且效果不一定很好。这个时候我们可以建立一套弱引用管理体系,通过弱引用去持有资源,在触发Resource.UnloadUnusedAssets()再去清除弱引用失效的对象。
 

将LZMA的压缩方式修改为LZ4

将打包参数添加BuildAssetBundleOptions.ChunkBasedCompression即可

BuildAssetBundleOptions buildOption = BuildAssetBundleOptions.IgnoreTypeTreeChanges | BuildAssetBundleOptions.DeterministicAssetBundle | BuildAssetBundleOptions.ChunkBasedCompression;

LZ4读取AB包

//读取ab包
AssetBundle.LoadFromFileAsync(url)

这里需要注意一点的就是,LoadFromFileAsync与WWW或UnityWebRequest区别在于是在windows下不需要file://前缀

This is a shared library for Android, iOS, OSX, Windows, Linux and webGL to decompress 7z (7zip) files and to compress/decompress zip/gzip (.zip/.gz), LZ4 (.lz4), brotli (.br), fastLZ files and buffers. ZIP plugin: iOS/tvOS compilation may require to add the -lz linking flag at Build Settings-> Linking- > Other Linker flags on xcode. webGL for flz,lzma & lz4 supports buffers compression/decompression only. Brotli supports buffer decompression. webGL for zip supports all functions except those that require file system operations. 7ZIP section: The library serves the scope to have fast decompression of 7z files and compress/decompress lzma files and buffers. - The library does 7z decompression and not 7z compression. Compression of lzma alone files is supported. Passwords are not supported. - It is about 2.5x times faster then using a c# implementation for 7z decompression. - You can extract a single file out of the 7z archive. - If you intend to decompress large files it would be better to use the largeFiles flag.(consumes less ram) - You can extract the contents of the 7z file keeping its folder structure. - Ability to get the filenames and file sizes of files in a 7z archive. - get progress of extraction when the 7zip archive has multiple files. - get byte level progress of 7z decompression (single or multiple files). - get byte level progress of lzma compression/decompression. - Ability to encode/decode to/from .lzma alone format. - Ability to decode a specific file in a 7z archive to a byte buffer. - Ability to decode/encode a byte buffer to/from the lzma alone format. - Ability to cancel the decompression when the 7z archive has multiple entries.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值