Unity AssetBundle resource loading

Common loading methods of AssetBundle (2018.4)

AssetBundle.LoadFromFile
AssetBundle.LoadFromFileAsync
AssetBundle.LoadFromMemory
AssetBundle.LoadFromMemoryAsync
AssetBundle.LoadFromStream
AssetBundle.LoadFromStreamAsync

Official API link

LoadFromFile:

Load an AssetBundle from the file on the hard disk. If the AB package is loaded in LZ4 mode, it will only load the header of the AB package, and then load that part of the AB package chunk after what resources are needed.

Advantages: fast loading speed, small memory usage. Disadvantage: Loading encrypted AB package may fail.

LoadFromMemory:

Create an AssetBundle from the memory area, and the AB package can be completely loaded through byte[].

Advantage: the encrypted AB package can be loaded through byte[]. Disadvantage: high memory usage, which will occupy two copies of memory.

LoadFromMemory:

Loads an AssetBundle from a managed stream, the data is provided by a managed Stream object.

Pros: Seek() and Read() can be called from any Unity native thread. Disadvantages: There are many restrictions.

BuildAssetBundleOptions

None = 0:								默认方式。使用LZMA压缩算法,该算法压缩后包体很小,但是加载的时候需要花费很长的时间解压。
备注:第一次解压之后,该包又会使用LZ4压缩算法再次压缩。这就是为什么第一次加载时间长,之后加载时间就没那么长了。
(LZMA需要完整解压之后才能加载包内资源,LZ4不需要完整解压就可以加载包内资源。)
UncompressedAssetBundle = 1:			不压缩。虽然包体大,但是加载快。
CompleteAssets = 4:					强制包含整个资产。
DisableWriteTypeTree = 8:				不包含TypeTree信息。虽然可以使得AB包更小,但是对低版本不兼容。
DeterministicAssetBundle = 16:			创建一个哈希来映射存储在AB包里的对象的id。
ForceRebuildAssetBundle = 32:			强制重建AB包。
IgnoreTypeTreeChanges = 64:			当做增量构建检测时,忽略type tree的变化。
AppendHashToAssetBundleName = 128:		添加哈希到AB包名。
ChunkBasedCompression = 256:			使用基于块的LZ4压缩算法。
StrictMode = 512:						如果在构建时有任何错误,则不允许构建成功。
DryRunBuild = 1024:					干构建。
DisableLoadAssetByFileName = 4069:		禁止AB包通过文件名加载资源。
DisableLoadAssetByFileNameWithExtension = 8192:禁止AB包通过文件扩展名加载资源。

The difference between LZMA and LZ4 compression algorithm

LZMA is a stream compression method (stream-based). Stream compression uses the same dictionary when processing the entire data block, which provides the greatest possible compression ratio, but only supports sequential reads. Therefore, when loading the AB package, the entire package needs to be decompressed, which will cause freezes and additional memory usage. The advantage is that the AB bag is small.

LZ4 is a block compression method (chunk-based). Data for block compression is divided into equal-sized blocks and compressed separately. If you need to decompress random reads in real time, block compression is a better choice. The advantage is that the compression ratio is high and the loading speed is fast.

Projects can formulate different strategies to choose compression methods according to their own needs.

Guess you like

Origin blog.csdn.net/qq_33808037/article/details/120079454