Does anyone have any strategies for managing situations where there are too many audio sources at once? It can be a bit perplexing trying to design for every scenario when audio is triggered by explosions etc, I wonder if there is a way to cull out excess signals, like limiting the number of tracks playing at any one time.
Why dont you use a public var to hold the number of playing sounds (if there isnt one built in to Unity - i didnt check)
Every time you trigger a sound, it increments the int, and when it completes it decrements - you simply choose not to trigger the sound if the number gets too high.
something like:
function playmysound(thesound){
check how many sounds are currently playing(from your public var)
if less than 10, then
+1 to my public var
trigger this sound
when sound has finished -1 from my public var
else
do nothing
end of function
There is an OpenAL-related limit on PlayOneShots, I think. It’s pretty high, but it’s being worked on.
If you’ve got a big enough world, I suggest first making sure that you never play sounds that the player will not hear. They still require OpenAL’s involvement.
Good stuff team, I think we’re on the right track here, this will be a useful thing to resolve for us all.
I’ve got a bit on atm but with any luck I’ll come back to this thread when Im closer to, or have, a solution. Its good to have an idea how to approach this now.
Hey I brought this up with my friend Ryan who I do animation with, and as he is a pretty switched on programmer, we managed to string this all together. This type of code could probably be flexed out for a lot of purposes, and Im really thankful for his help. I dont think hes posted here yet-he said to me he wanted to wait until he had enough time to give this community the time it deserves, so I think we can all look forward to that.
Heres the scripts, a lot like Shaunis and Pete suggested. Note we are using collisions to trigger the sounds, they could be triggered in an instantite type situation too…Or whatever flavour you like…
This goes on a rigidbody+collider+audiosource object:
Call that object what you like, but make sure you call this script Audiomanager, and put it on a game object called Manager:
//Audiomanager.js
var ConcurrentAudio = 0;
function CanPlay() {
if(ConcurrentAudio == 2) {
return false;
} else {
ConcurrentAudio++;
return true;
}
}
function HasPlayed() {
ConcurrentAudio--;
}
the varibile concurrent audio defines how many sounds maximum can play at any one time. We did discuss the possibility of developing some code that can recognise some sounds as being more important than others(like a conversation being more important than a passing car, for example, and if we get something usable along those lines, I’ll bring that along too…