unity5 's Assetbundle modification is relatively large, so the first suggestion Is: forget the previous usage and come back! You know, Unity5 has no way to load 2.x 3.x bundle package ...
So the improvement is that doesn't need us to manage referential relationships, we don't need to manage reference relationships, We don't need to manage the citation relationship! The can be packaged separately without the need for additional associated code, and for common resources, you only need to package the public resources separately.
Test case:
Packing method |
Results |
Analysis |
CUBE1 and Cube2 are packaged separately |
Cube1.assetbundle 53k Cube2.assetbundle 53k |
Texture Repeat Packaging |
cube1, Cube2 are packaged separately texture individually packaged |
cube1.assetbundle 9k P align= "left" >cube2.assetbundle 9k < Span style= "font-family: ' Microsoft Yahei ';" >texture.assetbundle 48k |
texture has only one copy, and does not require code to manually use texture for Cube1 settings, |
first, Resource Packaging
(1) set the tag for the resource to be packaged, indicating the corresponding package name:
You can use the code to set the package name in bulk (lower case only), roughly as Follows:
Assetimporter ai= = = xxx;
(2) Generate Assetbundle Resource bundle , packaging function simplification, The main parameters only need one output address:
Assetbundlemanifest buildassetbundles (string outputPath, buildassetbundleoptions assetbundleoptions, Buildtarget targetplatform);
Calling this function, unity is automatically packaged according to the label of the resource, and is incrementally packaged ,
A. Bundles that have no change to the resource will not trigger repackaging;
B. The resource has not changed, even if the bundle package under the generated directory is deleted, unity will not be repackaged;
C. The bundle package corresponding to the generated directory manifase is deleted and will be repackaged;
D. You can use the Buildassetbundleoptions.forcerebuildassetbundle parameter to trigger forced repackaging.
In addition, Unity provides another version of the wrapper function:
Assetbundlemanifest buildassetbundles (string outputPath, assetbundlebuild[] builds, Buildassetbundleoptions assetbundleoptions, Buildtarget targetplatform);
This function provides the ability to package generation resources without relying on resource tags and by substituting pure code.
(3) Generated Bundle Package Resource Directory parsing :
is the resource directory structure generated for cube.prefab:
streamingassets: a assetbundle package, Contains the assetbundlemanifest type of asset, which records all bundle bundles and dependencies between each other.
streamingassets.manifest: Global manifest , The name of the global manifest and package generated directory has the same name, not fixed, which is generated in the Streamingassets directory.
cube.assetbundle: resource Bundle.
Cube.assetbundle.manifest: each Resource's own manifest , which corresponds to bundle one by one, is used only for incremental build, which is not required at All.
two, compressed format
(1) LZMA : default compression format, compression ratio is large, save space, need to extract the entire compressed package before use;
(2) LZ4 : 5.3 version added, 40%–60% compression ratio, Turn on buildassetbundleoptions.chunkbasedcompression packaging Parameters.
LZ4 algorithm is "block-based", so when an object is compressed LZ4 from a package, only the corresponding block for that object is Decompressed. No need to unzip the entire package.
so LZ4 and non-compressed resources can be confused and read directly from the resources in the Package.
(3) uncompress : do not compress, access the fastest, fee space.
lzma:1.45m |
Lz4:2m |
uncompress:4m |
LZ4 format Compression ratio is also possible, and loading speed is also very fast, is the best solution in most cases.
three,assetbundle load mode comparison
The main loading methods are the following four kinds, the first two also have the function of downloading, the latter two have the corresponding asynchronous Interface:
(1) WWW: Only go memory, memory decompression, not cache;
(2) loadfromcacheordownload: Go to the local cache, no download and decompression (and then LZ4 compression), there is the use;
If there is no cache, for uncompressed and LZ4 compressed Assetbundle packages, unity will directly copy them into the cache directory, for Lzma compressed, it will be decompressed and then re-compressed into LZ4 format, and then cache it. You can control whether the cache is compressed through Caching.compressionenabled.
(3) loadfromfile: The fastest way, different from the 4.x version, you can use compressed resources directly;
if it is uncompress or LZ4, read directly from disk
if it is lzma, it will be extracted to memory and then read
(4) loadfrommemory: Loaded from memory, generally used to encrypt resources.
suggestions for how to use:
(1) streamingassets with package resources:
uncompressed or LZ4 compression: loadfromfile;
Lzma compression: can be decompressed to a local disk using WWW.LoadFromCacheOrDownload.
(2) Hot update resources: lzma+www.loadfromcacheordownload+caching.compressionenabled;
(3) encrypt resources: lz4+loadfrommemory;
(4) Own compressed resources: uncompressassetbundle assetbundle pack + own algorithm compression +loadfromfileasync.
four, Resource Offload
assetbundle.unload (false) : Kill the package, the bundle is no longer available, It is no longer possible to pass Bundles. Load loads the resource;
assetbundle.unload (true) : Kill the compressed package, and all the resources from the load (load) from it.
(1) assetbundle.unload (false) combined with resource.unloadunusedassets ()
(2) fragmentation using assetbundle.unload (true)
V. Project recommendations
(1) for shader in bundles, you need to avoid repeating packaging:
Scenario: CUBE1 uses its own shader standard
Packing method |
Results |
Analysis |
Cube1 individually packaged, shader not in the bag |
Cube1.assetbundle 9k |
|
Cube1 individually packaged, shader into the bag |
Cube1.assetbundle 118k |
You can see the standard this shader compiled is also quite large |
For shader be sure to pay attention to repeating packaging, either use the shader configured in always includedshaders, or the shader will be packaged separately (this scenario has not been tried)
(2) Memory Analysis:
A assetbundle compression package is not big, so at the same time cache 100 does not release is not much (4.x this thing is still very large, can not cache too much).
Assetbundle created, will be in the not Saved/assetbundle grouping under a bundle, size 4.1k:
After Loadasset (cube1), there will be cube1 generated asset under assets in multiple places:
after testing, the relevant memory occupied by assetbundle, as long as the management of good references, can be fully recovered.
(3) Resource Lod
A resource can be set to two tags, the first parameter assetbundlename represents the package name, and the second parameter assetbundlevariant the LOD used to do the Resource.
Suppose there are two sets of map resources, a set of hd, a set of normal, and then give them the same assetbundlename for myasset, HD resources assetbundlevariant set to ld, normal resources set to sd, then, depending on the device by choosing to load myasset.ld or MYASSET.SD to switch the library, so rely on Myasset prefab can dynamically switch versions.
Note that the asset of the two sets of resources must correspond exactly to one by one, and for Unity LD and SD are the same bundld, and only one of them can be loaded at the same time.
(4) Because packaging greatly simplifies, did not specify the process of mainasset, so Assetbundle.mainasset has been unable to use , very worried mulberry.
(5) WWW.LoadFromCacheOrDownload A frame will only have one bundle complete download
Assetbundle bundle = www.assetBundle;
Accessing the Www.assetBundle property is the synchronization of extracting and creating assetbundle from www
Unity5 assetbundle Series--basic Process