Legacy Documentation: Version 4.5.0

Script language:

  • JS
  • C#
  • Boo
Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

BuildPipeline.BuildAssetBundle

static function BuildAssetBundle(mainAsset: Object, assets: Object[], pathName: string, assetBundleOptions: BuildAssetBundleOptions = BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets, targetPlatform: BuildTarget = BuildTarget.WebPlayer): bool;
static bool BuildAssetBundle(Object mainAsset, Object[] assets, string pathName, BuildAssetBundleOptions assetBundleOptions = BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets, BuildTarget targetPlatform = BuildTarget.WebPlayer);
static def BuildAssetBundle(mainAsset as Object, assets as Object[], pathName as string, assetBundleOptions as BuildAssetBundleOptions = BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets, targetPlatform as BuildTarget = BuildTarget.WebPlayer) as bool
static function BuildAssetBundle(mainAsset: Object, assets: Object[], pathName: string, crc: uint, assetBundleOptions: BuildAssetBundleOptions = BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets, targetPlatform: BuildTarget = BuildTarget.WebPlayer): bool;
static bool BuildAssetBundle(Object mainAsset, Object[] assets, string pathName, uint crc, BuildAssetBundleOptions assetBundleOptions = BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets, BuildTarget targetPlatform = BuildTarget.WebPlayer);
static def BuildAssetBundle(mainAsset as Object, assets as Object[], pathName as string, crc as uint, assetBundleOptions as BuildAssetBundleOptions = BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets, targetPlatform as BuildTarget = BuildTarget.WebPlayer) as bool

Description

Builds an asset bundle (Unity Pro only).

Creates a compressed unity3d file that contains a collection of assets. AssetBundles can contain any asset found in the project folder.

This lets you stream resource data of any type, fully setup prefabs, textures, meshes, animations, any type of asset shown in the project window.

/mainAsset/ lets you specify a specific object that can be conveniently retrieved using AssetBundle.mainAsset.

The compressed asset bundle file will be saved at pathName. /options/ allows you to automatically include dependencies or always include complete assets instead of just the exact referenced objects.

All paths are relative to the project folder. Like: "Assetscrchello.png". The optional crc output parameter can be used to get a CRC checksum for the generated AssetBundle, which can be used to verify content when downloading AssetBundles using WWW.LoadFromCacheOrDownload.

Note that asset bundles built for standalone or webplayer targets cannot be loaded by applications built for mobile platforms and vice versa. Furthermore, bundles are not compatible between iOS and Android platforms.

The function returns a boolean value which is true if the build succeeded and false otherwise.

See Also: AssetBundle class, WWW.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 Bundle
	
	using UnityEngine;
	using UnityEditor;

public class ExportAssetBundles { [MenuItem("Assets/Build AssetBundle From Selection - Track dependencies")] static void ExportResource () { // Bring up save panel string path = EditorUtility.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 panel string 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); } } }