Hello all! I’m new to working with the Unity program, and I’ve been working on making a project recently and I’m currently following guides for making an options menu. When it came time to work on the audio settings, I just got completely stuck. I’ve spent a couple of hours now perusing forums regarding using Playerprefs to save audio settings between sessions of a game, but I just am having no luck. Currently, I’m just working on getting the master volume setting to save so that once that’s working I can apply that same code to music and SFX with appropriate adjustments. I’m working in Unity 2021.3.11f1 if that makes a difference, and from what I’ve figured using a Playerprefs key viewer from the asset store it seems like my program just isn’t properly saving an actual value? It just saves the key itself, which isn’t exactly helpful… Below is what my code looks like for my audio options including all the relevant info I can think of. I also attached the PlayerPrefsEditor window showing exactly what data is being saved to them:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Audio;
public class OptionsMenu : MonoBehaviour
{
public Toggle fullscreenTog, vsyncTog;
public ResItem[] resolutions;
private int selectedResolution;
public TMPro.TextMeshProUGUI resolutionLabel;
public AudioMixer theMixer;
public Slider masterSlider, musicSlider, sfxSlider;
public TMPro.TextMeshProUGUI masterLabel, musicLabel, sfxLabel;
public float masterVolume, musicVolume, sfxVolume;
public AudioSource sfxLoop;
public
// Start is called before the first frame update
void Start()
{
fullscreenTog.isOn = Screen.fullScreen;
if(QualitySettings.vSyncCount == 0)
{
vsyncTog.isOn = false;
}
else
{
vsyncTog.isOn = true;
}
//search for resolution in list
bool foundRes = false;
for(int i=0; i < resolutions.Length; i++)
{
if(Screen.width == resolutions[i].horizontal && Screen.height == resolutions[i].vertical)
{
foundRes = true;
selectedResolution = i;
UpdateResLabel();
}
}
if (!foundRes)
{
resolutionLabel.text = Screen.width.ToString() + " x " + Screen.height.ToString();
}
theMixer.SetFloat("MasterVol", Mathf.Log10(PlayerPrefs.GetFloat("MasterVol", 1) * 20));
}
// Update is called once per frame
void Update()
{
}
public void ResLeft()
{
selectedResolution--;
if(selectedResolution < 0)
{
selectedResolution = resolutions.Length - 1;
}
UpdateResLabel();
}
public void ResRight()
{
selectedResolution++;
if(selectedResolution > resolutions.Length - 1)
{
selectedResolution = 0;
}
UpdateResLabel();
}
public void UpdateResLabel()
{
resolutionLabel.text = resolutions[selectedResolution].horizontal.ToString() + " x " + resolutions[selectedResolution].vertical.ToString();
}
public void ApplyGraphics()
{
//apply fullscreen
//Screen.fullScreen = fullscreenTog.isOn;
if (vsyncTog.isOn)
{
QualitySettings.vSyncCount = 1;
}
else
{
QualitySettings.vSyncCount = 0;
}
//set resolution
Screen.SetResolution(resolutions[selectedResolution].horizontal, resolutions[selectedResolution].vertical, fullscreenTog.isOn);
}
public void SetMasterVol(float sliderValue)
{
//masterLabel.SetText($"{sliderValue:F0)}");
theMixer.SetFloat("MasterVol", Mathf.Log10(sliderValue) *20);
PlayerPrefs.SetFloat("MasterVol", sliderValue);
masterLabel.text = (sliderValue * 100).ToString("F0");
PlayerPrefs.Save();
}
public void SetMusicVol()
{
musicLabel.text = (musicSlider.value * 100).ToString("F0");
theMixer.SetFloat("MusicVol", Mathf.Log10(musicSlider.value) *20);
PlayerPrefs.SetFloat("MusicVol", musicSlider.value);
}
public void SetSFXVol(float sliderValue)
{
sfxLabel.text = (sfxSlider.value * 100).ToString("F0");
theMixer.SetFloat("SFXVol", Mathf.Log10(sliderValue) *20);
PlayerPrefs.SetFloat("SFXVol", sliderValue);
}
public void PlaySFXLoop()
{
sfxLoop.Play();
}
public void StopSFXLoop()
{
sfxLoop.Stop();
}
}
[System.Serializable]
public class ResItem
{
public int horizontal, vertical;
}