I have been having the worst trouble with the sound in my game.
I have the start of a race where everyone is reving up their engines. (thats 5 3D sfx)
Then the 321 go! sequence (another 2 2D sfx)
There is the background music playing (1 2D sfx)
There are different ambient city environment sounds looping (3 3D sfx)
There are the regular sfx for the car (5 3D sfx)
Ok, so that’s a lot of sound effects, but the priority option goes to 256 layers… I don’t see why 1/4th of my sounds don’t play. Occasionally, I can hear this crackling sound on one of the car’s sound effects. What is going on? Could it be my sound card, or am I breaking some rules? I first imagined that I could have 256 sounds playing all at once, but where have I gone wrong?
I’ve found that if you try to play more than 20 sounds at the same time through an audiosource / audiosources unity will stop playing audio entirely.
One technique that is simple is to count how many sounds you are playing and what their duration is, when they stop playing (using clip.length) uncount the sound. If the amount of sounds playing is more than 20 deny any new sounds from playing until less than 20 sounds are playing.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/*
really simple audio manager, to stop unity audio from crackling or becoming inaudible
hawken king 2016
*/
public class AudioPlayer : MonoBehaviour {
public static AudioPlayer instance;
public AudioSource audioSource;
int playing;
void Start()
{
instance = this;
}
public void PlayAudio(string clip)
{
if (playing > 20) return;
StartCoroutine(Playclip(clip));
}
IEnumerator Playclip(string clip)
{
playing++;
AudioClip a = Resources.Load("Audio/"+clip) as AudioClip;
audioSource.PlayOneShot(a);
yield return new WaitForSeconds(a.length);
playing--;
}
}
Unity is virtualizing AudioSources,
when there’s more AudioSources playing
than available hardware channels. The
AudioSources with lowest priority (and
audibility) is virtualized first.
Priority is an integer between 0 and
256. 0=highest priority, 256=lowest priority
I forgot to mention one little detail, and that was that I had an audio reverb zone set up in one area of my scene. I deleted it, and everything works now…