Version: 2022.1
LanguageEnglish
  • C#

BuildPipeline.BuildAssetBundles

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Declaration

public static AssetBundleManifest BuildAssetBundles(string outputPath, BuildAssetBundleOptions assetBundleOptions, BuildTarget targetPlatform);

Parameters

outputPath Output path for the AssetBundles.
assetBundleOptions AssetBundle building options.
targetPlatform Chosen target build platform.

Returns

AssetBundleManifest The manifest listing all AssetBundles included in this build.

Description

Build all AssetBundles.

Use this function to build AssetBundles based on the AssetBundle and Label settings you have configured in the Editor. (See the Manual page about building AssetBundles for further details.)

Set outputPath to the folder within your project folder where you want to save the built bundles (for example: "Assets/MyBundleFolder"). The folder is not created automatically and the function simply fails if it doesn't already exist. The files created by this function include a manifest file, with the same name as the output folder, but using ".manifest" as its extension (for example: "MyBundleFolder.manifest"). Assign the path to this manifest file to BuildPlayerOptions.assetBundleManifestPath before calling BuildPipeline.BuildPlayer to make sure that any types appearing in the AssetBundles are not stripped from the build. (See Managed code stripping for more information about code stripping.)

Use the optional assetBundleOptions argument to specify bundle build options.

The targetPlatform argument selects which deployment target (Windows Standalone, Android, iOS, and so on) to build the bundles for. An AssetBundle is only compatible with the specific platform that it was built for, so you must produce different builds of a given bundle to use the assets on different platforms. See EditorUserBuildSettings.activeBuildTarget and the BuildTarget section of the Building AssetBundles page in the Manual for more information about creating AssetBundles for different platforms.

This function returns an AssetBundleManifest instance that describes the bundles produced. If the build fails then the function returns null or throws an exception. You can find details about what went wrong in the exception message or in errors logged to the console. For example, if you pass an invalid or non-existent path as the outputPath parameter, the function throws an ArgumentException to indicate that one of the supplied arguments was invalid

// Create an AssetBundle for Windows.
using UnityEngine;
using UnityEditor;

public class BuildAssetBundlesExample { [MenuItem("Example/Build Asset Bundles")] static void BuildABs() { // Put the bundles in a folder called "ABs" within the Assets folder. BuildPipeline.BuildAssetBundles("Assets/ABs", BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows); } }

Declaration

public static AssetBundleManifest BuildAssetBundles(string outputPath, AssetBundleBuild[] builds, BuildAssetBundleOptions assetBundleOptions, BuildTarget targetPlatform);

Parameters

outputPath Output path for the AssetBundles.
builds AssetBundle building map.
assetBundleOptions AssetBundle building options.
targetPlatform Target build platform.

Returns

AssetBundleManifest The manifest listing all AssetBundles included in this build.

Description

Build AssetBundles from a building map.

This signature of BuildAssetBundles lets you specify the names and contents of the bundles programmatically, using a "build map" rather than with the details set in the editor. The map is simply an array of AssetBundleBuild objects, each of which contains a bundle name and a list of the names of asset files to be added to the named bundle.

using UnityEngine;
using UnityEditor;

public class BuildAssetBundlesBuildMapExample { [MenuItem("Example/Build Asset Bundles Using BuildMap")] static void BuildMapABs() { // Create the array of bundle build details. AssetBundleBuild[] buildMap = new AssetBundleBuild[2];

buildMap[0].assetBundleName = "enemybundle";

string[] enemyAssets = new string[2]; enemyAssets[0] = "Assets/Textures/char_enemy_alienShip.jpg"; enemyAssets[1] = "Assets/Textures/char_enemy_alienShip-damaged.jpg";

buildMap[0].assetNames = enemyAssets; buildMap[1].assetBundleName = "herobundle";

string[] heroAssets = new string[1]; heroAssets[0] = "char_hero_beanMan"; buildMap[1].assetNames = heroAssets;

BuildPipeline.BuildAssetBundles("Assets/ABs", buildMap, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows); } }