I have made a options menu for my game, and its for sound volume such as music, sfx, which I am using audio mixer for, and you can control the audio volume but I need help saving it, for example, say the user loads in for the first time and the audio is too loud so the user turns it down, later the user closes the game and later loads back in, when the user loads in the settings are all back at the default settings, how do I save the settings with playerprefs so the next time the user loads in, the settings are the same? Here is my code for controlling the volume sliders if its needed and it has playerprefs used in it in a attempt to save the settings.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;
using UnityEngine.UI;
public class AudioManager : MonoBehaviour {
public AudioMixer audioMixer;
public Slider gmVol;
public Slider mmVol;
public Slider sfxVol;
void Start()
{
gmVol.value = PlayerPrefs.GetFloat("GMVolume" ,1f);
mmVol.value = PlayerPrefs.GetFloat("MMVolume", 1f);
sfxVol.value = PlayerPrefs.GetFloat("SfxVolume", 1f);
audioMixer.SetFloat("gmVol", PlayerPrefs.GetFloat("GMVolume"));
audioMixer.SetFloat("mmVol", PlayerPrefs.GetFloat("MMVolume"));
audioMixer.SetFloat("sfxVol", PlayerPrefs.GetFloat("SfxVolume"));
PlayerPrefs.Save();
}
public void SetMenuMusicVolume(float menu)
{
PlayerPrefs.SetFloat("MMVolume", menu);
audioMixer.SetFloat("mmVol", PlayerPrefs.GetFloat("MMVolume"));
PlayerPrefs.Save();
}
public void SetGameMusicVolume(float game)
{
PlayerPrefs.SetFloat("GMVolume", game);
audioMixer.SetFloat("gmVol", PlayerPrefs.GetFloat("GMVolume"));
PlayerPrefs.Save();
}
public void SetSfxVolume(float sfx)
{
PlayerPrefs.SetFloat("SfxVolume", sfx);
audioMixer.SetFloat("sfxVol", PlayerPrefs.GetFloat("SfxVolume"));
PlayerPrefs.Save();
}
}