Audio Manager to handle scripted sounds in Unity


Difficulty

When adding sounds or music within a game, it is often useful to group their management within a single object. In this way, all the sounds pass through the same point and it is easier then to manage any particularities. One result is for example volume management at one point. Let’s create an Audio Manager for scripted sounds in Unity.

The Prefab object part

To do this, we create a static Prefab of an empty object to be inserted into the scenes in which we are going to produce our sounds. We will make them through scripts, usually triggers of contact with objects or damage and enhancements.

To it we associate the script that we will see shortly. This script will have the functions necessary to send the audio tracks to play.



To this Prefab must be added the AudioMixers, necessary for adjusting the sounds.
We add two to be able to differentiate:

  • sounds (like wind noise).
  • from music (like the background music of our game).

Objects of the AudioMixer type can be created as shown below, through the context menu, selecting Create and then the Audio Mixer item.

The script part

The main ingredient of our mechanism is the script that puts everything together, the mixers and the audio clips, creates an AudioSource and provides the Play() method to play the tracks.

// AudioManager.cs
using System;
using UnityEngine.Audio;
using UnityEngine;
using UnityEngine.SceneManagement;

public class AudioManager : MonoBehaviour {
    [SerializeField] private AudioMixerGroup musicMixer;
    [SerializeField] private AudioMixerGroup soundMixer;

    public static AudioManager instance;
    private static AudioSource staticSource;

    void Awake() {
        DontDestroyOnLoad(transform.gameObject);
        staticSource = gameObject.AddComponent<AudioSource>();
        staticSource.outputAudioMixerGroup = soundMixer;
    }

    public static void Play(AudioClip clip, float volumeScale = 1f) {
        if (clip != null) {
            staticSource.PlayOneShot(clip, volumeScale);
        }
    }
}


Then just add the call of the play method inside the Prefab which will produce the sound, in our case for example the damage to the Player following a blow delivered by his archenemy.

// PrefabWithSound.cs

// ...
// AudioClip initialized.
[SerializeField] private AudioClip DamagedAudioClip;

// ...
// AudioClip play inside a method.
AudioManager.Play(DamagedAudioClip, 0.8f);
// ...

Scripted sounds

The last thing to do is add our audio clip inside the field will appear in the script associated with our object that will produce the noise.
A note: the noise that we will produce will not be positioned in space but is a generic noise. This regardless of the distance from the game camera focus (and usually from our character).

Scripted sounds Unity
Scripted sounds Unity


This is the way to create scripted sounds in Unity.
Try it at home!

0
Be the first one to like this.
Please wait...

Leave a Reply

Thanks for choosing to leave a comment.
Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published.
Please do NOT use keywords in the name field. Let's have a personal and meaningful conversation.