Simple AudioManager code for your Unity3d project

You know, sometimes very simple code has to be written again and again by lots of different people to solve the same basic problem. I think playing audio is one of such cases in Unity3d.

You have the audio components: AudioSource (attached to your sound emitter) and AudioListener (attached to the main camera). Now, the problem is that audio clips are represented by the AudioClip class, and each AudioSource can have a single AudioClip playing at the moment. That is sometimes ok, but other times, you want your game object to be able to play a number of different sounds, maybe at the same time (Think of a character playing footsteps sounds and maybe saying something at the same time).

Since it is very natural to have one manager object in your scene acting as a singleton, I’ve written a component that can be attached to such manager. The work of this component is to play almost any sound you want to play in your game. The code is very simple and self explationary.

// /////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Audio Manager.
//
// This code is release under the MIT licence. It is provided as-is and without any warranty.
//
// Developed by Daniel Rodríguez (Seth Illgard) in April 2010
// http://www.silentkraken.com
//
// /////////////////////////////////////////////////////////////////////////////////////////////////////////

using UnityEngine;
using System.Collections;

public class AudioManager : MonoBehaviour
{
    public AudioSource Play(AudioClip clip, Transform emitter)
    {
        return Play(clip, emitter, 1f, 1f);
    }

    public AudioSource Play(AudioClip clip, Transform emitter, float volume)
    {
        return Play(clip, emitter, volume, 1f);
    }

    /// <summary>
    /// Plays a sound by creating an empty game object with an AudioSource
    /// and attaching it to the given transform (so it moves with the transform). Destroys it after it finished playing.
    /// </summary>
    /// <param name="clip"></param>
    /// <param name="emitter"></param>
    /// <param name="volume"></param>
    /// <param name="pitch"></param>
    /// <returns></returns>
    public AudioSource Play(AudioClip clip, Transform emitter, float volume, float pitch)
    {
        //Create an empty game object
        GameObject go = new GameObject ("Audio: " +  clip.name);
        go.transform.position = emitter.position;
        go.transform.parent = emitter;

        //Create the source
        AudioSource source = go.AddComponent<AudioSource>();
        source.clip = clip;
        source.volume = volume;
        source.pitch = pitch;
        source.Play ();
        Destroy (go, clip.length);
        return source;
    }

    public AudioSource Play(AudioClip clip, Vector3 point)
    {
        return Play(clip, point, 1f, 1f);
    }

    public AudioSource Play(AudioClip clip, Vector3 point, float volume)
    {
        return Play(clip, point, volume, 1f);
    }

    /// <summary>
    /// Plays a sound at the given point in space by creating an empty game object with an AudioSource
    /// in that place and destroys it after it finished playing.
    /// </summary>
    /// <param name="clip"></param>
    /// <param name="point"></param>
    /// <param name="volume"></param>
    /// <param name="pitch"></param>
    /// <returns></returns>
    public AudioSource Play(AudioClip clip, Vector3 point, float volume, float pitch)
    {
        //Create an empty game object
        GameObject go = new GameObject("Audio: " + clip.name);
        go.transform.position = point;

        //Create the source
        AudioSource source = go.AddComponent<AudioSource>();
        source.clip = clip;
        source.volume = volume;
        source.pitch = pitch;
        source.Play();
        Destroy(go, clip.length);
        return source;
    }
}

As you can see, this class is basically 6 overloads for the Play function. Basically we have two types: the type that takes a transform (emitter) as parameter and the type that takes a Vector3. The only difference is that if you provide a point (Vector3), the sound will be played there, but if you provide an emitter, the sound will be played where the emitter is, but will also follow the emitter as it moves (e.g. the engine of a passing car).
Hope you find it useful!
By the way, I’m planning on creating on tutorial on how to create Manager systems. By that I don’t mean simple components like this but rather the best way I have found to create such a Singleton object with those components, and how to integrate that with your game when you have multiple game states and game scenes (with potentially different managers). More on that soon.