Following Unity’s advice, starting Unity 5.3, it is strongly recommended to use UnityWebRequest -rather than WWW.LoadFromCacheOrDownload- to download AssetBundles from a server. However, we also need to store the downloaded bundles locally (via Application.persistentDataPath)… but how??
WWW.LoadFromCacheOrDownload provides you with a byte that could be saved/loaded. Unfortunately, accessing the data field in DownloadHandlerAssetBundle is not allowed and triggers an exception. Is there any way to access/save the downloaded data using UnityWebRequest? Or are we forced to go back and use WWW.LoadFromCacheOrDownload instead?
PS - We are aware of the Caching functionality, but we want to store it locally.
I think that what you are looking for is:
this seems to work:
string url = "http://myserver.com/myassetBundle";
UnityWebRequest www = UnityWebRequest.Get(url);
yield return www.Send();
string filePath = Path.Combine(Application.persistentDataPath, "myassetBundle");
AssetBundle bundle = AssetBundle.LoadFromFile(filePath);
It is a raw code, you need to write validations…
If you’re using DownloadHandlerAssetBundle, your AssetBundle data will probably be cached locally by Unity automatically. Since you want to access the raw binary data, I assume that you’re implementing your own AssetBundle cache system and want to bypass Unity’s implementation.
Then you will have to write your own DownloadHandlerScript like this:
class CustomAssetBundleDownloadHandler : DownloadHandlerScript
private string _targetFilePath;
private Stream _fileStream;
public CustomAssetBundleDownloadHandler(string targetFilePath)
: base(new byte) // use pre-allocated buffer for better performance
_targetFilePath = targetFilePath;
protected override bool ReceiveData(byte data, int dataLength)
// create or open target file
if (_fileStream == null)
_fileStream = File.OpenWrite(_targetFilePath);
// write data to file
_fileStream.Write(data, 0, dataLength);
protected override void CompleteContent()
// close and save
When you need to download a file, just create a
new CustomAssetBundleDownloadHandler(pathToFile), assign it to the
UnityWebRequest object as its download handler, and data will be saved to the target file.
i think you can download ab as a file and save it on local then load it OK?