Unity3D:资源包的压缩(Asset Bundle Compression)

2024-06-19 19:38

本文主要是介绍Unity3D:资源包的压缩(Asset Bundle Compression),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Unity支持三种资源包的压缩方式:LZMA,LZ4和不压缩。

LZMA格式

资源包默认编译压缩格式的。标准压缩格式使用的是单一的LZMA算法的序列化的数据文件流,使用前需要获取完整的数据再解压缩。

LZMA压缩包下载容量最小,但会导致解压变慢和加载时间更长。

LZ4格式

Unity还支持LZ4压缩,会导致较大的压缩文件,但是这种方式不要求完整的数据包就可以解压缩。LZ4算法是“基于块”的,因此当对象从一个LZ4压缩包加载时,仅用于该对象的相应块会被解压。这发生在瞬间,意味着使用前无需解压完整的资源包。LZ4格式是在Unity 5.3引入,以前的版本不可用。

不压缩格式

第三个压缩选项是不压缩。未压缩的资源包很大,但一旦下载后则访问最快。

压缩包的缓存

WWW.LoadFromCacheOrDownload 函数下载并缓存资源包到磁盘上,极大的加快了加载速度。从Unity 5.3开始,缓存数据也可以使用LZ4算法压缩。相比未压缩的包它能节省40%60%的空间。在下载过程中同时进行压缩,因此最终用户几乎不会察觉。当数据从网络到达,Unity将解压并以LZ4格式重新压缩它。在下载数据流同时重新压缩,这意味着一旦数据足够就马上进行缓存压缩,并一直持续增长到下载完成。在此之后,数据在需要时会从高速缓存的包中即时解压并读取。

缓存压缩默认是启用的,由属性Caching.compressionEnabled 控制。它同时影响缓存到磁盘和存储在内存中的资源包。

AssetBundle加载API概述

此表提供了使用不同的压缩类型和不同的加载方法时内存和性能开销的比较。

 

无压缩

块压缩(LZ4

流压缩(LZMA

WWW *

内存:未压缩的包的大小+(当WWW未释放时,未压缩的包的大小)。性能:没有额外的开销。

内存:LZ4HC压缩的包的大小+(当WWW未释放时,LZ4HC压缩的包的大小)。性能:没有额外的开销。

内存:LZ4压缩的包的大小+(当WWW未释放时,LZMA压缩的包的大小)。性能:LZMA解压+ 在下载过程中LZ4压缩。

LoadFromCacheOrDownload

内存:没有额外的开销。性能:从磁盘读取。

内存:没有额外的开销。性能:从磁盘读取。

内存:没有额外的开销。性能:从磁盘读取。

LoadFromMemory(异步)

内存:未压缩的包的大小。性能:没有额外的开销。

内存:LZ4HC压缩的包的大小。性能:没有额外的开销。

内存:LZ4压缩的包的大小。性能:LZMA压缩+ LZ4压缩。

LoadFromFile(异步)

内存:没有额外的开销。性能:从磁盘读取。

内存:没有额外的开销。性能:从磁盘读取。

内存:LZ4压缩的包的大小。性能:从磁盘读取+ LZMA压缩+ LZ4压缩。

WebRequest的(也支持缓存)

内存:未压缩的包的大小。性能:没有额外开销[+是否从磁盘读取缓存]。

内存:LZ4HC压缩的包的大小。性能:没有额外的开销[+是否从磁盘读取缓存]。

内存:LZ4压缩的包的大小。性能:下载过程中LZMA压缩+ LZ4压缩[+是否从磁盘读取缓存]。

*当使用WWW下载资源包,WebRequest还需要一个从socket读取数据的8x64KB的累加器缓存。

因此,使用在游戏中使用低级加载API时遵循以下原则:

1.    为游戏部署资源包时使用StreamingAssets:使用BuildAssetBundleOptions);使用基于块的压缩方式来构建包和资源包使用LoadFromFileAsync来加载它。这使您的内存数据压缩和加载速度几乎等于读取缓存。

2.    下载用作DLC的资源包使用默认的编译选项(LZMA压缩)和LoadFromCacheOrDownload / WebRequest来下载和缓存它。这样你可以有最好的压缩比,并得到资源包。LoadFromFile加载性能进一步提升。

3.    加密包使用BuildAssetBundleOptions;使用基于块的压缩方式,并使用LoadFromMemoryAsync加载(通常这是唯一方案)。

4.    自定义压缩使用BuildAssetBundleOptions;使用非压缩方式来构建资源包。用自定义的算法解压之后使用LoadFromFileAsync加载包。

你应该使用异步函数,因为他们不阻塞主线程,并且能让加载更高效和操作队列化。绝对避免在同时使用同步和异步函数这可能会在主线程上产生"打嗝"。

兼容性

资源包容器格式发生了变化,来支持新的压缩类型,并且为进一步改进提供基础。Unity5还支持由Unity 4创建的包,但是更早期版本(2.X,3.X)不支持。

 

---------------------------------------------------- 以下是原文 ----------------------------------------------------

 

Asset Bundle Compression

Unity supports three compression options for Asset Bundles: LZMA, LZ4, and Uncompressed.

LZMA Format

By default, when Asset Bundles are built, they are stored in a compressed format. The standard compressed format is a single LZMA stream of serialized data files, and needs to be decompressed in its entirety before use.

LZMA-Compressed bundles give the smallest possible download size, but has relatively slow decompression resulting in higher apparent load times.

LZ4 Format

Unity also supports LZ4 compression, which results in larger compressed file sizes, but does not require the entire bundle to be decompressed before use. LZ4 is a “chunk-based” algorithm, and therefore when objects are loaded from an LZ4-compressed bundle, only the corresponding chunks for that object are decompressed. This occurs on-the-fly, meaning there are no wait times for the entire bundle to be decompressed before use. The LZ4 Format was introduced in Unity 5.3 and was unavailable in prior versions.

Uncompressed Format

The third compression option is no compression at all. Uncompressed bundles are large, but are the fastest to access once downloaded.

Caching of Compressed Bundles

The WWW.LoadFromCacheOrDownload function downloads and caches asset bundles to disk and thus greatly speeds up loading afterwards. From Unity 5.3 onwards, cached data can also be compressed with the LZ4 algorithm. This saves 40%–60% of space compared to uncompressed bundles. Recompression happens during download and thus is almost unnoticeable by the end users. As data arrives from the socket, Unity will decompress it and recompress it in LZ4 format. This recompression occurs during the download streaming, which means the cache compression begins as soon as enough of the data is downloaded, and continues incrementally until the download is complete. After that, data is read from the cached bundle by decompressing chunks on-the-fly when needed.

Cache compression is enabled by default and is controlled by the Caching.compressionEnabled

  property. It affects bundles cached to disk and stored in memory.

AssetBundle load API overview

This table provides a comparison of memory and performance overheads when using different compression types and different loading methods.

 

Uncompressed

Chunk Compressed (LZ4)

Stream Compressed (LZMA)

WWW *

Memory: uncompressed bundle size + (while WWW is not disposed, uncompressed bundle size). Performance: no extra processing.

Memory: LZ4HC compressed bundle size + (while WWW is not disposed, LZ4HC compressed bundle size). Performance: no extra processing.

Memory: LZ4 compressed bundle size + (while WWW is not disposed, LZMA compressed bundle size). Performance: LZMA decompression + LZ4 compression during download.

LoadFromCacheOrDownload

Mem: no extra memory is used. Perf: reading from disk.

Mem: no extra memory is used. Perf: reading from disk.

Mem: no extra memory is used. Perf: reading from disk.

LoadFromMemory (Async)

Mem: uncompressed bundle size. Perf: no extra processing.

Mem: LZ4HC compressed bundle size. Perf: no extra processing.

Mem: LZ4 compressed bundle size. Perf: LZMA decompression + LZ4 compression.

LoadFromFile(Async)

Mem: no extra memory is used. Perf: reading from disk.

Mem: no extra memory is used. Perf: reading from disk.

Mem: LZ4 compressed bundle size. Perf: reading from disk + LZMA decompression + LZ4 compression.

WebRequest (also supports caching)

Mem: uncompressed bundle size. Perf: no extra processing [+reading from disk if cached].

Mem: LZ4HC compressed bundle size. Perf: no extra processing [+reading from disk if cached].

Mem: LZ4 compressed bundle size. Perf: LZMA decompression + LZ4 compression during download [+reading from disk if cached].

* When downloading a bundle using WWW, WebRequest there is also an 8x64KB accumulator buffer which stores data from a socket.

Thus, use the following guidelines when using low-level loading API in your games:

1.    Deploying asset bundles with your game as StreamingAssets - use BuildAssetBundleOptions.ChunkBasedCompression when building bundles and AssetBundle.LoadFromFileAsync to load it. This gives you data compression and the fastest possible loading performance with a memory overhead equal to read buffers.

2.    Downloading asset bundles as DLCs - use default build options (LZMA compression) and LoadFromCacheOrDownload/WebRequest to download and cache it. Here you’ll have the best possible compression ratio and AssetBundle.LoadFromFile loading performance for further loads.

3.    Encrypted bundles - choose BuildAssetBundleOptions.ChunkBasedCompression and use LoadFromMemoryAsync for loading (this is pretty much the only scenario where LoadFromMemory[Async] should be used).

4.    Custom compression - use BuildAssetBundleOptions.UncompressedAssetBundle to build and AssetBundle.LoadFromFileAsync to load a bundle after it was decompressed by your custom compression algorithm.

You should generally always choose asynchronous functions - they don’t stall the main thread and they allow loading operations to be queued more efficiently. And absolutely avoid calling synchronous and asynchronous functions at the same time - this might introduce hiccups on the main thread.

Compatibility

The Asset Bundle container format was changed in order to support new compression type, and to provide basis for further improvements. Unity 5 still supports bundles created in Unity 4, however bundles created in earlier version (2.x, 3.x) are not supported.

这篇关于Unity3D:资源包的压缩(Asset Bundle Compression)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1075956

相关文章

一份LLM资源清单围观技术大佬的日常;手把手教你在美国搭建「百万卡」AI数据中心;为啥大模型做不好简单的数学计算? | ShowMeAI日报

👀日报&周刊合集 | 🎡ShowMeAI官网 | 🧡 点赞关注评论拜托啦! 1. 为啥大模型做不好简单的数学计算?从大模型高考数学成绩不及格说起 司南评测体系 OpenCompass 选取 7 个大模型 (6 个开源模型+ GPT-4o),组织参与了 2024 年高考「新课标I卷」的语文、数学、英语考试,然后由经验丰富的判卷老师评判得分。 结果如上图所

加载资源文件失败

背景         自己以前装了一个海康的深度学习算法平台,试用期是一个月,过了一个月之后,因为没有有效注册码或者加密狗的支持了导致无法使用,于是打算卸载掉,在卸载一个软件的时候,无论是使用控制面板还是软件自带的卸载功能,总是卸载不掉,提示“加载资源文件失败”。该软体主要包括以下两部分: 用自带卸载功能卸载的时候分别提示如下:     用控制面板卸载的时候反应很慢,最后也是提示这个

模型压缩综述

https://www.cnblogs.com/shixiangwan/p/9015010.html

【鸿蒙】ERROR_GET_BUNDLE_INSTALLER_FAILED

错误信息 [ERROR_GET_BUNDLE_INSTALLER_FAILED] Troubleshooting guide $ hdc file send D:\Huawei\devEcoProjects\entry\build\default\outputs\default\entry-default-unsigned.hap /sdcard/e8a215ea7be1444197e6a58e

红队内网攻防渗透:内网渗透之内网对抗:横向移动篇Kerberos委派安全RBCD资源Operators组成员HTLMRelay结合

基于资源的约束委派(RBCD)是在Windows Server 2012中新加入的功能,与传统的约束委派相比,它不再需要域管理员权限去设置相关属性。RBCD把设置委派的权限赋予了机器自身,既机器自己可以决定谁可以被委派来控制我。也就是说机器自身可以直接在自己账户上配置msDS-AllowedToActOnBehalfOfOtherIdentity属性来设置RBCD。 所以核心就是谁或什么权限能修改

按顺序加载外部资源js css

cmmon.js /*** 公共js文件* 内置参数说明:* locationUrl:硬盘路径(开发模式使用)* ytRootUrl:系统根目录,引入该js文件后可直接使用* initJq:自定义jq文件的目录,如果需要引用不同的jq文件,请修改目录。* initCssUrl:初始化css文件容器,如果需要新增或修改引入的c

蔚蓝资源包和数据分析

代码如下 /* ==================================* COMPUTER GENERATED -- DO NOT EDIT* ==================================*/#include <windows.h>static FARPROC __Init_Fun_2__;int __RestartAppIfNecessary__Fun(

vue项目开启Gzip压缩配置方法及性能优化建议

原文地址:https://jingyan.baidu.com/album/454316ab29d0c0f7a7c03a1f.html?picindex=1   vue 项目开启gzip压缩和部署 nginx 开启gzip优化性能   第一步,在vue项目中安装依赖并将productionGzip改为true,开启Gzip压缩: npm install --save-dev compres

批量压缩文件夹内文件并记录解压密码

主要功能是: 遍历指定目录中的所有文件。 为每个文件生成一个随机密码。 使用生成的密码将文件压缩为 7z 格式。 将文件名和对应的密码记录到 passwords.txt 文件中。 import osimport randomimport stringfrom py7zr import SevenZipFile, exceptions# 设置文件夹路径folder_path = r'D:

20240621日志:大模型压缩-从闭源大模型蒸馏

目录 1. 核心内容2. 方法2.1 先验估计2.2 后验估计2.3 目标函数 3. 交叉熵损失函数与Kullback-Leibler(KL)损失函数 location:beijing 涉及知识:大模型压缩、知识蒸馏 Fig. 1 大模型压缩-知识蒸馏 1. 核心内容 本文提出在一个贝叶斯估计框架内估计闭源语言模型的输出分布,包括先验估计和后验估计。先验估计的目的是通