Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by chico_barnstorm · Oct 04, 2016 at 08:49 PM · assetbundlewebrequestassetbundlesloadfromcacheordownload

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image misterPantoni · Nov 15, 2016 at 10:42 AM 0
Share

Have you found any solution for this? I am having the same problem right now and i can't find anything working...

avatar image xpkoalz · Jul 07, 2017 at 06:55 AM 0
Share

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".

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by alvaro-em · Oct 21, 2016 at 06:47 PM

Hi, @chico_barnstorm.

I think that what you are looking for is:

 wwww.downloadHandler.data
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image LorenzoNuvoletta · Jan 10, 2017 at 06:39 PM 0
Share

DownloadHandlerAssetBundle cannot access .data or .GetData, the content is null.

avatar image
0

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...

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image yimi_cgh · Aug 17, 2018 at 02:00 PM 0
Share

it doesn't work!it throw error like this: NotSupportedException: Raw data access is not supported for asset bundles

avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image xpkoalz · Jul 07, 2017 at 07:08 AM 0
Share

so...is it WWW www = UnityWebRequest.GetAssetBundle (new CustomAssetBundleDownloadHandler (bundleURL), hashString, 0); ??

  • but bundleURL should be where the file should be written to right?

avatar image xpkoalz · Jul 07, 2017 at 07:29 AM 0
Share

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

avatar image
0

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?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

UnityWebRequest returns different errors for right urls 0 Answers

How do I have multiple UnityWebRequests in one coroutine? 1 Answer

LoadFromCacheOrDownload Version Variable Reset? 1 Answer

AssetBundles - depend on asset already included in executable 0 Answers

AssetBundle - Broken link between Scene and Assets 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges