要从远程服务器下载 AssetBundle,你可以使用 UnityWebRequest.GetAssetBundle。此函数将数据流式传输到一个内部缓冲区,该缓冲区在工作线程上对 AssetBundle 的数据进行解码和解压缩。
此函数的参数有多种形式。在最简单的形式中,它只获取应从中下载 AssetBundle 的 URL。你还可以选择提供一个校验和来验证已下载数据的完整性。
或者,如果你希望使用 AssetBundle 缓存系统,你可以提供一个版本号或一个 Hash128 数据结构。这些与通过 WWW.LoadFromCacheOrDownload 提供给旧系统的版本号或 Hash128 对象 相同。
UnityWebRequest,并将目标 URL 设置为提供的 URL 参数。它还将 HTTP 动词设置为 GET,但不会设置其他标志或自定义标头。DownloadHandlerAssetBundle 附加到 UnityWebRequest。此下载处理程序有一个特殊的 assetBundle 属性,可用于在下载并解码足够的数据以允许访问 AssetBundle 中的资源后提取 AssetBundle。Hash128 对象作为参数,它还会将这些参数传递给 DownloadHandlerAssetBundle。然后,下载处理程序会采用缓存系统。using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
public class MyBehaviour : MonoBehaviour {
void Start() {
StartCoroutine(GetAssetBundle());
}
IEnumerator GetAssetBundle() {
UnityWebRequest www = UnityWebRequestAssetBundle.GetAssetBundle("https://www.my-server.com/myData.unity3d");
yield return www.SendWebRequest();
if (www.result != UnityWebRequest.Result.Success) {
Debug.Log(www.error);
}
else {
AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(www);
}
}
}