- Home /
Ambient audio
Hey,
I'm working on a 2D platformer and each rooms are new scenes, to prevent the ambient sounds from getting cut off during scene transitions i created a persistent level that is always active, and i've placed the ambient audio sources there, this way i'm able to hear a waterfall that is in another scene from a distance, for example.
My issue is that having the whole game's ambient sounds loaded in the memory sounds like a bad idea, and even if i only activated the audiosources when in range, the inactive audiosources would still have their audioclip loaded into the memory.
I'd like to know if it is possible to fill my whole gigantic persistent level with audio sources and not have them use any resources until the player is in hearing range, if not , do you guys have any suggestions?
Thanks
Answer by slavo · Dec 18, 2016 at 10:51 PM
You can set preloadAudioData in inspector of clip to false, and then just load and unload clips as you need them.
https://docs.unity3d.com/ScriptReference/AudioClip-preloadAudioData.html https://docs.unity3d.com/ScriptReference/AudioClip.LoadAudioData.html https://docs.unity3d.com/ScriptReference/AudioClip.UnloadAudioData.html
I haven't heard of this before, most people have suggested that i use Resources.Load to load and unload clips from memory, when Preload Audio Data is set to false, an audio source won't have the audio clip taking memory at all until i call AudioClip.LoadAudioData? So loading it and unloading it should work with a trigger?
I've set Preload audio data to false and the audio clip still plays on awake.
It seems to work when i play, load and unload it manually, but when i call clip.UnloadAudioData(); I get a really long freeze, is it possible to unload the audio data without getting a big lag spike?
For some reason my OnTriggerEnter2D was being called repeatedly and loading the clip multiple times, once fixed and loaded once only, it can be unloaded without a hitch
I'll mark this as an answer, i'll have to run some tests but it seems to be the best solution so far, thanks for the help slavo.
Your answer
Follow this Question
Related Questions
Audio zones for ambient sounds 2 Answers
Unity 5 Audio Source volume falloff for mono sound without panning 0 Answers
Why does my audio stop working? 1 Answer
Change Audio source on collision 1 Answer
Clear microphone data 3 Answers