• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
2
Question by WolfpackPancake · Dec 14, 2015 at 03:29 PM · c#uiaudioslidervolume

How Do I Make A Change A Volume With A Slider?

Im currently working on a settings tab for my game and I was wondering How Do You Effect Volume With A Slider?. Now I know this is kind of a vague question, but I couldn't figure out how to break this question into a simpler part

Feedback is always appreciated ;)

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KdRWaylander · Dec 14, 2015 at 04:33 PM 0
Share

Are you talking about scale ?

avatar image Owen-Reynolds · Dec 14, 2015 at 05:53 PM 0
Share

break this question into a simpler part

Easy: how to make a slider. How to read the value from a slider. How to set volume. What are the values min/max for volume.

The nice thing is, all those should be easy to look up, Faster than even writing a Q here.

3 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by Arsonistic · Jan 05 at 06:43 AM

Let me just say "DEAR LORD, NO" at the prospect of changing the volume of individual AudioSources with an option slider, like others here have suggested.

What you should be using for Volume control is an AudioMixer, through which you can even set up multiple MixerGroups to control different parts of the audio (like Music and SFX).

To do this you'll first need to create an AudioMixer asset in your project. You can then double-click it to open it in the AudioMixer window. In that window you can add more mixer groups. Select the mixer group you want to control the volume of, right-click the Volume parameter in the Inspector and click "Expose to script". You can then change the name of this exposed parameter to something convenient in the AudioMixer window; "MasterVol" is a good start. Now that the parameter is exposed you can change it in a script with the SetFloat function. Ensure that each of your audio sources are using the Mixer you wish to control them through.

Finally, here's a script for interfacing between the Slider and the exposed Volume parameter:

 using UnityEngine;
 public class VolumeSlider : MonoBehaviour {
     public UnityEngine.UI.Slider slider;
     public UnityEngine.Audio.AudioMixer mixer;
     public string parameterName;
     
     void Awake(){
         float savedVol = PlayerPrefs.GetFloat(parameterName, slider.maxValue);
         SetVolume(savedVol); //Manually set value & volume before subscribing to ensure it is set even if slider.value happens to start at the same value as is saved
         slider.value = savedVol;
         slider.onValueChanged.AddListener((float _) => SetVolume(_)); //UI classes use unity events, requiring delegates (delegate(float _) { SetVolume(_); }) or lambda expressions ((float _) => SetVolume(_))
     }
     
     void SetVolume(float _value){
         mixer.SetFloat(parameterName, ConvertToDecibel(_value/slider.maxValue)); //Dividing by max allows arbitrary positive slider maxValue
         PlayerPrefs.SetFloat(parameterName, _value);
     }
     
     /// <summary>
     /// Converts a percentage fraction to decibels,
     /// with a lower clamp of 0.0001 for a minimum of -80dB, same as Unity's Mixers.
     /// </summary>
     public float ConvertToDecibel(float _value){
         return Mathf.Log10(Mathf.Max(_value, 0.0001f))*20f;
     }
 }

NOTE: slider MaxValue needs to be above 0 and values below 0 are ignored, so MinValue should be 0

Reference your Slider in the "slider" field, reference the mixer you wish to control in the "mixer" field and enter the name of the exposed Volume parameter you wish to change in the "parameterName" field (e.g. "MasterVol").

That's it. You can also put that script on other sliders with different "parameterName" values to control other MixerGroup volumes (e.g. "Music" and "SFX").

The script also saves and loads your setting from/to PlayerPrefs, using the name of your exposed parameter, so it doesn't change between scenes or sessions.

Bonus: There's a Unity tutorial on how to expose AudioMixer parameters, if you got lost somewhere in my text, just don't use the same slider min and max values as they do, they don't do the logarithmic conversion I do in my script. There are other useful tutorials in the Audio-section as well if you're new to the topic.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
3

Answer by RedHedZed · Dec 14, 2015 at 04:48 PM

You would write a public function and assign to it the slider's OnValueChanged.

The minimum and maximum values of the slider should be 0 and 1. In your function, you can then assign the value of the slider to either the AudioListener's volume, or an AudioSource's volume. Changing the AudioListener's volume will affect all audio in the game. Changing an AudioSource's volume will affect only the sounds/music from that specific AudioSource.

 public void OnValueChanged (){
     AudioListener.volume = mySlider.value;
 }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by welpie21 · Dec 14, 2015 at 04:42 PM

using UnityEngine;

using UnityEngine.UI;

using System.Collections;

public class ChangeVolume : MonoBehaviour {

 public Slider volumeSlider;
 public AudioSource volumeAudio;

 public void VolumeController(){
     volumeSlider.value = volumeAudio.volume;
 }


}

make a canvas.. in the canvas you'll put or drag a slider in the canvas. click on the slider and drag down the script.

and also you have a OnValueChanged (single). you have in the box a ( + ). you click on that you will see something. and drag the slider under the runtime thing. and check also if you put the slider in the script of the volumeController. and also the same with the audio source.

until you'll did that.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RedHedZed · Dec 14, 2015 at 04:51 PM 0
Share

Hmm, wouldn't this set the slider's value to be equal to the volume, and not the other way around? I thought we were trying to set the volume. In your example, should it not be more like this:

 volumeAudio.volume = volumeSlider.value;
avatar image KdRWaylander RedHedZed · Dec 15, 2015 at 08:45 AM 0
Share

Yes indeed, it's coded in the wrong way !

avatar image Nightmare25 · Jun 17, 2018 at 09:45 AM 0
Share

public Slider volumeSlider;

visual studio doesn't recognise slider.They ask if I'm asking any assembly reference

avatar image DimlyMad Nightmare25 · Jun 27, 2018 at 04:45 PM 0
Share

Did you write

using UnityEngine.UI?

avatar image Benny_Whenny · Aug 27, 2018 at 06:10 AM 0
Share

Am i right in thinking that this script needs to be added to all the audio sources in the game as well as the slider?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Adjusting Audio Volume via UI Slider 0 Answers

What Am I doing Wrong Here 1 Answer

Disabling the AudioListener works in Editor but not in Build. 2 Answers

Unity custom Audio Manager won't work with custom Stop function 0 Answers

Slider OnValueChanged not letting me chose a function to send it to 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges