要开始使用 AssetBundle,请按照以下步骤操作。有关每个工作流程的更多详细信息,请参阅本文档这一部分的其他页面。
要为 AssetBundle 分配指定资源,请按照下列步骤操作:
/
分隔文件夹名称。例如,使用 AssetBundle 名称 environment/forest
在 environment
子文件夹下创建名为 forest
的捆绑包要阅读有关 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);
}
}
此脚本将在 Assets 菜单底部创建一个名为 Build AssetBundles 的菜单项,该菜单项将执行与该标签关联的函数中的代码。单击 Build AssetBundles 时,将随构建对话框一起显示一个进度条。此过程将会获取带有 AssetBundle 名称标签的所有资源,并将它们放在 assetBundleDirectory
定义的路径中的文件夹中。
如需了解与此相关的更多详细信息,请参阅关于构建 AssetBundle 的文档。
如果您想从本地存储中加载,请使用 AssetBundles.LoadFromFile
API,如下所示:
public class LoadFromFileExample : 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 url = "file:///" + Application.dataPath + "/AssetBundles/" + assetBundleName;
UnityEngine.Networking.UnityWebRequest request
= UnityEngine.Networking.UnityWebRequest.GetAssetBundle(url, 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 的位置 URL 以及要下载的捆绑包的版本。此示例仍然指向一个本地文件,但 string url
可以指向托管 AssetBundle 的任何 URL。
UnityWebRequest 有一个特定的句柄来处理 AssetBundle:DownloadHandlerAssetBundle
,可根据请求获取 AssetBundle。
无论使用哪种方法,现在都可以访问 AssetBundle 对象了。对该对象需要使用 LoadAsset<T>(string)
,此函数将获取您尝试加载的资源的类型 T
以及对象的名称(作为捆绑包内部的字符串)。这将返回从 AssetBundle 加载的任何对象。可以像使用 Unity 中的任何对象一样使用这些返回的对象。例如,如果要在场景中创建游戏对象,只需调用 Instantiate(gameObjectFromAssetBundle)
。
有关用于加载 AssetBundle 的 API 的更多信息,请参阅关于本机使用 AssetBundle 的文档。
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.