要开始使用 AssetBundle,请按照以下步骤操作。有关每个工作流程的更多详细信息,请参阅本文档这一部分的其他页面。
要为 AssetBundle 分配指定资源,请按照下列步骤操作:
1.从 Project 视图中选择要为捆绑包分配的资源 2.在 Inspector 中检查对象 3.在 Inspector 底部,应该会看到一个用于分配 AssetBundle 和变体的部分: 4.左侧下拉选单分配 AssetBundle,而右侧下拉选单分配变量 5.单击左侧下拉选单,其中显示“None”,表示当前注册的 AssetBundle 名称 6. If none have yet been created, you’ll see a list like that in the image above 7. Click “New…” to create a new AssetBundle 8. Type in the desired AssetBundle name. Note that AssetBundle names do support a type of folder structure depending on what you type. To add sub folders, separate folder names by a “/”. For example: AssetBundle name “environment/forest” will create a bundle named forest under an environment sub folder 9. Once you’ve selected or created an AssetBundle name, you can repeat this process for the right hand drop down to assign or create a Variant name, if you desire. Variant names are not required to build the AssetBundles
To read more information on AssetBundle assignments and accompanying strategies, see documentation on Preparing Assets for AssetBundles .
在 Assets 文件夹中创建一个名为 Editor 的文件夹,并将包含以下内容的脚本放在该文件夹中:
using UnityEditor;
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.Standalone);
}
}
此脚本将在 Assets 菜单底部创建一个名为“Build AssetBundles”的菜单项,该菜单项将执行与该标签关联的函数中的代码。单击 Build AssetBundles 时,将随构建对话框一起显示一个进度条。此过程将会获取带有 AssetBundle 名称标签的所有资源,并将它们放在 assetBundleDirectory 定义的路径中的文件夹中。
For more detail about what this code is doing, see documentation on Building AssetBundles.
此步骤对每个用户都是不同的,因此 Unity 不能告诉您应该具体如何操作。如果计划将 AssetBundle 上传到第三方托管站点,请在此步中执行该操作。如果正在严格执行本地开发并打算将所有 AssetBundle 都放在磁盘上,请跳转到下一步。
打算从本地存储加载的用户可能会对 AssetBundles.LoadFromFile API 感兴趣。该 API 如下所示:
public class LoadFromFileExample extends MonoBehaviour {
function Start() {
var myLoadedAssetBundle = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, "myassetBundle"));
if (myLoadedAssetBundle == null) {
Debug.Log("Failed to load AssetBundle!");
return;
}
var prefab = myLoadedAssetBundle.LoadAsset.<GameObject>("MyObject");
Instantiate(prefab);
}
}
LoadFromFile
获取捆绑包文件的路径。
如果是自己托管 AssetBundle 并需要将它们下载到游戏中,应使用 UnityWebRequest API。下面是一个示例:
IEnumerator InstantiateObject()
{
string uri = "file:///" + Application.dataPath + "/AssetBundles/" + assetBundleName;
UnityEngine.Networking.UnityWebRequest request = UnityEngine.Networking.UnityWebRequest.GetAssetBundle(uri, 0);
yield return request.Send();
AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(request);
GameObject cube = bundle.LoadAsset<GameObject>("Cube");
GameObject sprite = bundle.LoadAsset<GameObject>("Sprite");
Instantiate(cube);
Instantiate(sprite);
}
GetAssetBundle(string, int)
获取 AssetBundle 的位置以及要下载的捆绑包的版本。在这个例子中,我们仍然指向一个本地文件,但字符串 uri 可以指向托管 AssetBundle 的任何 url。
UnityWebRequest 有一个特定的句柄来处理 AssetBundle:DownloadHandlerAssetBundle
,可根据请求获取 AssetBundle。
无论使用哪种方法,现在都可以访问 AssetBundle 对象了。对该对象需要使用 LoadAsset<T>(string)
,此函数将获取尝试加载的资源的类型 T
以及对象的名称(作为捆绑包内部的字符串)。这将返回从 AssetBundle 加载的任何对象。可以像使用 Unity 中的任何对象一样使用这些返回的对象。例如,如果要在场景中创建游戏对象,只需调用 Instantiate(gameObjectFromAssetBundle)
。
For more information on APIs that load AssetBundles, see documentation on Using AssetBundles Natively.