Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Resolved How to load sound files into memory only when they're needed?

Discussion in 'Audio & Video' started by QuinnWinters, Feb 26, 2023.

  1. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    494
    My game has excessively long load times of around 1 minute and 20 seconds so I did some experiments and determined it's the songs and background ambience tracks causing the load time. When I deleted all of them from the lists that contained them the load time reduced to 20 seconds total. Now that I know the source of the load time I have the task of figuring out how to fix it.

    I've got my sound set up so that every sound is a prefab with an audio clip on it. When I want to play one of the sounds I spawn the prefab. It seems that because all the prefabs are referenced in a list it loads the sounds they reference into memory when the scene loads. Breaking them into separate scenes is not an option. I have dozens of sound files that all need to be played at some point in the same scene.

    How would I go about loading sound files into memory only when they need to be played?

    Edit: I've got it figured out. See my thread in the scripting forum: https://forum.unity.com/threads/getting-an-audio-clip-from-file-via-script.1404952/
     
    Last edited: Feb 26, 2023
    Deadcow_ likes this.
  2. SeventhString

    SeventhString

    Unity Technologies

    Joined:
    Jan 12, 2023
    Posts:
    435
    Glad you found a way to make it work! Just to be sure, did you try different loading options from the AudioClip Inspector? It feels like using the "Streaming" loading type might be what you're looking for.
     
  3. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    494
    Thanks for the response. Indeed I did. I spent time researching the various loading types and compression formats and streaming vorbis is what I'm using. It's working well.
     
    SeventhString likes this.