- Home /
Access raw AssetBundle data using UnityWebRequest?
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.
Have you found any solution for this? I am having the same problem right now and i can't find anything working...
I'm trying to get answers to this. Am aware of the caching class, problem is , where do i actually retreive the cached data? I can't write it locally using www.downloadHandler.data - I get error "NotSupportedException: Raw data access is not supported for asset bundles".
Answer by alvaro-em · Oct 21, 2016 at 06:47 PM
DownloadHandlerAssetBundle cannot access .data or .GetData, the content is null.
Answer by IgorSimovic · Mar 27, 2017 at 07:23 PM
Hi, 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");
 File.WriteAllBytes(filePath, www.downloadHandler.data);
     
 AssetBundle bundle = AssetBundle.LoadFromFile(filePath);
It is a raw code, you need to write validations...
it doesn't work!it throw error like this: NotSupportedException: Raw data access is not supported for asset bundles
Answer by Ran-Quan · Mar 29, 2017 at 05:40 AM
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[4096]) // 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);
 
         return true;
     }
 
     protected override void CompleteContent()
     {
         // close and save
         _fileStream.Close();
     }
 }
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.
so...is it WWW www = UnityWebRequest.GetAssetBundle (new CustomAssetBundleDownloadHandler (bundleURL), hashString, 0); ??
- but bundleURL should be where the file should be written to right? 
I did it like this
  UnityWebRequest www = UnityWebRequest.Get (bundleURL + ".manifest");
   
     // create empty hash string
     Hash128 hashString = (default(Hash128));
     
     if (www.downloadHandler.text.Contains ("$$anonymous$$anifestFileVersion"))  {
     var hashRow = www.downloadHandler.text.ToString ().Split ("\n".ToCharArray ()) [5];
     hashString = Hash128.Parse (hashRow.Split (':') [1].Trim ());
     
     if (hashString.isValid == true) {
     if (Caching.IsVersionCached (bundleURL, hashString) == true) {
     Debug.Log ("Bundle with this hash is already cached!... " + hashString);
     } else {
     string writepath = Application.persistentDataPath + "/" + hashString;
     www.downloadHandler = new CustomAssetBundleDownloadHandler (writepath);
     Debug.Log ("No cached version founded for this hash.." + hashString);
     }
I get error InvalidOperationException: UnityWebRequest has already been sent; cannot modify the download handler
Answer by Handsome-Wisely · Nov 26, 2021 at 03:04 AM
i think you can download ab as a file and save it on local then load it OK?
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                