Search Unity

Is there a way to get an AssetBundle from a server and download the contents to a directory?

Discussion in 'Scripting' started by EthanHooper1023, Aug 1, 2019.

  1. EthanHooper1023

    EthanHooper1023

    Joined:
    Jan 19, 2017
    Posts:
    24
    Hello,
    There are a lot of answers about DLCs and downloading AssetBundles but all the ones I've seen are with the WWW.LoadFromCacheOrDownload() which is obsolete now.

    I was wondering how to write assets to a directory using UnityWebRequest. I can get the file from a server and even get AudioClips from the bundle into a list. I just need to write to a directory in the unity project so the user doesn't need to connect to the internet to use the bundles every time.



    I have seen a couple of answers that talked about AudioClip.GetData(samples,0); but the File.WriteAllBytes() takes a byte array

    Thank you in advance!
     
  2. Kerber996

    Kerber996

    Joined:
    Nov 9, 2016
    Posts:
    23
  3. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
  4. EthanHooper1023

    EthanHooper1023

    Joined:
    Jan 19, 2017
    Posts:
    24
    Thank you for the responses, I have been messing around with a lot of different methods and got it working with the help of this answer https://forum.unity.com/threads/save-microphone-recording-to-disk.378038/

    I got the bundle to download from the web and paste the contents into a directory but now I need to load the contents of that directory into the project or to call on it for an AudioSource.PlayOneShot() or even just into an array for future use.

    I will keep looking through the answers given to see if there is anything about accessing directories
     
  5. EthanHooper1023

    EthanHooper1023

    Joined:
    Jan 19, 2017
    Posts:
    24
    After probably too long I figured it out. It ended up being a simple fix...like usual lol.
    Basically, if you want to get a UnityWebRequest to read from your local file system you do the same as you would a server except change the "http://" to "file://". hopefully, this will save other people some time instead of reading every inch of documentation to find it was a quick fix

    Here is an example of playing a random audio file from my streamingAssetsPath (The allClips list is just a string array holding all the names that are in the directory)

    Code (CSharp):
    1. private IEnumerator PlayRandom() {
    2.         string clipPath = "file://" + Application.streamingAssetsPath + "/Sounds/";
    3.  
    4.         using (UnityWebRequest uwr = UnityWebRequestMultimedia.GetAudioClip(clipPath + allClips[UnityEngine.Random.Range(0, allClips.Count)], AudioType.WAV))
    5.         {
    6.             var request = uwr.SendWebRequest();
    7.             yield return request;
    8.  
    9.             if (uwr.isHttpError)
    10.             {
    11.                 Debug.Log(uwr.error);
    12.             }
    13.             else
    14.             {
    15.                 AudioClip clip = DownloadHandlerAudioClip.GetContent(uwr);
    16.                 audioSource.PlayOneShot(clip);
    17.             }
    18.         }
    19.     }
     
  6. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    You don't need to extract asset bundle content to a directory. After the bundle is downloaded, you can just stream audio clip from the bundle directly
    Code (CSharp):
    1. audioSource.PlayOneShot(
    2.   assetBundle.LoadAsset<AudioClip>("SomeTrackName")
    3. );
    and that's it. Clip must be set to streaming in Unity, before building the bundle.
    Summary: if you want to load files, you don't need asset bundle at all. If you have loaded asset bundle, you don't need to extract it's content to files.
     
  7. EthanHooper1023

    EthanHooper1023

    Joined:
    Jan 19, 2017
    Posts:
    24
    Hey @palex-nx
    I had something similar to that working but I wanted to save the audio clips to a file so you didn't need to be connected to the internet to play them.
    Like if you downloaded a sound pack it would be in your game even after the connection is broken. I guess the asset bundle part isn't really needed since I could just download a ".wav" file directly from a server, but it saves on space and makes it easy to add more packs. Does that make sense though? It's functional now but I might be using resources incorrectly
     
  8. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    If you dowloaded asset bundle, it is saved to local filesystem and can be accessed later there. No need to extract it, it just doubles device memory usage without any reason.
     
  9. EthanHooper1023

    EthanHooper1023

    Joined:
    Jan 19, 2017
    Posts:
    24
    Oh okay that would make things way easier, so even if the app gets shut down the asset bundle is still saved to local memory? How do I access it without using the server for another web request?
    I was under the impression that if you wanted to use those assets it would need to be connected to the server with the asset bundle on it
     
  10. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Yes, it is cached locally after downloading. There's option for maximum cache size to ensure your app wont take all user space however. Old unused bundles will be cleaned up when total amount of occupied space will be more than specified value.

    Recently Unity team introduced a lot of changes into bundle loading system. I may be confusing. You may find good explanation here https://forum.unity.com/threads/downloading-and-caching-of-assetbundles.404564/
    Generally, you should not care where your bundle is. You just give unity it's url and everything (donwloading, caching, maintaining cache size, loading assets, etc) is done under the hood without your concern.
     
  11. EthanHooper1023

    EthanHooper1023

    Joined:
    Jan 19, 2017
    Posts:
    24
    Thanks for helping me out with this by the way I really appreciate it, I've been hung up on it for a little bit now.

    I think I understand this, but just to make sure. If I wanted to add another bundle of audioClips for the user to download I would just need to increase the maximum cache size to ensure the most recent download doesn't go over the limit? or is that just an optimization option, if not specified will it just use as much memory as needed?
     
  12. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748