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 ;)
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 $$anonymous$$/max for volume.
The nice thing is, all those should be easy to look up, Faster than even writing a Q here.
Answer by Arsonistic · Jan 05, 2019 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.
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;
}
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.
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;
public Slider volumeSlider;
visual studio doesn't recognise slider.They ask if I'm asking any assembly reference
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
Follow this Question
Related Questions
Adjusting Audio Volume via UI Slider 0 Answers
Disabling the AudioListener works in Editor but not in Build. 2 Answers
[SOLVED!] PlayerPrefabs SetFloat and GetFloat is not working 0 Answers
Volume Slider not saving when changing scene 1 Answer
How to set a button to change slider to specific value 0 Answers