I’m trying to learn how to save data to files/a file and how to load and use that data in the game I’m making. I’ve done it as in this tutorial. I have another script in my game which control the audio mixers I have, that’s setup as in this tutorial. I want the volume of the audio mixers to be saved in a file and I want that file to load when the game starts(maybe the exposed parameters could be stored in the file or something). Any ideas?
You could use PlayerPrefs to store/retrieve the volumes as floats. For example it would be something like this (I haven’t tested the code, so I may have missed something, but this should get you going):
This is to save the volume:
public Slider VolumeSlider;
private string _volumeKey = "VolumeSetting";
// This could be set as the OnClick of a button in your scene
private void SaveVolume()
{
PlayerPrefs.SetFloat(_volumeKey, VolumeSlider.value);
}
Then, to retrieve the volume when the scene loads you could do:
public Slider VolumeSlider;
private string _volumeKey = "VolumeSetting";
void Awake()
{
VolumeSlider.value = GetPrefValueOrDefault(_volumeKey);
}
// Returns the value of the key, or a default value if the key isn't found
private float GetPrefValueOrDefault(string key)
{
return PlayerPrefs.HasKey(key) ? PlayerPrefs.GetFloat(key) : 0.5f;
}
This example works with UI sliders, you would just need to adapt it for use with an Audio Mixer