AssetBundle Miscellaneous

Source: Internet
Author: User

First, let's take a look at the sample code of the official Build AssetBundle:

// C # Example // Builds an asset bundle from the selected objects in the project view. // Once compiled go to "Menu"-> "Assets" and select one of the choices // to build the Asset Bundleusing UnityEngine; using UnityEditor; public class ExportAssetBundles {[MenuItem ("Assets/Build AssetBundle From Selection-Track dependencies")] static void ExportResource () {// Bring up save panelstring path = EditorU Tility. SaveFilePanel ("Save Resource", "", "New Resource", "unity3d"); if (path. Length! = 0) {// Build the resource file from the active selection. object [] selection = Selection. getFiltered (typeof (Object), SelectionMode. deepAssets); BuildPipeline. buildAssetBundle (Selection. activeObject, selection, path, BuildAssetBundleOptions. collectDependencies | BuildAssetBundleOptions. completeAssets); Selection. objects = selection;} [MenuItem ("Assets/Build AssetBundle From Selection-No dependency Tracking ")] static void ExportResourceNoTrack () {// Bring up save panelstring path = EditorUtility. saveFilePanel ("Save Resource", "", "New Resource", "unity3d"); if (path. length! = 0) {// Build the resource file from the active selection. BuildPipeline. BuildAssetBundle (Selection. activeObject, Selection. objects, path );}}}
Let's take a look at the official explanation:

  1. Uild AssetBundle From Selection-Track dependencies. this will build the current object into an asset bundle and include all of its dependencies. for example if you have a prefab that consists of several hierarchical layers then it will recursively add all the child objects and components to the asset bundle.

  2. Build AssetBundle From Selection-No dependency tracking. This is the opposite of the previous method and will only include the single asset you have selected.

In fact, the No dependency tracking section in the official sample code is problematic. If you use this official code, whether or not you choose to automatically package dependencies, the dependent content is packaged into the final assetBundle. This is because the default BuildPipleLine. BuildAssetBundle interface BuildAssetBundleOptions parameter is: BuildAssetBundleOptions. CollectDependencies | BuildAssetBundleOptions. CompleteAssets. Let's look at the official definition:

Static function BuildAssetBundle (mainAsset: Object, assets: Object [], pathName: string, assetBundleOptions: BuildAssetBundleOptions = BuildAssetBundleOptions. collectDependencies | BuildAssetBundleOptions. completeAssets, targetPlatform: BuildTarget = BuildTarget. webPlayer): bool;

Therefore, if we do not want to automatically package dependencies, we need to manually import BuildAssetBundleOptions. CollectDependencies without BuildAssetBundleOptions, as shown in

BuildPipeline. BuildAssetBundle (Selection. activeObject, Selection. objects, path, BuildAssetBundleOptions. CompleteAssets );

Let's take a look at the official definitions of BuildAssetBundleOptions. CollectDependencies and BuildAssetBundleOptions. CompleteAssets.

BuildAssetBundleOptions. CollectDependencies

Includes all dependencies.

This follows all references to any assets, game objects or components and events des them in the build.

BuildAssetBundleOptions. CompleteAssets

Forces validation Sion of the entire asset.

For example if you pass a Mesh into the BuildPipeline. BuildAssetBundle function and use CompleteAssets it wocould also include the game object and any animation clips in the same asse

These two parameters are easy to confuse. CollectDependencies here mainly refer to the items on which the packaged object depends. For example, a Prefab may have references to another prefab, shader and so on. CompleteAssets refers to packaging the entire asset during assetbundle. What is the concept here? For example, we have an Fbx model that includes mesh and animation. If we select mesh when packaging assetbundle, if we input CompleteAssets, the entire Fbx file will be packaged. If not passed in, only the Mesh will be packaged.


Generally, when we package assetbundle, the dependent files will be automatically packaged. However, there will be a problem, that is, if multiple objects are referenced to the same dependent object, there will be multiple dependent objects, affecting both the package body and memory. Here we can use the dependency management provided by Unity for packaging. Let's take a look at the official sample code.

Using UnityEngine; using UnityEditor; public class Exporter: MonoBehaviour {[MenuItem ("Assets/Export all asset bundles")] static void Export () {BuildAssetBundleOptions options = BuildAssetBundleOptions. collectDependencies | BuildAssetBundleOptions. completeAssets | BuildAssetBundleOptions. deterministicAssetBundle; BuildPipeline. pushAssetDependencies (); BuildPipeline. buildAssetBundle (AssetDatabase. loadMainAssetAtPath ("Assets/ShadersList. prefab "), null," WebPlayer/ShadersList. unity3d ", options); BuildPipeline. pushAssetDependencies (); BuildPipeline. buildAssetBundle (AssetDatabase. loadMainAssetAtPath ("Assets/Scene1.prefab"), null, "WebPlayer/Scene1.unity3d", options); BuildPipeline. buildAssetBundle (AssetDatabase. loadMainAssetAtPath ("Assets/Scene2.prefab"), null, "WebPlayer/Scene2.unity3d", options); BuildPipeline. popAssetDependencies (); BuildPipeline. popAssetDependencies ();} [MenuItem ("Assets/Update shader bundle")] static void ExportShaders () {BuildAssetBundleOptions options = BuildAssetBundleOptions. collectDependencies | BuildAssetBundleOptions. completeAssets | BuildAssetBundleOptions. deterministicAssetBundle; BuildPipeline. pushAssetDependencies (); BuildPipeline. buildAssetBundle (AssetDatabase. loadMainAssetAtPath ("Assets/ShadersList. prefab "), null," WebPlayer/ShadersList. unity3d ", options); BuildPipeline. popAssetDependencies ();}}

Then, when loading, we need to first load the dependent assetbundle, and then load other assetbundle. But there are some pitfalls here. Especially when the dependent assetbundle is a resource file, because it is a resource file, we do not explicitly Load (general objects such as prefab, after loading assetbundle, we usually manually Load and then Instantiate). For resource files, we only need to Load the assetbundle to the memory, and then when loading other assetbundle, the system automatically loads data. There are two problems,

1. For resource files, we cannot load them to the memory (for example, WWW asset = new WWW (BundleURL + "res. assetbundle "), we need to manually call: AssetBundle bundle = asset. assetBundle; no, the resources will be lost when other assetbundle is loaded in the future. Why? Let's take a look at the official definition of www. assetBundle:

Streams an AssetBundle that can contain in any kind of asset from the project folder.

The explanation here is not easy to understand. Here we should call www. assetbundle to load the assetbundle ing structure of assetbundle to the memory. (WebStream consists of three parts: Assetbundle raw compressed data, Decompression Buffer, and Assetbundle decompressed data. Only data streams are in WebStream, while resources in Assets are in Assetbundle. resources loaded by Load can be recognized by the engine, such as Texture2D and Mesh. Therefore, we need to convert webStrem into identifiable resources for the system to load later ).

2. For general Assetbundle, we usually call assetBundle. Unload (false) to release the original data of AssetBundle in memory. However, we cannot perform this operation on resource files. Let's take a look at the official definition of Unload:

AssetBundle. UnloadUnload (unloadAllLoadedObjects: bool): void;

Unloads all assets in the bundle.

Unload frees all the memory associated with the objects inside the bundle.

When UnloadAllLoadedObjectsIs false, compressed file data for assets inside the bundle will be unloaded, but any actual objects already loaded from this bundle will be kept intact. of course you won't be able to load any more objects from this bundle.

When UnloadAllLoadedObjectsIs true, all objects that were loaded from this bundle will be destroyed as well. If there are game objects in your scene referencing those assets, the references to them will become missing.
According to the description, assetBundle. after Unload (false), the content already loaded into the memory will not be unloaded, but the principle of the problem here is still the same as that in Section 1.1. After unload, the ing structure of the assetbundle in the memory will be uninstalled, so the system will load the resources in the future.


Refer:

Http://docs.unity3d.com/Manual/AssetBundlesIntro.html

Http://forum.china.unity3d.com/thread-379-1-1.html




AssetBundle Miscellaneous

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.