Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Is Preloading EVERYTHING into memory at game start (textures and audioclips) a bad idea?

Discussion in 'Scripting' started by astracat111, Sep 19, 2020.

  1. astracat111

    astracat111

    Joined:
    Sep 21, 2016
    Posts:
    627
    So I've had this problem for years with my game in where when it transitions from one scene to the next it's hit with long loading times, especially recently as I had to make everything asynchronous for VR.

    An simple minded idea though came to me today....What about just preloading every texture possible into memory before the start of the game?

    I've got 200MB of .ogg files that would probably be 1.5GB when decompressed into PCM.

    ...and I've got 44MB of textures in PNG format, I don't know what they would be decompressed....

    But the question I have is how it's going to actually load this stuff into memory. It gets loaded into the devices RAM, right? Or do Textures get loaded into the GDDR ram on the graphics card?

    I'm sorry for the dumb questions...but the idea is that it'd be insanely fast transitioning from one scene to the next, then.
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    1,652
    Definitely not a dumb question. I had to look up the texture part, as i was not quite sure. Seems like OpenGL let's you virtualize the texture, storing it into RAM until it is required by an actual renderer. Not sure if this is optional or default, this may be worth a read:
    https://forum.unity.com/threads/how-does-unity-handle-texture-memory.176624/

    Probably not the best person to answer this, but you could technically load everything at the beginning, at the cost of a very long loading screen, and loading data you may not need at all. Another disadvantage is that people with little (free) memory would be locked out of your game, since it artificially increases the memory requirements, while people may be able to play it if you actually only loaded what you needed at a given time. That's one of the main reasons of loading screens after all - not having to load everything at once.
    For tiny games it' certainly possible and can make sense, but in that case nothing speaks against loading it at the proper time either, since loading screens would be non-existant. There are even games that offer mini-games in their loading screens to pass the time, which technically is 'loading an entire game at once'. The difference is size and complexity.

    Hope i'm not spreading misinformation about anything, since i did not have to worry about loading tons of data in a real project yet. I'm very much interrested in what others have to say about this topic, so definitely not a dumb question imho.
     
    astracat111 likes this.
  3. astracat111

    astracat111

    Joined:
    Sep 21, 2016
    Posts:
    627
    Nah you're answer really helps out.

    Here are the results of a first test, and as expected it extracts OGG files into big PCM data. I have 22 songs preloaded, there could be say 50+ in the final thing so preloading in chunks has to be the best option I guess, but now...



    Now I'm running into other problems, though.

    EDIT: Alright, so putting the files also in PCM format makes them load eons faster, at the expense of no more or less ram so there's no point in having them in OGG in the first place in Unity.
     
    Last edited: Sep 19, 2020
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    9,035
    Another approach I've had for audio is to make almost everything of any size be streaming, and load all the audio you want. It will not take up hardly any memory compared with loading it all regularly.

    With streaming audio, Unity will load in just enough that it can start playing the audio immediately, usually without hiccup, then it will internally begin streaming the audio in another thread. It might not ALWAYS be the best solution, but I've seen it often improve the hiccup / memory use situation.
     
    astracat111 likes this.
  5. astracat111

    astracat111

    Joined:
    Sep 21, 2016
    Posts:
    627
    For music this definitely seems to be a good suggestion, as well as Compress in Memory when loading. That has to be the best solution when streaming.

    The problem is that early on in development without thinking about the consequences of memory, I loop the audio by splitting it into two separate audioclips and using dsp samples with int loopStart and int loopEnd to get it to loop....WELL....I don't recommend doing it that way anymore!

    For this particular case since my game is episodic, I ended up having it load all AudioClips when the particular episode is loaded, and unload them when the episode is unloaded.

    The one good thing about just loading uncompressed is that it takes a single frame to load PCM data compared to decompressing from .OGG which makes the game hiccup, something that you can't have when you're designing for VR.

    Having everything asynchronous with IEnumerators has been a b*#$@ to reprogram x__x

    One way better idea if you're making something that's retro or pixels for music is use this asset:
    https://assetstore.unity.com/packages/tools/audio/midi-tool-kit-pro-115331

    It seems to come with a general midi SF2, and you can probably replace it, so the only thing you gotta have loaded into RAM for music is the soundfont. MIDI still isn't a bad option my eyes. With MIDI files using a DAW I think there's a note event you can put in specifically for looping audio.

    In any case, for anyone reading this....the two things that seem to be the biggest problem are Textures and AudioClip's when dealing with keeping your RAM down...
     
    Kurt-Dekker likes this.
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    9,035
    @astracat111 ( who has a dog not a cat avatar... head explode) :

    Thank you for taking the time to impart some interesting gems up there, the OGG demux time, your solution of episodic packaging, etc... sounds like you've done some homework and we all appreciate the benefit!

    As always, the never-ending battle to preserve resources on our minspec machines!
     
  7. astracat111

    astracat111

    Joined:
    Sep 21, 2016
    Posts:
    627
    @Kurt-Dekker For sure x__x A few changes are the difference between 1GB and 50MB being used of RAM sometimes.
     
    Kurt-Dekker likes this.
unityunity