We were running short on time, and i did not really have too much time to consider the implementation of a sound manager completely. in order to save time, i rigged up a simple sound manager class as a singleton that contains a list for each of the types of sound (gunshot, jump, punch etc). A single simple static function is used to instruct the manager to play a random sound from the list at a given location in the world. The sound choice is made with a simple enum
and a switch statement. All sound cues in the game are one shot calls to the manager that play a random sound of the required type at the location of the caller.
This is not an elegant solution, but it was fast and effective.
public class AudioManager : MonoBehaviour
{
public List<AudioClip> gunshots;
public List<AudioClip> gunHits;
public List<AudioClip> spitHits;
public List<AudioClip> jumpSound;
public List<AudioClip> dashSound;
static AudioManager instance;
public enum SoundType
{
Gun,
Hit,
Spit,
Jump,
Dash,
}
private void Start()
{
if (AudioManager.instance != null)
{
Destroy(gameObject);
return;
}
instance = this;
}
public static void PlaySound(SoundType soundType, Vector3 position)
{
if(instance == null)
{
return;
}
switch (soundType)
{
case SoundType.Gun:
if (instance.gunshots != null)
AudioSource.PlayClipAtPoint(instance.gunshots[Random.Range(0, instance.gunshots.Count - 1)], position, 1);
break;
case SoundType.Hit:
if (instance.gunHits != null)
AudioSource.PlayClipAtPoint(instance.gunHits[Random.Range(0, instance.gunHits.Count - 1)], position, 1);
break;
case SoundType.Spit:
if (instance.spitHits != null)
AudioSource.PlayClipAtPoint(instance.spitHits[Random.Range(0, instance.spitHits.Count - 1)], position, 1);
break;
case SoundType.Jump:
if (instance.jumpSound != null)
AudioSource.PlayClipAtPoint(instance.jumpSound[Random.Range(0, instance.jumpSound.Count - 1)], position, 1);
break;
case SoundType.Dash:
if (instance.dashSound != null)
AudioSource.PlayClipAtPoint(instance.dashSound[Random.Range(0, instance.dashSound.Count - 1)], position, 1);
break;
default:
throw new ArgumentOutOfRangeException("soundType", soundType, null);
}
}
```