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

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: Getting an audio clip from file via script

1 Like

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.

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.

1 Like