Version: 2022.1

BuildPipeline.BuildAssetBundles

切换到手册
public static AssetBundleManifest BuildAssetBundles (string outputPath, BuildAssetBundleOptions assetBundleOptions, BuildTarget targetPlatform);

参数

outputPath AssetBundle 的输出路径。
assetBundleOptions 资源包构建选项。
targetPlatform 选择的目标构建平台。

返回

AssetBundleManifest 列出了此构建中包含的所有 AssetBundle 的清单。

描述

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); } }

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

参数

outputPath AssetBundle 的输出路径。
builds AssetBundle 构建映射。
assetBundleOptions 资源包构建选项。
targetPlatform 目标构建平台。

返回

AssetBundleManifest 列出了此构建中包含的所有 AssetBundle 的清单。

描述

使用构建映射构建 AssetBundle。

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); } }