Depth understanding of AssetBundle management mechanism

AssetBundle base load

By AssetBundle load resources, divided into two steps, the first step is to get AssetBundle the object, the second step is to load the resource through the object needed. And the first step is divided into two ways, in conjunction with conventional API will hereinafter be described in detail.

 

The first step in obtaining the object of common API AssetBundle

 

A way, to get WWW objects, and then get AssetBundle objects by WWW.assetBundle:

● public WWW(string url);

Bundle file to load and get WWW objects, complete will create large WebStream in memory (content after decompression, usually 4 to 5 times the size of the original file Bundle, the texture may be greater proportion of resources), subsequent AssetBundle. Load can be done directly in memory.

 

● public static WWW LoadFromCacheOrDownload(string url, int version, uint crc = 0);

Bundle file to load and get WWW objects, while extracting Bundle content in the form of a disk as a cache (if the Bundle is already in cache, eliminating the need for this step), created after the completion of only a small SerializedFile stored in memory, the subsequent AssetBundle.Load need to get from the disk cache by IO.

 

● public AssetBundle assetBundle;

After obtaining the object through the WWW before the two interfaces, you can get AssetBundle objects through WWW.assetBundle.

 

Second way, direct access AssetBundle:

● public static AssetBundle CreateFromFile(string path);

Bundle by uncompressed file, create AssetBundle synchronization object, which is the fastest way to create. It will only create smaller SerializedFile in memory after created, and the subsequent need to get AssetBundle.Load from disk by IO.

 

● public static AssetBundleCreateRequest CreateFromMemory(byte[] binary);

Bundle of binary data, asynchronous create AssetBundle object. It creates greater WebStream in memory when finished. When invoked, Bundle decompression is asynchronous, and therefore for the uncompressed file Bundle, the interface CreateFromMemoryImmediate equivalent.

 

● public static AssetBundle CreateFromMemoryImmediate(byte[] binary);

The interface is a synchronous version of CreateFromMemory.

 

Note: 5.3 respectively renamed LoadFromFile, LoadFromMemory, LoadFromMemoryAsync and increased LoadFromFileAsync, and there is a certain mechanism of change, Unity can see the official documents.

 

The second step, loading resources from common API AssetBundle

 

● public Object Load(string name, Type type);

By given name and resource type, load resources. Will automatically load when you load it depends on resources, ie Load a Prefab, automatically Load Texture resource it references.

 

● public Object[] LoadAll(Type type);

The disposable loading Bundle all resources in a given resource type.

 

● public AssetBundleRequest LoadAsync(string name, Type type);

The interface is Load asynchronous version.

 

Note: 5.x were renamed LoadAsset, LoadAllAssets, LoadAssetAsync, and increased LoadAllAssetsAsync.

 

 

Advanced load AssetBundle

 

Interface Comparison: new WWW and WWW.LoadFromCacheOrDownload

 

The former advantage

● a subsequent Load operation in memory, which compared to a smaller cost IO operations;

● does not form a cache file, while the latter requires additional disk space to store cache;

● by WWW.texture, WWW.bytes, WWW.audioClip loaded directly interfaces to external resources, the latter only for loading AssetBundle;

 

The former disadvantage

● each decompression operation involve loading, which is loaded in the second overhead eliminating the need for decompression;

● have greater WebStream in memory, while the latter only in memory generally smaller SerializedFile. (This is usually the case, but not absolute, for more information on the sequence of the Prefab, probably more cases than WebStream SerializedFile appear)

 

Memory Analysis

 

In managing AssetBundle, understand the impact of their loading process of the memory of great significance. In the figure, we have a list of resources in the middle of the loading AssetBundle, distribution of various types of objects in memory, on the left lists the generation of each type of memory involved loading API:

 

● WWW objects: 1 in the first step of the way to produce, small memory overhead;

● WebStream: When using new WWW or CreateFromMemory generated, usually large memory overhead;

● SerializedFile: In the first step will have two modes, memory overhead usually small;

● AssetBundle objects: In the first step will have two ways, small memory overhead;

● resources (including Prefab): in the second step is generated by Load according to the resource type, the size of each memory overhead;

● Scene object (GameObject): produced by Instantiate In the second step, usually small memory overhead.

 

In subsequent chapters, we will also analyze ways to uninstall the figure for all types of memory objects, thus avoiding even the residual memory leak.

 

important point

● CreateFromFile only be applied to uncompressed AssetBundle, and the next Android system StreamingAssets in the compression directory (.jar), so you need to first uncompressed AssetBundle into the SD card in order to use them CreateFromFile.

Application.streamingAsstsPath = "jar:file://" + Application.dataPath + "!/assets/";

 

● iOS system has 256 open file limit, therefore, in memory by CreateFromFile or WWW.LoadFromCacheOrDownload loaded AssetBundle objects will be lower than this value, the newer version, if LoadFromCacheOrDownload exceeds the upper limit, it is automatically replaced by new WWW form loading, and earlier versions will fail to load.

 

● CreateFromFile and WWW.LoadFromCacheOrDownload calls will increase the size of RersistentManager.Remapper, and PersistentManager responsible for maintaining the persistent storage resources, Remapper saved is loaded to the mapping between resources HeapID with the source of data FileID memory, which is a Memory Pool, Mono that behaves like a heap, only to rise, it is necessary to use these two interfaces make rational planning.

 

● Bundle pack for the presence of dependencies, the main attention in the loading order. For example, suppose CanvasA in BundleA in, rely AtlasB in BundleB, in order to ensure that resources are properly referenced, the latest creation time point AssetBundle BundleB object is instantiated before CanvasA. That is, when you create an object BundleA of AssetBundle, when Load ( "CanvasA"), BundleB of AssetBundle objects are not in memory.

 

● Based on experience, recommendations AssetBundle file size not exceeding 1MB , because under the prevailing circumstances Bundle loading times its size is not linear, too much can cause a larger load Bundle overhead.

 

● Since the loading is asynchronous WWW objects, so in the case of FIG idle CPU loading individually prone (Vsync selected account for the majority of the frame), are recommended to appropriately while loading a plurality of objects, to increase the CPU utilization , while speeding up the loading is completed.

 

 

AssetBundle Uninstall

 

Previously mentioned memory allocation when loading resources by AssetBundle, below, we combine common API to describe how to uninstall the allocated memory, and ultimately achieve the purpose of emptying all the relevant memory.

 

Memory Analysis

 

On the right side of the figure above, we list the various memory objects unload way:

● Scene object (GameObject): Such article may be unloaded by Destroy function;

 

● resources (including Prefab): In addition to the Prefab, resource files can be uninstalled in three ways:

1) by Resources.UnloadAsset uninstall the specified resource, small CPU overhead;

2) Uninstall all at once by Resources.UnloadUnusedAssets resources unreferenced, large CPU overhead;

3) Uninstall together by AssetBundle.Unload (true) when unloading AssetBundle object, load out of resources.

For Prefab, at present it can only be uninstalled by DestroyImmediate, and after unloading must be reloaded AssetBundle to reload the Prefab. Due to the small memory overhead is generally not recommended targeted unloaded.

 

● WWW objects: Dispose calling the object or function can be set to null;

 

● WebStream: after unloading AssetBundle WWW objects and corresponding object, i.e. this memory is automatically offload engine;

 

● SerializedFile: After uninstalling AssetBundle, this memory will be automatically offload engine;

 

● AssetBundle objects: AssetBundle unload in two ways:

1) by AssetBundle.Unload (false), to retain the resource is already in memory when unloading AssetBundle objects;

2) by AssetBundle.Unload (true), offload resource is already in memory of unloading AssetBundle objects, since the method is easy to cause the loss of resource references, and therefore do not recommend regular use;

 

important point

● After uninstalling AssetBundle objects through AssetBundle.Unload (false), if the load off before re-create the object and load the resource into memory, redundant, ie, the same two resources occur.

 

● static variables are referenced resource script, when calling Resources.UnloadUnusedAssets, and will not be unloaded, Profiler can be seen in its reference case.

 

 

 

UWA recommendations

 

Through the above explanation, I believe you are loading and unloading of AssetBundle have a clear understanding. Below, we will simply do what the API recommended options:

 

● The need for permanent memory Bundle files, give priority to reduce the memory footprint, so for non Prefab storage resources (especially grain) Bundle of documents, consider using WWW.LoadFromCacheOrDownload or AssetBundle.CreateFromFile load, thus avoiding the often WebStream in memory; and for more Bundle Prefab storage resources, consider using the new WWW load, because of the greater WebStream SerializedFile generated when loaded with such Bundle WWW.LoadFromCacheOrDownload may produce than the new WWW.

 

● For Bundle file that is loaded after unloading, the two cases: priority to speed (when loading scenes) and give priority to fluency (during the game) .

1) loading case scenario, be noted that WWW avoid loading caused by-object CPU is idle, consider using load faster WWW.LoadFromCacheOrDownload or AssetBundle.CreateFromFile, but the need to avoid a large number of subsequent Load operation resources , causing IO overhead (you can try to direct LoadAll).

2) a case where the game, it is desirable to avoid the use of synchronous operation causes Caton, can be considered to use the new WWW mating AssetBundle.LoadAsync smoothing resource loading, but note that for Shader, greater resources Texture , its initialization is often time consuming, prone to Caton, it is recommended that these resources will be pre-loaded when loading the scene.

 

● Bundle only in the case where encryption is required, consider CreateFromMemory, because the interface is slow loading.

 

● Try to avoid calling Resources.UnloadUnusedAssets during a game () because the interface cost is large, easily lead to Caton, try using Resources.Unload (obj) to uninstall one by one, in order to ensure the smooth flow of the game.

 

 

It should be noted that the above memory management is better suited for Unity versions prior to 5.3. Unity engine certain adjustments to AssetBundle memory footprint in 5.3, we now also further study and research.

Guess you like

Origin www.cnblogs.com/zangjiapei/p/12132270.html