AssetBundle
AssetBundles let you stream additional assets via the WWW class and instantiate them at runtime.Note that bundles are not fully compatible between platforms. A bundle built for any of the standalone platforms (including webplayer) can be loaded on any of those platforms but not on iOS or Android. Furthermore, a bundle built for iOS is not compatible with Android and vice versa.
Building AssetBundles
There are three class methods you can use to build AssetBundles:
- BuildPipeline.BuildAssetBundle allows you to build AssetBundles of any type of asset.
- BuildPipeline.BuildStreamedSceneAssetBundle is used when you want to include only scenes to be streamed and loaded as the data becomes available.
- BuildPipeline.BuildAssetBundleExplicitAssetNames is the same as BuildPipeline.BuildAssetBundle but has an extra parameter to specify a custom string identifier (name) for each object.
An example of how to build an AssetBundle
Building asset bundles is done through editor scripting.Create a new C# script called ExportAssetBundles. This script should be placed in a folder named Editor, so that it works inside the Unity Editor.
using UnityEngine;
using UnityEditor;
public class ExportAssetBundles
{
[MenuItem("Assets/Build AssetBundle From Selection - Track dependencies Windows")]
static void ExportResource ()
{
string path = EditorUtility.SaveFilePanel("AssetBundle", "", "New Resource", "unity3d");
if (path.Length != 0) {
Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets, BuildTarget.StandaloneWindows);
}
}
[MenuItem("Assets/Build AssetBundle From Selection - Track dependencies iPhone")]
static void ExportAssetBundleIOSComplete() {
string path = EditorUtility.SaveFilePanel("AssetBundle", "", "New Resource", "unity3d");
if (path.Length != 0) {
Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets, BuildTarget.iPhone);
Selection.objects = selection;
}
}
[MenuItem("Assets/Build AssetBundle From Selection - Track dependencies Android")]
static void ExportResourceAndroid () {
string path = EditorUtility.SaveFilePanel ("AssetBundle", "", "New Resource", "unity3d");
if (path.Length != 0) {
Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path,
BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets,BuildTarget.Android);
Selection.objects = selection;
}
}
}
Now in the Assets menu, you should see three new menu options.
- Build AssetBundle From Selection - Track dependencies Windows (This will build the current object into an asset bundle and include all of its dependencies for Windows Platform)
- Build AssetBundle From Selection - Track dependencies iPhone (This will build the current object into an asset bundle and include all of its dependencies for iOS Platform)
- Build AssetBundle From Selection - Track dependencies Android (This will build the current object into an asset bundle and include all of its dependencies for Android Platform)
To Build AssetBundle, you should create a new prefab.Drag the desired object from the Hierarchy View into the Project View, which will create a prefab of that object.You should then right click the prefab in the project window and select the option to build AssetBundle according to the platform. At this point you will be presented with a window to save the "bundled" asset. You can now move the AssetBundle object.unity3d elsewhere on your local storage, or upload it to a server of your choice.
Happy Coding.............
2 Comment(s)