Unity Assetbundles and Resources guide (iii) Assetbundle Foundation

Source: Internet
Author: User

The content of this article is mainly translated from the following article

https://unity3d.com/cn/learn/tutorials/topics/best-practices/guide-assetbundles-and-resources?playlist=30089 A Guide to Assetbundles and Resources

Part III Assetbundle Foundation

3.1 Overview

The Assetbundles system provides a means to archive one or more asset files and to be indexed by Unity. The goal is to distribute the data that can be compatible with the Unity serialization system, which is the primary tool for updating Non-code content after Installation. This makes it possible to reduce the size of the installation package and Run-time memory Pressure. As well as the optional load optimized content based on terminal equipment is known as Feasible.

Understanding the Assetbundle workflow is key to building a successful unity mobile device Project.

3.2 What is Assetbundle

A assetbundle consists of two parts: the head and the data Section.

The head is generated by unity when building Assetbundle. Store Assetbundle information, such as the Assetbundle tag, whether the Assetbundle is compressed and a Sub-list. A list is a lookup table that has the name of an object as an Index. Each lookup item provides a byte index of the data segment position of an object given the Name. The data segment contains all the serialized raw data for the Assets. If the data segment is Compressed. The LZMA is compressed throughout the data segment. The first is to serialize all the assets, then compress the entire byte Stream.

unity5.3, The assetbundle cannot be compressed inside a single object. If you are using a version prior to 5.3, if you want to read one or more obect,unity, unzip the entire assetbundle. In general, unity caches uncompressed versions to improve load efficiency later.

5.3 Added LZ4 compression Selection. Assetbundle constructed with the LZ4 option is compressed in a single object. Yes load a single object without extracting the entire assetbundle.

3.3 Assetbundle Manager

Unity has developed a assetbundle open source reference implementation that can be downloaded from Https://bitbucket.org/Unity-Technologies/assetbundledemo .

3.4 Loading Assetbundle

Inside the Unity5, assetbundle can load Assetbundle via 4 apis. These four APIs will behave differently with the following two scenarios

    1. Assetbundle is Lzma compressed or LZ4 compressed or uncompressed
    2. Loading the Assetbundle platform

These four APIs are assetbundle.loadfrommemoryasync, loadfromfile,

Loadfromcacheordownload and Unitywebrequest's Downloadhandlerassetbundle.

3.4.1 Loadfrommemoryasync

Unity does not recommend using this Api. Before 5.3.2 This API was named Createfrommemory. The name is different but the function is the Same.

This method loads the assetbundle from the managed byte array. He always copies the managed byte array to the cost body byte array First. If it is Lzma compressed, the copy of the time to understand the Pressure. If not compressed in the original copy.

This API requires at least twice times the size of the Assetbundle memory. The asset loaded from Assetbundle will be copied 3 times in memory: one managed byte array, one local memory copy and one time It's asset. GPU or system memory Footprint.

3.4.2 LoadFromFile

5.2 ago called createfromfile, the name is not the same, but the function is the Same.

This API is very efficient if you are loading an uncompressed assetbundle. If it is uncompressed or LZ4 compressed, the API behaves as Follows:

The API on mobile devices only loads header Information. Only a single object in load will load a particular object. No other memory is Wasted.

The entire assetbundle is loaded into the memory in the Editor.

Note If the version is 5.3 or older on an Android device, loading from the streamassts directory will Fail. Because the content is inside a zipped jar Package. 5.4 Fixes this bug later.

3.4.3 Loadfromcacheordownload

This is a useful API if you are loading resources from a remote server. If it is loaded from the local file system, it can be in File://URL form. If the content already exists in the local cache. This API is the same as Loadfromfile.

If there is no cache, the content will be read from the source, and if it is compressed, he will unzip it with a worker thread and write it into the Cache. Once cached, it is an uncompressed assetbundle.

3.4.4 Assetbundledownloadhandler

5.3 After the introduction of the mobile platform Api. More resilient than www. Allows the developer to specify if the downloaded data is processed in order to eliminate unnecessary memory Consumption.

3..4.5 recommendations

In general, use the LoadFromFile API as much as Possible. He is more effective faster. If you need to download resources from remote, 5.3 or newer version is recommended with unitywebrequest, the old version with WWW.

3.5 Loading asset from Assetbundle

You can load object:loadasset, loadallasset, and Loadassetwithsubasset from Assetbundle with three different apis. These APIs all have asynchronous Versions. The synchronization API will be at least 1 frames faster than async. In the 5.1 or older version you can say So. Because only one object is loaded in one frame in these versions. This means that the asynchronous API version that loads more than one object is much slower than the corresponding synchronous Version. This issue was fixed after 5.2. You can load more than one object inside a frame, loading a time slice that you want to see Set.

You should use Loadallassets when you want to load multiple unrelated objects. however, It is only used when most or all of the load is Loaded. It's a little faster than the other two api,loadall versions. But if there is a lot of resources in the assetbundle, not more than two-thirds to Load. It is recommended that you re-split the assetbundle to multiple smaller assetbundle, and then call the Loadall Version.

Loading details:

The load is not run on the main thread. Where data reads are performed on a worker Thread. 5.3 Before loading objects are executed sequentially, and some functions are performed on the main thread. When the worker thread finishes reading the Data. He pauses execution to allow the main thread to integrate integration (integration) until the main thread has been integrated. After 5.3, object loading can be parallel. Multiple objects can then be deserialized, processed, and consolidated on the working THREAD. When the object finishes Loading. The awake callback executes and is then available in the next Frame.

3.5.2 Assetbundle Dependent

Unity5 's Assetbundle System. Assetbundle dependencies can be tracked automatically by two different apis. In the editor, dependencies can be queried through the Assetdatabase Api. Assetbundle allocations and dependencies can be accessed and changed through the Assetimporter Api. The runtime can be accessed through the Assetbundlemanifest API of the Scriptableobject SUBCLASS.

If an object of Assetbundle is referenced to one or more other Assetbundle objects, we are called Assetbundle Dependencies. As described in the first section, Assetbundle can be used as the data source for the local ID and GUID of the object contained within it.

Because object is loaded only if the instance ID is referenced for the first Time. And when the Assetbundle is loaded, it is given a correct instance ID. So the order of Assetbundle loading is Irrelevant. It is important to first load the assetbundle that it depends on before loading an object. Unity does not automatically load all dependent assetbundle, which is the responsibility of the Developer.

For example, a material a refers to a map B. A pack into the Assetbundle1,b and pack into the AssetBundle2. in this use case, AssetBundle2 must be loaded before loading material a from AssetBundle1. But this does not have to AssetBundle2 than AssetBundle1 load First.

Unity does not automatically load AssetBundle2 when loaded AssetBundle1. this must be manually loaded by the Script. and the API to load AssetBundle1 and 2 is Irrelevant. Regardless of which API you LOAD.

3.5.3 Assetbundle List

When generating assetbundle through buildpipeline, unity generates a dependency information into a single assetbundle. In the common parent directory where all the Assetbundles are Stored. He has a assetbundlemanifest type in it. It provides a Getallaseetbundles API to query all Assetbundle. The getalldependencies returns all Dependencies. Includes a dependency dependency, dependency dependency, and so On. Getdirectdependencies only returns direct Dependencies. 、

3.5.4 Recommendations

It is recommended that only the required objects be Loaded. Especially on mobile platforms, because their local storage reads are very slow, and loading and unloading of object triggers garbage Collection.

Unity Assetbundles and Resources guide (iii) Assetbundle Foundation

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.