cancel
Showing results for 
Search instead for 
Did you mean: 

Enable / Disable Audio Sources During Runtime

Colton-Vitruviu
Protege
Hi All,

In our largest scene we have approx. 50 ambient audio sources (enemies walking around, music, etc.) in addition to many audio sources for NPCs talking (that only play while interacting with them). Currently we are enabling/disabling ambient audio sources based on distance to the main camera (greatly reduces DSP load and prior to implementing this we were having audio sources not playing) but we are finding that when/if you return to the exact same spot on the map after the audio sources in that spot have been enabled/disabled, they are quieter (25-50% reduction in volume, i.e go to one end of the map and come back, the sound will not be the same). We are using the native Oculus Audio plugin and I'm not sure what this problem is stemming from? Is there a problem with enabling/disabling audio source with regards to the mixer or onspaudiosource.cs?

Is anyone else finding this? Is there a better way to manage a large number of audio sources?

Cheers,
Colton
14 REPLIES 14

Colton-Vitruviu
Protege
Hey Peter,

I changed the above code and everything looks to be working 🙂

Cheers,
Colton

petergiokaris
Protege
Hey Colton, that's great news! Thanks for letting us know.
Peter Giokaris Senior Software Engineer

SvenViking
Protege
"petergiokaris" wrote:
In ONSPAudioSource.cs, replace the Update function with this one:

void Update()
{
// We might iterate through multiple sources / game object
var source = GetComponent<AudioSource>();
SetParameters(ref source);
}


... (this will not incur a hit, since we are only setting a handful of parameters in the spatialier audio source).

SetParameters() presumably won't, but what about GetComponent()? GetComponent() calls have traditionally been relatively slow, and it seems like this would be calling it for every ONSPAudioSource every frame.

Edit: Would it possibly work in OnEnable()?
Scorched Battalion - VR Jam edition available for Gear VR and DK2 (Win/Mac/Linux) Jungle Juice Sven Co-op

petergiokaris
Protege
From profiling, GetComponent does not incur a noticable CPU hit.
Peter Giokaris Senior Software Engineer

SvenViking
Protege
OK, thanks -- presumably they've optimised it in more recent Unity version.
Scorched Battalion - VR Jam edition available for Gear VR and DK2 (Win/Mac/Linux) Jungle Juice Sven Co-op