I've written a custom class that creates a fixed number of audio sources. When a new sound is played, it goes through the class, which creates a queue of sounds that will be played during that frame. The sounds that are closer to the camera are given preference.

If new sounds arrive in the next frame, I have a complex set of rules that determines how to replace the old ones. Ideally, "big" or "important" sounds should not be replaced by small ones. Sound replacement is necessary since the game can be fast-paced at times, and should try to play new sounds by replacing old ones. Otherwise, there can be "silent" moments when an old sound is about to stop playing and isn't replaced right away by a new sound. The drawback of replacing old sounds right away is that there is a harsh transition from the old sound clip to the new one.

But I wonder if I could just remove that management logic altogether, and create audio sources on the fly for new sounds. I could give "important" sounds more priority (closer to 0 in the corresponding property) as opposed to less important ones, and let Unity take care of culling out sound effects that exceed the channel limit. The only drawback is that it requires many heap allocations.

I wonder what strategy people use here?


1 Answer 1


But I wonder if I could just remove that management logic altogether, and create audio sources on the fly for new sounds.

Don't create and destroy "numerous" audio sources on each frame, you'll just slow the game down eventually. Allocation is not your only issue there, but also the garbage collector kicking in to clean things.

Keep on using a constant number of audio sources and switch old sounds with new ones. To avoid what you mention to be a harsh transition, add a smooth transitioning effect. You can do more than audioSource.sound = newSound; to change a sound.

For example, when it's time to switch to a new sound, quickly lower the volume of the old sound, replace the old sound, then raise the volume again. Set this interval to whatever value you find to work best for your case.

Another example would be to use two audio sources for a single transition, and crossfade (audio source 1's volume is lowered, audio source 2's volume is increased) between the two different sound effects.

  • \$\begingroup\$ Yeah, I've resisted the route of creating audio sources on-the-fly and just stuck with method of using a fixed number of audio sources. Usually the audio sources closest to the camera are played first, with the exception of "high-priority" sounds which get first dibs. Your interpolation replacement technique sounds interesting...only problem is that another sound could replace that replacement when you are changing volume levels. Having two audio sources for the transition would unfortunately use an extra audio channel. Is there any book or article on managing too many sounds at once in games? \$\endgroup\$
    – luxchar
    Aug 23, 2014 at 0:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .