Version: 2023.1
AssetBundle
为 AssetBundle 准备资源

AssetBundle 工作流程

要开始使用 AssetBundle,请按照以下步骤操作。有关每个工作流程的更多详细信息,请参阅本文档这一部分的其他页面。

Note: This section describes the creation of AssetBundles using the built-in BuildPipeline.BuildAssetBundles() API. A recommended, and more user friendly, alternative is to use the Addressables package.

为 AssetBundle 分配资源

要为 AssetBundle 分配指定资源,请按照下列步骤操作:

  1. 从 Project 视图中选择要为捆绑包分配的资源。
  2. 在 Inspector 中检查对象。
  3. 在 Inspector 底部,有一个用于分配 AssetBundle 和变体的部分。可使用左侧下拉选单分配 AssetBundle,而使用右侧下拉选单分配变量。
  4. 单击左侧下拉选单的 None 以显示当前注册的 AssetBundle 名称。
  5. 单击 New 以创建新的 AssetBundle
  6. 输入所需的 AssetBundle 名称。注意:AssetBundle 名称支持某种类型的文件夹结构,具体取决于您输入的内容。要添加子文件夹,请用 / 分隔文件夹名称。例如,使用 AssetBundle 名称 environment/forestenvironment 子文件夹下创建名为 forest 的捆绑包
  7. 一旦选择或创建了 AssetBundle 名称,便可以重复此过程在右侧下拉选单中分配或创建变体名称(如果需要)。构建 AssetBundle 不需要变体名称

Note: In the Inspector you can assign an AssetBundle to a folder in your Project. By default, all Assets in that folder are assigned to the same AssetBundle as the folder. The AssetBundle assignments for individual Assets takes precedence, however.

要阅读有关 AssetBundle 分配和相关策略的更多信息,请参阅关于为 AssetBundle 准备资源的文档。

构建 AssetBundle

在 Assets 文件夹中创建一个名为 Editor 的文件夹,并将包含以下内容的脚本放在该文件夹中:

using UnityEditor;
using System.IO;

public class CreateAssetBundles
{
    [MenuItem("Assets/Build AssetBundles")]
    static void BuildAllAssetBundles()
    {
        string assetBundleDirectory = "Assets/AssetBundles";
        if(!Directory.Exists(assetBundleDirectory))
            Directory.CreateDirectory(assetBundleDirectory);

        BuildPipeline.BuildAssetBundles(assetBundleDirectory, 
                                        BuildAssetBundleOptions.None, 
                                        BuildTarget.StandaloneWindows);
    }
}

This script creates a menu item at the bottom of the Assets menu called Build AssetBundles. When you click Build AssetBundles the BuildAllAssetBundles() function is called. A progress bar appears while the build takes all the Assets you labeled with an AssetBundle name and uses them to populate AssetBundles at the path that assetBundleDirectory defines.

Let’s take a closer look at the arguments passed to BuildPipeline.BuildAssetBundles:

assetBundleDirectory: This is the directory that the AssetBundles will be output to, e.g. “Assets/AssetBundles” within the current Unity project. The folder does not need to be inside the Assets folder, you can change this to any output directory you desire. Notice how our script creates the folder on demand if it does not exist yet.

BuildAssetBundleOptions.None: This is the default value for the build options argument. You can use this argument to specify one or more flags to enable a variety of optional behaviours. For example, this argument controls the choice of compression algorithm, see AssetBundle compression. See BuildAssetBundleOptions for a full listing of the available options.

BuildTarget.StandaloneWindows: Here we’re telling the build pipeline which target platform we are going to be using these AssetBundles for. You can find a list of the available build targets in the Scripting API Reference for BuildTarget. Alternatively, rather than hardcoding your build target you could call EditorUserBuildSettings.activeBuildTarget, which return the platform currently selected in the Build Settings Window.

Using a Script to Define AssetBundles Contents

The example above describes how to use the Inspector to assign assets to AssetBundles. You can also assign assets to AssetBundles in code, using a signature of BuildPipeline.BuildAssetBundles that accepts an array of AssetBundleBuild structures. If you use this technique, the data you pass in takes priority, and any assignments to AssetBundles made in the Inspector are ignored.

Downloading and Loading AssetBundles and Assets

AssetBundles can be distributed in several ways:

  • Locating the files inside the StreamingAssets folder and including them with your Player build.
  • Hosted by a web service such as Unity’s Cloud Content Delivery and downloaded using UnityWebRequestAssetBundle.
  • Distributed by your own download or installation code. This approach takes more development work but does give flexibility to completely control aspects like compression, caching, patching and validation prior to loading the file using Unity APIs.

Depending how the files are distributed you should either use AssetBundle.Load APIs or UnityWebRequestAssetBundle to load an AssetBundle and access the AssetBundle object in your runtime code.

From the AssetBundle object, you call one of the LoadAsset methods. For example, LoadAsset<T>(string) which takes the type, T, of the asset you’re attempting to load and the name of the Asset (typically its path). You can use the returned object just like any object inside of Unity. For example, if you load a prefab then LoadAsset will return the prefab’s root GameObject, which you can then instantiate into your current scene by calling Instantiate().

You can reclaim memory used by a loaded AssetBundle by calling AssetBundle.Unload(bool) or AssetBundle.UnloadAsync(bool).

For more information on APIs that load and unload AssetBundles, see documentation on Using AssetBundles Natively.

AssetBundle
为 AssetBundle 准备资源