Search Unity

Store many references

Discussion in 'Scripting' started by Dextozz, Oct 9, 2019.

  1. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    493
    I'm going to be using a bunch of short audio clips for my app. Right now I have around 20-30, but this number can grow up to 2000 later in the apps life. I'm using asset bundles to store these audio files on the server and download them outside of the play store. Once they are downloaded they are used in the code somewhere. Right now, I'm downloading them in a single array and picking the ones I need by a specific naming convention I implemented. Basically, I have a working system now, I just need to know how to make it more efficient so it can be used without the need to expand.

    As I mentioned, they are being downloaded in a single array and there can be up to 2000 of them. I have an idea of how I'm going to be finding the ones I need somewhat efficiently (binary search), however, I'm afraid that an array of 2000 audio clips is going to take a ton of memory space. Any idea on how to create a large number of references without storing them in a huge chunk of memory? Keep in mind that a lot of these audio files won't be used always, some of them might be used, other might not, it might be up to the user to chose which ones he wants to use and which ones he doesn't.
     
  2. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    Presumably they won't all be on the player's screen at once, so you only really need to load as many as you can see at one time.
     
  3. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    I'm not sure I fully understand your issue. Yes 2000 Audio Clips are going to take up a fair amount of storage no matter what. But do they need to be loaded all at once into memory? Can't you load/unload them as needed? Plus, putting them into an array will create very Little Overhead somewehre around 10-20 Bytes per Clip. At 20 Bytes and 2000 Clips we are talking about 40 Kilobytes, hardly something to worry over. Can you tell us more about the use case you have?
     
  4. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    493
    40kb only? Seriously? How can I measure the size of an array with all those audio clips?
     
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    The problem is not the array, it's downloading all of the audio clips at once.

    It really sounds like you should use addressable for this. That'll allow you to chuck the audio clips into different asset bundles, so you don't have to download all at once, without that being much harder to manage.
     
  6. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    I may have phrased that carelessly. When you create a List of objects (and audioclips are objects), the list only stores the pointer to that object (Clip) into the Array/List. The Array itself is small, the total amount of data (including the audio data of the Clip objects themselves) probably isn't. But since it's unlikely that you'll be playing all Clips at once, you don't Need to have all Clips in Memory at the same time, only those that are likely to be played. Hence, you only Need a fraction of the total Memory amount that you would Need if you had all Clips in Memory at the same time.

    So the real question is: can you devise a strategy that allows you not to have all autio pre-loaded.
     
  7. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    493
    Why? I could do that, but I have no knowledge of which audio files the user will choose, therefore I would have to download them all anyway? How does splitting them into chunks make a difference?

    ---------------------------------------------------------------------------------------------------------------

    You're basically saying that AssetBundle.LoadAllAssets() shouldn't be used? By using that I would store all of them in the memory, right? The only thing I can think of is loading all assets, taking the ones I need and unloading the rest. Will that suffice?
     
  8. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    You want to download them all eventually, but to begin with you only want the user to download the ones they need right away. That's the difference between waiting 5 seconds for the files they need now and a couple of minutes to download all the files they might eventually need.

    There's nothing worse than downloading an app only to have it go "lol, please wait while I actually download!".
     
  9. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    493
    Hmmm, that's a nice idea. I agree. Thanks.