Hi guys, can anyone help me with this issue please?
I want to play audio from animation keys, but if I create a function with an audio clip as parameter and set it to an audio source and play it, It will stop any audio that is still playing. I can create multiple sources in a object and check if anyone of them is idle, but if all of them are playing, I need to override one of them, and I don’t want to. My idea is to create a pool of audio sources in an object and if the number of audio clips exceeds the number of sources, I instantiate new ones. Also I want to pass metadata, some struct probably, with the audio source parameters I want, i.e. loop, volume, pitch, etc… I think I’m complicating things. is there a way to play an undefined number of audio clips from animations whithout all this work?
i.e. add several animation keys with audio clip as parameter (or some kind of data structure with other parameter) and play them all without worring at the audio source? I’m doing a 2D game, so spatial audio is not as relevant
All your audio in your project should be handled by a singleton audio manager that has as many audiosources as you want your project to be limited to playing at the same time. You can access the singleton from any code, so you can just request it to play things from anywhere. You don’t create or destroy audiosources as you need them, you just create the maximum number when you start your project. When your audio manager is requested to play something, it looks for the first audiosource that is not playing, and it uses that audiosource. All your audio and requests are being handled by a single object that knows about all other audio and requests, and that is very useful for mixing, implementing priorities, conditional limits etc… just gives you much more control.
Don’t use PlayOneShot for the convenience (which does create and destroy audiosources in the background for you), because it has limitations down the line that make it useless in most cases outside of basic prototyping.
I don’t think that’s true, do you have source for this ? IIRC it registers as additional voice in the audio system but that’s not the same
it’s perfectly fine to use for one shots 2D clips, has also an additional benefit in that more clips can be played simultaneously on one AudioSource; never had problems with its intended usage really
Interesting, I think they changed how it works over the years. It’s now like a mini audio group which is actually very useful.
The issue is that people typically use PlayOneShot as an excuse to not write an audio manager, which inevitably limits them for an initial small convenience.
If you’re going to use PlayOneShot, then use it in certain circumstances inside your Audio Manager. An Audio Manager is as convenient to use as PlayOneShot, except that it is potentially much more powerful.
And if your audio is only 2D, then writing an audio manager is easy because your manager doesn’t even have to move audiosources around to match the spatialization of the objects requesting a play.
Doubt it, it’s the same since cca 1.0 I guess
It’s useful this way only for short-ish sfx clips
hey guys, thanks for the suggestions!
so, from what you say i’ll keep fiddling with my own attempt to manage the instances. ok, duly noted!
- I have a follow up question - does anyone know of a audio manager for unity2D that could eliminate the need to make my own? I wanted to focus my dev hours on the AI of my game wich would be crucial and this audio issue seems to be a lot of time invested to reinventing the wheel - for something that isn’t even groundbreaking or out of the norm for managing basic audios in the first place…
researched what you were saying about play one shot to see what its about
Unity - Scripting API: AudioSource.PlayOneShot