Sound Manager for Unity3D

Unity3D’s default sound philosophy (2D/3D audio sources with support for directionality, volume, panning, property animations and filters like Reverb, Echo, etc.) is cool but if you want to add some simple sounds to your game really quickly and also:
– keep playing music when loading and into new scenes
– fade in/fade out sound channels
you need something more. I’m going to share with you an extemely simple Sound Manager I’ve created for the needs of my game – GraviBall. You can see the sumary of the API below. SoundManager is a singleton which survives level loads. It uses 16 AudioSources to play different sounds simultaneously and supports fade in/fade out on any number of channels. Additionally, a useful high-level routine (ShuffleMusic()) for streaming different (for each level) music clips from files is included. With all those features combined you can “soundify” your game in a matter of minutes. The code is BSD-licensed. Enjoy!

	/// <summary>
	/// Returns (creating if necessary) the SoundManager singleton.
	/// </summary>
	/// <returns>
	/// The SoundManager singleton.
	/// </returns>
	public static SoundManager GetSingleton();

	/// <summary>
	/// Immediately sets volume of the specified channel.
	/// </summary>
	/// <param name="i">Channel number</param>
	/// <param name="newVol">New volume setting, 0.0f to 1.0f</param>
	public void SetVolume(int i, float newVol);

	/// <summary>
	/// Linearly interpolates volume of the specified channel
	/// from current value to the new value during the specified time.
	/// </summary>
	/// <param name="i">Channel number</param>
	/// <param name="newVol">New volume setting, 0.0f to 1.0f</param>
	/// <param name="time">Time in seconds</param>
	public void SetVolume(int i, float newVol, float time);

	/// <summary>
	/// Immediately sets volume of the specified clip. The difference
	/// between this method and SetVolume() taking channel number as
	/// parameter is that this method will effect the setting for all
	/// channels playing the given clip.
	/// </summary>
	/// <param name="c">Audio clip</param>
	/// <param name="newVol">New volume setting, 0.0f to 1.0f</param>
	public void SetVolume(AudioClip c, float newVol);

	/// <summary>
	/// Linearly interpolates volume of the specified clip
	/// from current value to the new value during the specified time.
	/// The difference between this method and SetVolume() taking channel
	/// number as parameter is that this method will effect the setting for all
	/// channels playing the given clip.
	/// </summary>
	/// <param name="c">Audio clip</param>
	/// <param name="newVol">New volume setting, 0.0f to 1.0f</param>
	/// <param name="time">Time in seconds</param>
	public void SetVolume(AudioClip c, float newVol, float time);

	/// <summary>
	/// Plays given audio clip on any free channel.
	/// </summary>
	/// <param name="c">Audio clip</param>
	/// <param name="loop">Loop setting</param>
	/// <returns>Number of the assigned channel</returns>
	public int PlayClip(AudioClip c, bool loop);

	/// <summary>
	/// Plays given audio clip on any free channel included in the mask.
	/// </summary>
	/// <param name="c">Audio clip</param>
	/// <param name="mask">Channel mask, e.g. to specify 0th, 3rd and 11th channel, use 0x0809</param>
	/// <param name="loop">Loop setting</param>
	/// <returns>Number of the assigned channel</returns>
	public int PlayClip(AudioClip c, int mask, bool loop);

	/// <summary>
	/// Stops all channels playing the given clip.
	/// </summary>
	/// <param name="c">Audio clip</param>
	public void StopClip(AudioClip c);

	/// <summary>
	/// Stops the given channel.
	/// </summary>
	/// <param name="i">Channel number</param>
	public void StopChannel(int i);

	/// <summary>
	/// Utility function for changing music between levels.
	/// Has to be called using StartCoroutine(). Requires System.IO, therefore
	/// for maximum portability you might want to remove it along with the
	/// System.IO dependency.
	///
	/// The function lists all .ogg files in the music/ subdirectory
	/// picks one on random and starts playing it, fading out any previous music.
	/// Calling it once at the beginning of a level should achieve what people
	/// usually want.
	///
	/// Example:
	/// SoundManager sm = SoundManager.GetSingleton();
	/// StartCoroutine(sm.ShuffleMusic());
	/// </summary>
	public IEnumerator ShuffleMusic();

DOWNLOAD: SoundManager.zip

Leave a Reply

Your email address will not be published. Required fields are marked *