版本:Unity 6 (6000.0)
语言英语
  • C#

BuildPipeline.BuildAssetBundles

建议修改

成功!

感谢您帮助我们提高 Unity 文档的质量。虽然我们不能接受所有提交内容,但我们会阅读用户提出的每项修改建议,并在适用的情况下进行更新。

关闭

提交失败

由于某些原因,您的建议修改无法提交。请<a>稍后再试</a>。感谢您花时间帮助我们提高 Unity 文档的质量。

关闭

取消

声明

public static AssetBundleManifest BuildAssetBundles(BuildAssetBundlesParameters buildParameters);

参数

buildOptions 构建配置。

返回值

AssetBundleManifest 总结构建过程中生成的所有 AssetBundle 的清单。

描述

构建 AssetBundle。

建议使用此 BuildAssetBundles 签名,它提供了最多的功能。为了向后兼容和方便起见,保留了下面记录的其他签名。

在 AssetBundle 构建过程中,实现 IProcessSceneWithReport 的类将在处理场景时被调用。类似地,可以通过实现 IPreprocessShadersIPreprocessComputeShaders 来影响着色器的构建方式。

此函数返回一个 AssetBundleManifest 实例,它描述了生成的捆绑包。如果构建失败,则该函数返回 null 或抛出异常。您可以在异常消息中或在记录到控制台的错误中找到有关发生错误的详细信息。例如,如果您将无效或不存在的路径作为 outputPath 参数传递,则该函数会抛出 ArgumentException 以指示提供的参数无效。

其他资源:AssetBundleEditorBuildSettings.UseParallelAssetBundleBuilding

using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;
using UnityEditor;

public class BuildAssetBundlesExample { [MenuItem("Example/Build AssetBundles")] static void BuildBundles() { List<AssetBundleBuild> assetBundleDefinitionList = new();

// Define two asset bundles, populated based on file system structure

// The first bundle is all the scene files in the Scenes directory (non-recursive) { AssetBundleBuild ab = new(); ab.assetBundleName = "Scenes"; ab.assetNames = Directory.EnumerateFiles("Assets/" + ab.assetBundleName, "*.unity", SearchOption.TopDirectoryOnly).ToArray(); assetBundleDefinitionList.Add(ab); }

// The second bundle is all the asset files found recursively in the Meshes directory { AssetBundleBuild ab = new(); ab.assetBundleName = "Meshes"; ab.assetNames = RecursiveGetAllAssetsInDirectory("Assets/" + ab.assetBundleName).ToArray(); assetBundleDefinitionList.Add(ab); }

string outputPath = "MyBuild"; // Subfolder of the current project

if (!Directory.Exists(outputPath)) Directory.CreateDirectory(outputPath);

// Assemble all the input needed to perform the build in this structure. // The project's current build settings will be used because target and subtarget fields are not filled in BuildAssetBundlesParameters buildInput = new() { outputPath = outputPath, options = BuildAssetBundleOptions.AssetBundleStripUnityVersion, bundleDefinitions = assetBundleDefinitionList.ToArray() }; AssetBundleManifest manifest = BuildPipeline.BuildAssetBundles(buildInput);

// Look at the results if (manifest != null) { foreach (var bundleName in manifest.GetAllAssetBundles()) { string projectRelativePath = buildInput.outputPath + "/" + bundleName; Debug.Log($"Size of AssetBundle {projectRelativePath} is {new FileInfo(projectRelativePath).Length}"); } } else { Debug.Log("Build failed, see Console and Editor log for details"); } }

static List<string> RecursiveGetAllAssetsInDirectory(string path) { List<string> assets = new(); foreach (var f in Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories)) if (Path.GetExtension(f) != ".meta" && Path.GetExtension(f) != ".cs" && // Scripts are not supported in AssetBundles Path.GetExtension(f) != ".unity") // Scenes cannot be mixed with other file types in a bundle assets.Add(f);

return assets; } }

除了 AssetBundle 文件之外,构建还会生成其他几个文件

* AssetBundleManifest 存储在一个小型 AssetBundle 中,该 AssetBundle 的名称与输出文件夹相同,例如“MyBundleFolder”。这是从 BuildAssetBundles 调用返回的相同对象,并且序列化形式在运行时非常有用,例如确定其他 AssetBundle 的预期哈希值。

* 主“.manifest”文件,这是一个文本格式文件。它与输出文件夹具有相同的名称,但使用“.manifest”作为其扩展名(例如:“MyBundleFolder.manifest”)。在调用 BuildPipeline.BuildPlayer 之前,将此清单文件路径分配给 BuildPlayerOptions.assetBundleManifestPath,以确保不会从构建中剥离出现在 AssetBundle 中的任何类型。(有关代码剥离的更多信息,请参阅 托管代码剥离。)

* 每个 AssetBundle 还会根据 AssetBundle 的名称编写一个单独的“.manifest”文件。

* 最后,构建的 BuildReport 会写入“Library/LastBuild.buildreport”。

using System.IO;
using UnityEngine;
using UnityEditor;

public class BuildAssetBundlesOutputFileExample { [MenuItem("Example/AssetBundle Output File Example")] static void BuildAndPrintOutputFiles() { var bundleDefinitions = new AssetBundleBuild[] { new AssetBundleBuild { assetBundleName = "mybundle", assetNames = new string[] { "Assets/Scenes/Scene1.unity" } } };

string buildOutputDirectory = "build"; Directory.CreateDirectory(buildOutputDirectory);

BuildAssetBundlesParameters buildInput = new() { outputPath = buildOutputDirectory, bundleDefinitions = bundleDefinitions };

AssetBundleManifest manifest = BuildPipeline.BuildAssetBundles(buildInput); if (manifest != null) { var outputFiles = Directory.EnumerateFiles(buildOutputDirectory, "*", SearchOption.TopDirectoryOnly);

//Expected output (on Windows): // Output of the build: // build\build // build\build.manifest // build\mybundle // build\mybundle.manifest Debug.Log("Output of the build:\n\t" + string.Join("\n\t", outputFiles)); } } }

声明

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

参数

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

返回值

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

描述

构建所有 AssetBundle。

使用此函数根据您在编辑器中配置的 AssetBundle 和标签设置来构建 AssetBundle。(有关更多详细信息,请参阅关于 AssetBundle 工作流程 的手册页面。)

outputPath 设置为项目文件夹中您要保存已构建捆绑包的文件夹(例如:“Assets/MyBundleFolder”)。该文件夹不会自动创建,如果该文件夹不存在,则该函数 simply fails.

使用可选的 assetBundleOptions 参数来指定捆绑包构建选项。

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.

对于新代码,建议使用接受 BuildAssetBundlesParameters 结构的 BuildAssetBundles 签名,而不是使用此签名。要匹配此签名的行为,请将 BuildAssetBundlesParameters.bundleDefinitions 字段留空,以便使用编辑器的 AssetBundle 赋值。

其他资源:EditorUserBuildSettings.activeBuildTargetAssetDatabase.GetAssetPathsFromAssetBundleAssetDatabase.GetImplicitAssetBundleNameAssetImporter.assetBundleNameAssetBundle

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

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

声明

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

参数

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

返回值

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

描述

从构建映射构建 AssetBundle。

此 BuildAssetBundles 签名允许您使用“构建映射”而不是在编辑器中设置的详细信息以编程方式指定捆绑包的名称和内容。映射只是一个 AssetBundleBuild 对象数组,每个对象包含一个捆绑包名称和要添加到该命名捆绑包的资产文件名称列表。

对于新代码,建议使用接受 BuildAssetBundlesParameters 结构的 BuildAssetBundles 签名,而不是使用此签名。当使用该签名时,构建映射将分配给 BuildAssetBundlesParameters.bundleDefinitions

using UnityEngine;
using UnityEditor;
using System.IO;

public class BuildAssetBundlesBuildMapExample { [MenuItem("Example/Build AssetBundles with Build Map")] static void BuildMapABs() { // Create the array of bundle build details. AssetBundleBuild[] bundleDefinitions = new AssetBundleBuild[2];

bundleDefinitions[0].assetBundleName = "enemybundle"; bundleDefinitions[0].assetNames = new string[] { "Assets/Textures/char_enemy_alienShip.jpg", "Assets/Textures/char_enemy_alienShip-damaged.jpg" };

bundleDefinitions[1].assetBundleName = "herobundle"; bundleDefinitions[1].assetNames = new string[] { "Assets/char_hero_beanMan.prefab" };

string outputPath = "Assets/ABs"; Directory.CreateDirectory(outputPath); BuildPipeline.BuildAssetBundles(outputPath, bundleDefinitions, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64); } }