To download an AssetBundle from a remote server, you can use UnityWebRequest.AssetBundle
. This function streams data into an internal buffer, which decodes and decompresses the AssetBundle’s data on a worker thread.
The function’s arguments take several forms. In its simplest form, it takes only the URL from which the AssetBundle should be downloaded. You may optionally provide a checksum to verify the integrity of the downloaded data.
Alternately, if you wish to use the AssetBundle caching system, you may provide either a version number or a Hash128 data structure. These are identical to the version numbers or Hash128 objects
provided to the old system via WWW.LoadFromCacheOrDownload
.
UnityWebRequest
and sets the target URL to the supplied URL argument. It also sets the HTTP verb to GET
, but sets no other flags or custom headers.DownloadHandlerAssetBundle
to the UnityWebRequest
. This download handler has a special assetBundle
property, which can be used to extract the AssetBundle once enough data has been downloaded and decoded to permit access to the resources inside the AssetBundle.Hash128
object as arguments, it also passes those arguments to the DownloadHandlerAssetBundle
. The download handler then employs the caching system.using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
public class MyBehaviour : MonoBehaviour {
void Start() {
StartCoroutine(GetAssetBundle());
}
IEnumerator GetAssetBundle() {
UnityWebRequest www = UnityWebRequest.GetAssetBundle("http://www.my-server.com/myData.unity3d");
yield return www.Send();
if(www.isError) {
Debug.Log(www.error);
}
else {
AssetBundle bundle = ((DownloadHandlerAssetBundle)www.downloadHandler).assetBundle;
}
}
}
Alternatively, you can implement GetAssetBundle
using a helper getter:
IEnumerator GetTexture() {
UnityWebRequest www = UnityWebRequest.GetAssetBundle("http://www.my-server.com/myData.unity3d");
yield return www.Send();
AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(www);
}
Did you find this page useful? Please give it a rating: