Search Unity

Loading audio into memory

Discussion in 'Editor & General Support' started by bigbrainz_legacy, Jul 21, 2006.

  1. bigbrainz_legacy

    bigbrainz_legacy

    Joined:
    Jul 15, 2005
    Posts:
    137
    We're trying to define precisely when audio is loaded into memory. Here is what we have determined so far:

    -Assigning an audio clip to an object's Audio Source does NOT load it into memory.

    -However, setting it to AutoPlay does.

    -Playing the audio from "Update" does NOT load it into RAM until it's called.

    -But loading clips into a script using variables loads all the audio clips into memory from the beginning:
    var song1: AudioClip;
    var song2: AudioClip;
    etc . . .

    If you could elaborate on what will and won't load the song into memory that would help us with our overall Memory Management strategy.

    Thanks!
     
  2. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    This is different in the Editor and in an exported player.

    IIRC in a player, everything is preloaded when loading a scene, but the editor loads everything (or at least as much as possible) dynamically. The only exception is scene 0 which is always loaded dynamically both in the player and editor.
     
  3. bigbrainz_legacy

    bigbrainz_legacy

    Joined:
    Jul 15, 2005
    Posts:
    137
    Ah, I should have clarified--we are only concerned with the player, not the editor--thank you for helping me clarify that.

    But we're looking for much greater specificity from the developer about loading assets, specifically audio, into RAM. As I mentioned in my original post, everything is not preloaded when loading a scene, and we're looking for a specific description of what causes assets (in this case audio) to be loaded into memory.

    This is a small, but important distinction as we create the foundation of our Music Mixer. For simplicity, we'd like to pack 100+ MBs of different audio files into a single piece of code, but only load into memory the music that will be used with the current level.

    We will also use a similar technique for placing various bosses and creatures into all the different levels.
     
  4. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Unity automatically preloads all assets that will be used by the scene when loading that scene. We do this by tracing through all references from any objects in the scene.
    (Thats one of the reasons why we don't allow loading by pathname)

    So basically, don't worry it will just work itself out.

    The first scene in a player is never preloaded, but all scenes after that are preloaded. This is because the first scene is usually used as a splashscreen and you dont want to have the user wait for that only because there is a lot more data coming later. Thus the first scene is loaded on the demand, all other scenes are preloaded.
     
  5. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    But if he has one Jukebox type object with 100 MB of audio linked to it and has that in all his levels, there will be a problem as it'll be loading stuff not used.

    I think the only thing to be done is to not have unneeded audio/music linked in a scene in the Inspector.

    -Jon
     
  6. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    We do ogg vorbis decompression on the fly so that will only be a problem if you have like 2 hours of music in your game.

    And yes, then you can split it up by having a jukebox per scene.
     
  7. bigbrainz_legacy

    bigbrainz_legacy

    Joined:
    Jul 15, 2005
    Posts:
    137
    Joachim,

    Thank you so much for clearing that up for us--it's very helpful. And doing the .ogg decompression on the fly is HUGE--thanks!!

    It's particularly cool that you've set up the first level to not load assets dynamically. Is there any way we could tap into that functionality on an individual basis? For instance, if we have a huge tutorial movie that we'd like players to be able to access during gameplay, but 99% of the time they won't need it, could we tell the game to only load that asset when it's specifically triggered? (I can think of workarounds if we can't, but I just thought I'd ask).

    Thank you again for just excellent clarification.
     
  8. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    At the moment you can't but you are most likely writing movie support with a custom plugin right? In that case you can do the loading in any way you like.
     
  9. bigbrainz_legacy

    bigbrainz_legacy

    Joined:
    Jul 15, 2005
    Posts:
    137
    Excellent--thank you!