Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Need help controlling music, sfx, voice volumes seperately

Discussion in 'Scripting' started by fstim82, Oct 28, 2011.

  1. fstim82

    fstim82

    Joined:
    May 4, 2010
    Posts:
    28
    Unity audio comes down to using audio sources, but the sources don't have tags or any way that I'm aware of to control volume separately for voice, SFX, and music. How can I separate audio sources categorically so I can control them that way in script?

    EDIT: Continuing to think about it, perhaps an audio clip naming convention is the only option? You could search them all out on an AudioManager.Start(), but then what of dynamically loaded prefabs? We don't want to go searching for audio clips every frame. Just thinking out loud here...
     
    Last edited: Oct 28, 2011
    yamen2 likes this.
  2. jfeliu

    jfeliu

    Joined:
    Apr 27, 2011
    Posts:
    24
    Hi fstim82,
    I had to tackle with similar problems before, the way I do it is to have a centralized AudioManager Class which needs to be always active during the gameplay (see http://unity3d.com/support/documentation/ScriptReference/Object.DontDestroyOnLoad.html ). Now, I usually set two AudioSources in the AudioManger, one for my music, and one for my SFX, and use PlayOnce() every time I need one or the other depending on the needs (Music or SFX). So that they maintain an specific volume throughout the game.
    Now, must f the games I develop are 2.5d games which do not require 3D sound positioning. If that is your case I have another solution for you.

    Create the AudioManager but have the following function in it.

    Code (csharp):
    1.  
    2. public static void playMusic(AudioSource source, AudioClip clip)
    3. {
    4.     if(source != null  clip != null)
    5.     {
    6.         source.clip = clip;
    7.         source.volume = AudioSource.musicVolume; // a variable defined in the AudioManager as the music volume
    8.         source.Play();
    9.     }
    10. }
    11.  
    And do a similar function for SFX and Voice so that you can define a different Volume for each audio sector.

    I understand this could create complications if you allow volume to be change during gameplay (because you would need bump into the same problem you had before), but if the volume is changed only at an options menu, you could use a method similar to the one I posted.

    Of course, this solution will only work if each one of the sounds are played programmatically.

    I hope this helps :) .
     
    yamen2 likes this.
  3. fstim82

    fstim82

    Joined:
    May 4, 2010
    Posts:
    28
    Awesome. I'm doing a 2D app so that's perfect. Thanks so much! Hopefully Unity will add this feature in the future though. It seems pretty standard to me.
     
  4. Michael-ClockStone

    Michael-ClockStone

    Joined:
    Jan 18, 2012
    Posts:
    90