- Home /
How to save an Audio Mixer's value wich is adjusted with a Slider?
Hi Guys! I'm new to Unity and I couldn't figure it out how to save the value of an Audio Mixer properly.
I have 3 scenes in my game: Start menu, Main menu, Gameplay scene;
I have 3 Audio Mixers and 3 Sliders to adjust the main music, the sfx and the button click sound.
Each mixer is attached to its slider via script using its exposed parameter and it works correctly.
My sliders are in my main menu scene's audio window and i tried to put a saving with playerprefs script on my sliders but it only worked partially.
Because when i start the game i have to go into the audio window to get( "Hear" )the changed audio volume which i have adjusted when I started the game previously.
Otherwise my adjustments stays ignored.
Please help me make my adjustments take effect at the start of my game. I really appreciate any help.:) Thx Adam Gombos
Answer by poisoned_banana · Apr 10, 2018 at 02:28 PM
For simple values like this use PlayerPrefs. Also I would advise you to create a VolumeManager which exists throughout your whole game. So you can change the volume at any time. For example:
public class AudioManager : MonoBehavior{
[SerializeField]
private AudioMixer audioMixer;
public static AudioManager instance;
void Awake(){
if(AudioManager.instance == null){
DontDestroyOnLoad(gameObject);
AudioManager.instance = this;
}
else
Destroy(gameObject);
}
void Start(){
//Get the saved music volume, standard = 10f
float music = PlayerPrefs.GetFloat("Music",10f);
//Set the music volume to the saved volume
AdjustMusicVolume(music);
}
public void AdjustMusicVolume(float volume){
//Update AudioMixer
audioMixer.SetFloat("Music",volume);
//Update PlayerPrefs "Music"
PlayerPrefs.SetFloat("Music",volume);
//Save changes
PlayerPrefs.Save();
}
}
Put this script on a new GameObject in your MainMenu Scene and you can change the volume from anywhere with AudioManager.instance.AdjustMusicVolume(0.3f)
Your answer
Follow this Question
Related Questions
Highscore won't save 3 Answers
Save game support, how do I load audio in a sertain time frame? 0 Answers
Instantiate a prefab just one time 1 Answer
Where do I put PlayerPrefs.SetInt? 1 Answer
PlayerPrefs.DeleteAll() not deleting unless app restarts 0 Answers