By default audiosources can play only one sound and the clip they are playing is shown at audioClip field in audiosource component.
And when I tried playing music clips with playoneshot they were playing fine but 1) I couldn’t see either of those clips in the audioClip field and 2) I could control the volume, pitch of both the clips using that single audiosource.
If anyone has understood the concept then please tell me how it works or point me to the resources to understand.
Using Play One Shot plays a sound once through the audio source. It doesn’t require an Audio Clip in the field because you can play multiple sounds from one audio source using Play One Shot, which is why you need to specify the Audio Clip when calling it.
The Audio Source volume will control the level of any sound playing through that audio source. When you call Play One Shot you can choose to add a volume scale parameter, which will be a percentage (between 0 and 1) of the Audio Source’s volume.
This means you can play two sounds from one Audio Source using Play One Shot at different volumes but turn them both down using the Audio Source volume control if you want.
Generally it’s best to use Play One Shot for sound effects and other short audio clips and use Play for music and long loops (as Play One Shot can’t loop audio)
So does that mean that unity internally creates multiple audiosources (because by default only one sound can be played from an audiosource) and it then associates all the parameters like pitch, volume, etc with calling audiosource.
And what happens if I use playoneshot for a bullet sound. Will it have any performance issues?
And I read your blog and in it you say playoneshot will count as voice what do you mean by that?
You can play multiple Audio Clips through one Audio Source but only by using Play One Shot. The other play functions interrupt the playing sound.
Every Audio Clip you play through that Audio Source will use the Audio Source’s settings. It’s not the case that Unity creates multiple Audio Sources, it’s just one with multiple clips playing through it (i.e. changing something on the audio source affects all the clips playing through it).
A voice is a playing audio clip and there’s a default limit of 32 at once. A single audio source can play multiple clips (i.e. multiple voices).
For example an Audio Source playing 10 clips all at once would mean 10 voices playing. 10 Audio Sources playing one audio clip each, would still be 10 voices.
Play One Shot can increase the voice count because, for example, if you have a machine gun shot sound that starts before the previous shot ends then it counts as 2 voices. If the audio clips are long, or triggered close together, this can stack up and push you closer to the voice limit.
I have other posts on my blog that cover some of these points in more detail too. A post on unity audio optimisation and a general audio tips post that includes a graphic about voice limits:
Any idea how this works with respect to spacialization?
Specifically, does using playOneShot to a single AudioSource and playing many sounds count as 1 spacialization or 1 spacialization per voice being played?
In general my understanding is that spacializing sounds is very resource intensive so if playOneshot would be more performant that would be great.
I can’t speak for all of the different options, but I kind of know how it worked for Resonance Audio, which I realise is possibly deprecated now.
I’m pretty sure that the spatialisation processing is done at an Audio Source level (i.e. other than adding to your voice count, multiple one-shots shouldn’t affect it, so long as they use the same audio source).
Hello John,
First, thank you for your answers - very informative!
Perhaps you are familiar with the performance comparison of “AudioSource.PlayOneShot()” vs “Pool of the Audio Sources”? Means for frequent reuse.
Because I can’t figure out how AudioSource.PlayOneShot() works behind the scenes and does it create any overhead (new components, variables, etc.) or other performance issues?
For example: AudioSource.PlayClipAtPoint() - is a very heavy method where a new object is created, then a new component is created, and then they are removed.
Just find out, that PlayOneShot is not using all volume rollOff features, which is sad… Didn’t checked any other settings, but afraid that other fetures are cut off too.
Is there any way take advantage of roll off curves with PlayOneShot, instead of calculating distance by myself.