Managing Music and Audio

In this blog post I want to share our self-made Audio Manager for Unity 3D, which we will use in our new game and did so in our first Unity game Thunderballs. We probably will add new features to it during development of our new game, so stay tuned!
The Audio Manager allows you to set global audio volumes for music, sounds and ambience sound and provides you with options like random order, random position in a predefined area, fade in/out, cross-fade, intervals and more.

Global Volume Manager

The GlobalVolumeManger script is a singelton which is used to define the global audio volumes for 3 sound types:

  • ambience
  • music
  • sounds

These volumes can be adjusted by the player in the game settings, but for testing purposes you can set them in the inspector as well:

GlobalVolumeManager

Audio Manger

In the AudioManager you define the audio files and set various options. The interface looks like this:

AudioManager

In the above example we defined five AudioClips (bird-sounds).
The Sound Type is set to ambient, which means that the AudioManger uses the ambient volume from the GlobalVolumeManager in conjunction with the Playback Volume.

  • Play On Awake starts the playback in the Awake method,
    which gets called when the GameObject is enabled.
  • If Loop is checked, the AudioClips are played in a loop
    (one clip after the other – in the order they are listed).
  • Destroy When Finished destroys the GameObject when playback has finished.
    This is useful for sounds you only wanna play once like explosions
    sounds (instantiate an explosion sound prefab which destroys itself after the playback).
  • If Random Order is checked, the AudioClips are played in an random order.
    otherwise they are played in the order they are listed.
  • With Random Order checked, the option Avoid Repetition appears, which means that
    the same AudioClip will no be played multiple times in a row.
  • Interval From and Interval To let’s you define a time interval in seconds in which the
    AudioClip should be played. e.g. From = 2 and To = 4 from the screenshot above plays
    a bird sound every 2 to 4 seconds.
  • Overlay Time is a percentage value (between 0 and 1) and defines when the next
    AudioClip should start. e.g. if Over Time is set to 0.15  the next AudioClip
    starts 15% before the current clip has finished. This is useful for footstep sounds if the
    player is running.
  • Random Position sets the position of the GameObject randomly within the range of
    the GameObject’s transform scale property. A blue gizmo shows the area in the
    scene view when the GameObject is selected.

FadeOptions

To fade between two AudioClips, you can set the cross fade duration in the Cross Fade Time parameter. This will fade-out the current clip and fade-in the next clip using the given duration.

  • Fade In Time fades the AudioClip in, every time the clip starts playing.
  • Fade In Only Once only fades the first AudioClip in, this is useful
    in conjunction with cross fade or overlay.
  • Fade Out Time fades the AudioClip out, every time a clip finishes playing.
  • Fade Out Only Once only fades the first AudioClip out.

There are public Play and Stop methods to start/stop playback in your code. The AudioManager script uses many coroutines, which are explained in our previous blog post.

Download

You can download and use our Audio Manager for free (even commercially). If you wanna credit us, please do so – you don’t need to, though.
We can’t provide any support for the package but feel free to use and modify it in your own projects.

If you have any suggestions or tips how we can improve our Audio Manager please let us know in the comments or drop us a line on Twitter!

Christian Stuecklschweiger

About Christian Stuecklschweiger

Developer at BitsAlive Follow us on Twitter: @bitsalive

Hinterlasse eine Antwort

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind markiert *

Du kannst folgende HTML-Tags benutzen: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Current month ye@r day *

Post Navigation