Encounter / Assets / Scripts / GUIHandlers / Settings.cs
Settings.cs
Raw
using System;
using System.IO;
using UnityEngine;
using UnityEngine.UI;

public class Settings : MonoBehaviour
{
    [Header("Buttons and Ranges")]
    [SerializeField] private Slider musicSlider;                // Slider attributed to the Music volume
    [SerializeField] private Slider soundFxSlider;              // Slider attributed to the Sound FX volume
    [SerializeField] private Slider dialogueSlider;             // Slider attributed to the Dialogue volume
    [SerializeField] private Toggle curMaxSwapToggle;           // Toggle attributed to the switching of current and max stat placements in GUIs
    
    private bool curMaxSwapToggleState;                         // True should be CUR / MAX, false should be MAX / CUR
    private float musicVolume;                                  // The volume of the Music
    private float soundVolume;                                  // The volume of the Sound FX
    private float dialogueVolume;                               // The volume of the Dialogue

    // Constant values to prevent acccidental errors for the string keys in player prefs
    private const string MusicV         = "MusicV";
    private const string SoundV         = "SoundV";
    private const string DialogueV      = "DialogueV";
    private const string CMSwitch       = "CurrentMaxPlacementSwitch";

    /// <summary>
    /// Retrieves player settings values from system, otherwise resets the values if an error occurs
    /// </summary>
    private void Start()
        => ExtractPlayerPrefs();
    private void ExtractPlayerPrefs()
    {
        try
        {
            if (!PlayerPrefs.HasKey(MusicV) || !PlayerPrefs.HasKey(SoundV) || !PlayerPrefs.HasKey(DialogueV) || !PlayerPrefs.HasKey(CMSwitch))
                throw new InvalidDataException();

            musicVolume = PlayerPrefs.GetFloat(MusicV);
            soundVolume = PlayerPrefs.GetFloat(SoundV);
            dialogueVolume = PlayerPrefs.GetFloat(DialogueV);

            curMaxSwapToggleState = PlayerPrefs.GetInt(CMSwitch) is 1;
        }
        catch
        {
            musicVolume = .5f;
            soundVolume = .75f;
            dialogueVolume = .75f;

            PlayerPrefs.SetFloat(MusicV, musicVolume);
            PlayerPrefs.SetFloat(SoundV, soundVolume);
            PlayerPrefs.SetFloat(DialogueV, dialogueVolume);

            PlayerPrefs.SetInt(CMSwitch, 0);
        }
        finally
        {
            musicSlider.value = musicVolume;
            soundFxSlider.value = soundVolume;
            dialogueSlider.value = dialogueVolume;

            curMaxSwapToggleState = PlayerPrefs.GetInt(CMSwitch) is 1;

            PlayerPrefs.Save();
        }
    }

    /// <summary>
    /// Changes the volume of the music and saves it to the player prefs while changing the volume on the music manager source
    /// </summary>
    public void ChangeMusicVolume()
    {
        musicVolume = musicSlider.value;

        PlayerPrefs.SetFloat(MusicV, musicVolume);
        MusicManager.Instance.GetAudioSource().volume = musicVolume;

        PlayerPrefs.Save();
    }
    /// <summary>
    /// Changes the sound effect volume and saves it to the player prefs while changing the sound manager volume
    /// </summary>
    public void ChangeSoundVolume()
    {
        soundVolume = soundFxSlider.value;

        PlayerPrefs.SetFloat(SoundV, soundVolume);
        foreach (AudioSource source in SoundManager.Instance.GetAudioSources())
            source.volume = soundVolume;

        PlayerPrefs.Save();
    }
    /// <summary>
    /// Changes the dialogue volume and saves it to the player prefs, if available the dialogue manager's volume will be changed
    /// otherwise the function will simply not act on that part of the function
    /// </summary>
    public void ChangeDialogueVolume()
    {
        dialogueVolume = dialogueSlider.value;

        PlayerPrefs.SetFloat(DialogueV, dialogueVolume);
        try
        {
            DialogueManager.Instance.GetAudioSource().volume = dialogueVolume;
        }
        // This catch simply stops the function if the dialogue manager is not available to be changed
        catch (NullReferenceException) when (DialogueManager.Instance == null || DialogueManager.Instance.GetAudioSource() == null) { }

        PlayerPrefs.Save();
    }
    public void ChangeCurMaxPositioning()
    {
        curMaxSwapToggleState = curMaxSwapToggle.isOn;
        
        PlayerPrefs.SetInt(CMSwitch, curMaxSwapToggleState is true ? 1 : 0);
        PlayerPrefs.Save();

        Globals.SwitchCurMaxPositions(curMaxSwapToggleState);
    }
}