Search Unity

What's the best way to deal with audio volumes?

Discussion in 'Scripting' started by psychicparrot, Dec 4, 2012.

  1. psychicparrot

    psychicparrot

    Joined:
    Dec 10, 2007
    Posts:
    874
    I'm dealing with audio (in C#) and the way I have it set up is that I have a singleton audio class storing music and sound effects volume levels (just a couple floats). Whenever anything wants to play a sound, it looks to the audio class to find out what volume to play it at.

    The pain happens when I change audio levels from the options menu during the game and I have sounds already playing (some looping), as I need to tell everything that the volumes have changed. The way I'm doing this right now is by having all sound gameobjects on an 'sfx' layer and music on the 'music' layer. If the volume gets changed, I can loop through every transform in the scene to find gameobjects on the right layers and change their volume settings. I have an audio volume setting script attached to each gameobject that stores its base volume level and then scales the volume from a new value passed in when the global levels change via the options menu sliders. This just seems really messy to me and I'm looking for a cleaner method.

    So I wonder how other people do it. Anybody got a cleaner system than that? I want sliders for both sfx and music, I want them to set individually and I would like individual sounds to be able to have different volume levels that are scaled by the sliders in the options menu.

    Cheers! I'm looking forward to your suggestions :)
     
    Last edited: Dec 4, 2012
  2. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,486
    I would think that since you can not funnel audio sources into one source... Like one would do on a mixing board, that what you are doing is the best I can think of.
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,224
    Change the audio listener volume for sound effects, and play music using AudioSource.ignoreListenerVolume.

    --Eric
     
  4. psychicparrot

    psychicparrot

    Joined:
    Dec 10, 2007
    Posts:
    874
    Thanks for the replies, chaps!

    Eric, I like it!... very neat :)
     
    Last edited: Dec 4, 2012
  5. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,486
    Good stylings Eric!
     
unityunity