Unity3d 5.3 New Assetbundle use plan and strategy

Source: Internet
Author: User
Tags unity 5

1. Overview

The assetbundle mechanism and the previous 4.x version of Unity3d 5.0 have changed a lot, and some of the once-used processes are no longer in use, and even some older APIs have been superseded by new APIs.
Therefore, the main content of this paper is analysis 5. The x version of the assetbundle mechanism (including several aspects such as creating a resource bundle, compressing a resource bundle, loading a resource bundle and loading/unloading resources from a resource bundle ) and its critical API usage, and summarizing some recommendations for the project (for example, depending on the scenario, Choose a different package body loading scheme, etc.).

Back to Directory 2. New features of the Assetbundle system

This section includes:

    • New features of the Assetbundle system
    • Advantages of the new Assetbundle system
New features for 2.1.AssetBundle systems

In the new Assetbundle system, the following new features appear:

  • With the UI in editor, you can easily tag resources for Assetbundle. The mapping of a resource and the corresponding Assetbundle will be created in the resource database (assetdatabase).
    You can specify the assetbundle described in this resource at the arrow, the first option is the name of Assetbundle, and the next option is to create a variant for Assetbundle, for example, some footage needs to be differentiated into HD or stored in a different assetbundle. Then the second option can be distinguished by HD and normal.
  • A new API is provided to set the Assetbundle that the resource belongs to:
  • Set the value of assetimporter.assetbundlename to specify the Assetbundle to which it belongs. The first name of the Assetbundle set in the UI above is to assign a value to the value, and after the resource has assetbundlename, the information is actually present in Assetdatabase.
  • In the new version, the API to create the Assetbundle file has become quite straightforward:
  • buildpipeline.buildassetbundles (): We only need to provide an address of the output assetbundle. The engine will automatically build bundles and dependencies between resources based on the Assetbundlename property of the resource (that is, the value set in the UI above).
  • Some new packaging strategies/options have been added, and some of the older policies in 4.x are turned on by default.
  • Completeassets , which is used to ensure the completeness of resources, is opened by default;
  • collectdependencies, which is used to collect resource dependencies, is turned on by default;
  • Deterministicassetbundle, which is used to maintain a fixed ID for a resource, is enabled by default;
  • Forcerebuildassetbundle, for forcing all assetbundle files to be re-punched, added;
  • Ignoretypetreechanges, used to Judge Assetbundle update, whether to ignore the typetree changes, add;
  • Appendhashtoassetbundlename, which is used to add the hash value after the Assetbundle file name, This option can be used to determine which bundle content is updated directly by the file name (4.x is generally required by comparison of binary methods, but in some cases even if the content is not changed to repackage, the bundle's binary will also change), added.
  • Chunkbasedcompression, used for compression using the LZ4 format , 5.3 new.
  • Manifest file. In the 4.x release, we usually need to maintain our own configuration files to record the dependencies between Assetbundle and use them for runtime. In the 5.x version, using the manifest file eliminates this process in the 4.x version. The manifest file is divided into two types:
  • A single bundle of manifest files, once a new Assetbundle file is created and exported, generates a. manifest file that contains information such as checksums, dependent files, and so on. So it can be used to do incremental updates.
  • In fact, when packaging, the output of the bundle in the folder will also generate a total manifest file, called the [folder name].manifest. It contains information about all the bundles in the folder, and the information that they depend on each other. So when we load bundles, we need to load the gross position manifest file to confirm the dependencies between the bundles.
  • Some APIs that dynamically load Assetbundle at run time are replaced by new APIs.
  • Assetbundle in the 4.x version. The createfromfile method became assetbundle in the 5.x version. LoadFromFile method.
  • Assetbundle in the 4.x version. The createfrommemory method became the Loadfrommemoryasync method in the 5.x version.
  • Assetbundle in the 4.x version. The createfrommemoryimmediate method became the loadfrommemory method in the 5.x version.
2.2. Advantages of the new Assetbundle system

Because of these new features provided by the engine, we no longer need the complexity of the 4.x era for packaging scripts.
At the same time, the interdependence between resources no longer requires the developer manual maintenance, has been due to improper use of pushassetdependencies/popassetdependencies and may cause dependency problems, now Unity3d has been solved for us.
And because of the introduction of the manifest file manifest, we can implement an incremental update, that is, we only need to update the changed parts, without changing the update.
To give an example:
Suppose we have a cube whose material has a material, we pack the cube and the material into Cubebundle and Materialbundle respectively, and then we modify the material on the material. In the past, we needed to repackage the cube and material separately, and now we just need to repackage the material, and the cube will not be affected.

Back to Directory 3. Creation of the Assetbundle file

This section includes:

    • Legacy API to create assetbundle files
    • New API to create a Assetbundle file
    • Project-Specific recommendations
3.1. Legacy API to create assetbundle files

In the 4.x era, the most commonly used Assetbundle packaging methods mainly include the following two:

    • Buildpipeline.buildassetbundle
      Packaging resources other than scene, supporting single and multiple resources, need to identify the resources that need to be assetbundle into the parameters of the method;
    • Buildpipeline.buildstreamedsceneassetbundle
      The scene file is packaged, and also supports single and multiple.

And in the 4.x era, packaging also needs to pay attention to the interdependence of resources between the problem. To avoid resource redundancy while increasing the flexibility of resource loading and unloading, the importance of dependency packaging is self-evident. In the old version, we can use the following two methods to implement this dependency:

    • Buildpipeline.pushassetdependencies
    • Buildpipeline.popassetdependencies

This mechanism is not difficult to understand, simply said Pushassetdependencies is to put resources into the stack, popassetdependencies is to let the resources out of the stack, each dozen a package, the engine will check the current stack of all the dependencies, to see if the same resources are already in the stack. If so, establish a dependency relationship with the assetbundle associated with it.

3.2. New API for creating Assetbundle files

In the new version, Unity3d provides us with a unique API for hitting Assetbundle packages. That

    • Buildpipeline.buildassetbundles

The

Call buildpipeline.buildassetbundles,u3d in the script automatically builds the dependencies between the bundle and the resource according to the Assetbundlename property of the resource.
The Assetbundlename of the resource can be set at the bottom of the Inpector interface of the resource, and each assetbundlename corresponds to a bundle, that is, assetbundlename the same resource is in a bundle.
If the resource you depend on has a different assetbundlename, it will automatically establish a dependency to avoid redundancy and reduce the bundle size.
Of course, in addition to specifying ASSETBUNDLENAME, we can also set another name in Inpector, the variant. When packaged, the variant is added as a suffix after assetbundlename. With the same assetbundlename, different variant bundles can be replaced with each other. After the

is set up, we just need to create a new script that expands the call to the Buildpipeline.buildassetbundles method via the editor:

using UnityEditor;public class CreateAssetBundles{  [MenuItem ("Assets/Build AssetBundles")] static void BuildAllAssetBundles () { BuildPipeline.BuildAssetBundles ("Assets/AssetBundles"); }}

The parameter of the Buildpipeline.buildassetbundles method is the exported directory of the bundle. Of course it has a lot of overloaded versions and can provide additional parameters to customize the Assetbundle to suit your needs.

3.3. Project-specific recommendations

While the new Assetbundle simplifies the process of packaging and processing resource dependencies, it introduces a new level of complexity that requires the assetbundlename of resources to be packaged for functionality.
So what we might need to do is:

    1. Provides script batch to resource settings Assetbundlename
    2. Plan the corresponding resource types for assetbundle and plan the number of Assetbundle
Back to directory 4. Compression of the Assetbundle

This section includes:

    • Compression type of Assetbundle
    • Project-Specific recommendations
Compression type of 4.1.AssetBundle

The Unity3d engine provides us with three compression strategies to handle assetbundle compression, namely:

    • LZMA format
    • LZ4 format
    • Do not compress

LZMA format:
By default, the Assetbundle generated by the package will be compressed. In u3d, the standard compression format for Assetbundle is Lzma (Lzma is a serialized stream file), so by default the outgoing Assetbundle package is in the compressed state in LZMA format, which needs to be uncompressed before using Assetbundle.
Assetbundle with LZMA format has the smallest package volume (high compression ratio), but the corresponding increase in the time of decompression.
LZ4 format:
The version after Unity 5.3 added the LZ4 format compression, due to the compression ratio of LZ4, so the volume of the compressed Assetbundle package is larger (the algorithm is based on chunk). However, the advantage of using the LZ4 format is that the time to decompress is relatively short.
To use LZ4 format compression, you only need to turn on buildassetbundleoptions.chunkbasedcompression when packaging.

BuildPipeline.BuildAssetBundles(Application.streamingAssetsPath,             BuildAssetBundleOptions.ChunkBasedCompression);

Do not compress:
Of course, we can also not compress the assetbundle. Packets that are not compressed have the largest volume, but the fastest access.
To use an uncompressed policy, simply turn on buildassetbundleoptions.uncompressedassetbundle when you are packing.

BuildPipeline.BuildAssetBundles(Application.streamingAssetsPath,                 BuildAssetBundleOptions.UncompressedAssetBundle);
4.2. Project-Specific recommendations

Assetbundle's compression strategy is not only related to the size of the package, the decompression speed of the package, but also to the API usage that assetbundle dynamically loads at runtime. Therefore, for different types of resources assetbundle to specify a compression strategy that conforms to its usage characteristics.

Back to directory 5. Loading and unloading of assetbundle

This section mainly includes:

    • New API
    • Dynamic load Mode comparison
    • Project-Specific recommendations
5.1 New API

In the 5.x version of the new Assetbundle system, some of the old dynamic loading API has been replaced by the new API, the following details:

    • Assetbundle in the 4.x version. The createfromfile method became assetbundle in the 5.x version. LoadFromFile method.
    • Assetbundle in the 4.x version. The createfrommemory method became the Loadfrommemoryasync method in the 5.x version.
    • Assetbundle in the 4.x version. The createfrommemoryimmediate method became the loadfrommemory method in the 5.x version.

As a result, the new API will be used later in this section.

5.2. Dynamic Loading mode comparison

Using Assetbundle to dynamically load resources first to get the Assetbundle object, the second step is to load the target resource from Assetbundle. So this section will focus primarily on how to get Assetbundle objects at run time, and how to load resources from Assetbundle will be analyzed in the next section.
There are two main ways to load Assetbundle objects at run time:

    • Get the WWW object First, then get the Assetbundle object by WWW.assetBundle
    • Get Assetbundle directly

Let's look at these two approaches in detail:

Get the WWW object first, and then load the Assetbundle object through WWW.assetBundle:
To get the WWW object earlier, in this way we can use the following two APIs to implement this function in the Assetbundle.

    • Public www (string url), directly call the WWW class constructor, the target Assetbundle is located in the path as its parameters, the construction of the WWW object process will load the bundle file and return a WWW object, After the completion of the memory will create a large webstream (extracted content, usually the original bundle file, the size of the five-fold, texture resource ratio may be greater), so the subsequent assetbundle.loadasset can be directly in memory.
    • Public static www loadfromcacheordownload (string url, int version, uint CRC = 0), a static method of the WWW class, Calling this method also loads the bundle file and returns a WWW object, and the difference between the previous constructor that calls www directly is that the method saves the extracted bundle contents to disk as a cache (if the bundle is already in the cache, save this step), When done, only small serializedfile are created in memory, and subsequent assetbundle.loadasset need to be fetched from the disk cache via IO.

To load the Assetbundle object directly:
In the 4.x era, we can construct the files on disk or the streams in memory by CreateFromFile or Createfrommemory methods into the Assetbundle objects we need. However, in the 5.x version, these two methods have been replaced by new LoadFromFile, Loadfrommemory methods (both of which have asynchronous versions), and there are some differences in mechanism.

    • Public static Assetbundle loadfromfile (string path, uint CRC = 0): The new load Assetbundle method from file creation and the CreateFromFile method in 4.x have some differences in mechanism, the old createfromfile must use an uncompressed bundle file to dynamically create Assetbundle objects at run time. The new LoadFromFile method does not have this requirement, it supports several of the compression formats mentioned in the previous section, which are directly loaded for the LZ compression format and the bundle files on the uncompressed disk. For bundle files that are compressed using the default LZMA compression format, this method will first unzip the bundle file before loading it behind the scenes. This is the quickest way to load assetbundle. The method is a synchronous version, as well as an asynchronous version: Loadfromfileasync.
    • Public static Assetbundle loadfrommemory (byte[] binary, uint CRC = 0): gets the binary data of the bundle from memory and creates the Assetbundle object synchronously. This method is generally used on encrypted data, after the encrypted stream data is decrypted, we can call the method to dynamically create the Assetbundle object. The method is a synchronous version, as well as an asynchronous version: Loadfrommemoryasync.

These are the methods for dynamically loading Assetbundle objects at run time. Below, we then from the load process of memory consumption angle to compare these several methods of loading Assetbundle objects, the following table is the official Chinese version of Unity3d summary.

Note??? : When using www to download a bundle, WebRequest also has a 8*64KB buffer for storing data from the socket.

5.3. Project-specific recommendations

As a result of the above analysis of several loading methods have various use scenarios and characteristics. It is therefore recommended that you use these methods in our projects in the following scenarios:

    • Assetbundle (typically located in the Streamingassets folder) that is published with the game:
    • When you hit the Assetbundle package, Use the LZ4 compression format to package (turn on buildassetbundleoptions.chunkbasedcompression). The
    • is loaded using the LoadFromFile method when the Assetbundle object needs to be loaded at run time. The advantage of
    • is that you can compress the Assetbundle file, take into account the load speed, and save memory.
    • As the update package, the assetbundle that needs to be downloaded from the server:
    • is compressed using the default LZMA format when the Assetbundle package is hit. The
    • uses the WWW.LoadFromCacheOrDownload method to download and cache the Assetbundle package file. The benefit of
    • is that gets the maximum compression rate, which reduces the amount of data transferred during the download process. At the same time, after the local disk creates the cache, it can also take into account the load speed and save memory.
    • Our own encrypted Assetbundle:
    • When you hit the Assetbundle package, Use the LZ4 compression format to package (turn on buildassetbundleoptions.chunkbasedcompression). The
    • is loaded using the Loadfrommemory method when the Assetbundle object needs to be loaded at run time. (This is also the only use scenario for loading Assetbundle objects from memory using streaming data.)
    • Our own compressed Assetbundle:
    • We can also use a third-party library or tool to compress the generated assetbundle package files, if this is required, Then we'd better not use Unity3d to compress assetbundle, so choose to turn on Buildassetbundleoptions.uncompressedassetbundle when packaging.
    • asynchronously loads using the Loadfromfileasync method when the Assetbundle object needs to be loaded at run time.
Back to Catalog 6. Loading and unloading of resources

This section includes:

    • Load a resource from a Assetbundle object
    • Unloading of resources
6.1. Loading resources from the Assetbundle object

There have been some changes to the API names for the new and older versions of load and unload resources, but the mechanism has not changed much.
In the old 4. In the X version, the APIs used to load resources from the Assetbundle object mainly include the following:

    • Load: Load the specified resource from the resource bundle
    • Loadall: Load all resources in the current resource bundle
    • LoadAsync: Loading resources asynchronously from a resource bundle

In the new version of Assetbundle, the API for loading resources has become the following:

    • Loadasset: Load the specified resource from the resource bundle
    • Loadallasset: Load all resources in the current resource bundle
    • Loadassetasync: Loading resources asynchronously from a resource bundle
6.2. Resource Offload

The unload portion of the resource is not changing much, and the Unload method is still used.

    • Unload

This method unloads all the resources contained in the bundle in memory when the payload is loaded.
When the passed-in parameter is true, not only the resources contained in the in-memory Assetbundle object are destroyed. In-game objects that are instantiated from these resources are destroyed as well.
When the passed-in parameter is false, only the resources contained by the Assetbundle object in memory are destroyed

Unity3d 5.3 New Assetbundle use plan and strategy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.