UnityGameProjectsCode / InTheDarkGame / Managers / SettingsControl.cs
SettingsControl.cs
Raw
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.Audio;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class SettingsControl : MonoBehaviour
{
    public Slider masterVolumeSlider;
    public TMP_Dropdown resolutionDrop, qualityDrop;
    public Toggle fullscreenToggle;
    public List<Button> controlButtons = new List<Button>();
    public AudioMixer mixer;
    public Text masterVolumeText;

    private Resolution[] resolutions;
    private int keyIndex;

    private GUIManager guiMgr;

    private void Awake()
    {
        //create all values that need to be saved
        if (!PlayerPrefs.HasKey("masterVolume"))
            PlayerPrefs.SetFloat("masterVolume", 0.8f);
        if (!PlayerPrefs.HasKey("resX"))
            PlayerPrefs.SetInt("resX", Screen.currentResolution.width);
        if (!PlayerPrefs.HasKey("resY"))
            PlayerPrefs.SetInt("resY", Screen.currentResolution.height);
        if (!PlayerPrefs.HasKey("qualitySetting"))
            PlayerPrefs.SetInt("qualitySetting", 5);
        if (!PlayerPrefs.HasKey("fullscreenToggle"))
            PlayerPrefs.SetInt("fullscreenToggle", 1);

        masterVolumeSlider.value = PlayerPrefs.GetFloat("masterVolume");

        resolutions = Screen.resolutions;
        resolutionDrop.ClearOptions();

        List<string> options = new List<string>();

        for (int i = 0; i < resolutions.Length; i++)
        {
            options.Add(resolutions[i].width + " x " + resolutions[i].height);
        }

        resolutionDrop.AddOptions(options);

        string currentResolution = PlayerPrefs.GetInt("resX") + " x " + PlayerPrefs.GetInt("resY");

        for (int i = 0; i < resolutionDrop.options.Count; i++)
        {
            if (resolutionDrop.options[i].text == currentResolution)
            {
                resolutionDrop.value = i;
                break;
            }
        }

        qualityDrop.value = PlayerPrefs.GetInt("qualitySetting");

        if (PlayerPrefs.GetInt("fullscreenToggle") == 1)
            fullscreenToggle.isOn = true;
        else
            fullscreenToggle.isOn = false;

        guiMgr = GameObject.Find("PersistentObject").GetComponent<GUIManager>();
    }

    private void Start()
    {
        if (controlButtons[0] != null)
        {
            for (int i = 0; i < controlButtons.Count; i++)
            {
                controlButtons[i].GetComponentInChildren<Text>().text = InputManager.GetInputKey(i).ToString();
            }
        }
    }

    public Slider GetMasterSlider()
    {
        return masterVolumeSlider;
    }

    public TMP_Dropdown GetDropdown(int dropID)
    {
        switch (dropID)
        {
            default:
                return null;
            case 0:
                return resolutionDrop;
            case 1:
                return qualityDrop;
        }
    }

    public void SetMasterVolume() //called to change the current volume slider values
    {
        float newVolume = Mathf.Log10(masterVolumeSlider.value) * 20;
        mixer.SetFloat("masterVolume", newVolume);
        masterVolumeText.text = (System.Math.Round(masterVolumeSlider.value, 1).ToString());

        PlayerPrefs.SetFloat("masterVolume", masterVolumeSlider.value);
        PlayerPrefs.Save();
    }

    public void SetQuality()
    {
        QualitySettings.SetQualityLevel(qualityDrop.value);
        PlayerPrefs.SetInt("qualitySetting", qualityDrop.value);
    }

    public void SetResolution()
    {
        Screen.SetResolution(resolutions[resolutionDrop.value].width, resolutions[resolutionDrop.value].height, Screen.fullScreen);
        PlayerPrefs.SetInt("resX", resolutions[resolutionDrop.value].width);
        PlayerPrefs.SetInt("resY", resolutions[resolutionDrop.value].height);
    }

    public void SetFullscreenState()
    {
        Screen.fullScreen = fullscreenToggle.isOn;

        if (fullscreenToggle.isOn)
            PlayerPrefs.SetInt("fullscreenToggle", 1);
        else
            PlayerPrefs.SetInt("fullscreenToggle", 0);
    }

    public void SetNewKey(int ID)
    {
        keyIndex = ID;
        StartCoroutine(WaitForKeyDown());
    }

    private IEnumerator WaitForKeyDown()
    {
        KeyCode keyToAssign = KeyCode.None;
        controlButtons[keyIndex].GetComponentInChildren<Text>().text = "Input a key...";

        while (!Input.anyKeyDown)
            yield return null;

        foreach (KeyCode vKey in System.Enum.GetValues(typeof(KeyCode)))
        {
            if (Input.GetKey(vKey))
            {
                keyToAssign = vKey;
                break;
            }
        }

        InputManager.SetInput(keyIndex, keyToAssign);
        controlButtons[keyIndex].GetComponentInChildren<Text>().text = keyToAssign.ToString();

        if (keyIndex <= 4 && SceneManager.GetActiveScene().buildIndex != 0)
        {
            GameObject.Find("Player Object").GetComponent<PlayerMove>().SetNewKey(keyIndex);
        }
        else if (keyIndex == 5 && SceneManager.GetActiveScene().buildIndex != 0)
        {
            foreach (GameObject lightSwitch in GameObject.FindGameObjectsWithTag("Switch"))
            {
                lightSwitch.GetComponent<LightSwitch>().SetNewKey();
            }

            foreach (GameObject terminal in GameObject.FindGameObjectsWithTag("Terminal"))
            {
                if (terminal.GetComponent<Terminal>())
                    terminal.GetComponent<Terminal>().SetNewKey();
                else
                    terminal.GetComponent<LoreTerminal>().SetNewKey();
            }
        }
        else if (keyIndex == 7 && SceneManager.GetActiveScene().buildIndex != 0)
        {
            foreach (GameObject terminal in GameObject.FindGameObjectsWithTag("Terminal"))
            {
                if (terminal.GetComponent<Terminal>())
                    terminal.GetComponent<Terminal>().SetNewKey();
                else
                    terminal.GetComponent<LoreTerminal>().SetNewKey();
            }
        }
        else if (keyIndex == 6 && SceneManager.GetActiveScene().buildIndex != 0)
        {
            if (GameObject.Find("Player Object").GetComponent<BarrierTool>() != null)
                GameObject.Find("Player Object").GetComponent<BarrierTool>().SetNewKey();
        }
        else if (keyIndex == 7)
        {
            GameObject.Find("PersistentObject").GetComponent<GUIManager>().SetNewEscapeMenuKey();
        }
    }

    public void ResetToDefault()
    {
        if (controlButtons[0].transform.parent.parent.gameObject.activeSelf) // the controls menu is open
        {
            InputManager.SetAllKeysToDefault();

            if (SceneManager.GetActiveScene().buildIndex != 0)
            {
                foreach (GameObject lightSwitch in GameObject.FindGameObjectsWithTag("Switch"))
                {
                    lightSwitch.GetComponent<LightSwitch>().SetNewKey();
                }

                foreach (GameObject terminal in GameObject.FindGameObjectsWithTag("Terminal"))
                {
                    if (terminal.GetComponent<Terminal>())
                        terminal.GetComponent<Terminal>().SetNewKey();
                    else
                        terminal.GetComponent<LoreTerminal>().SetNewKey();
                }

                for (int i = 0; i < 5; i++)
                {
                    GameObject.Find("Player Object").GetComponent<PlayerMove>().SetNewKey(i);
                }
                if (GameObject.Find("Player Object").GetComponent<BarrierTool>() != null)
                    GameObject.Find("Player Object").GetComponent<BarrierTool>().SetNewKey();
            }

            GameObject.Find("PersistentObject").GetComponent<GUIManager>().SetNewEscapeMenuKey();

            for (int i = 0; i < controlButtons.Count; i++)
            {
                controlButtons[i].GetComponentInChildren<Text>().text = InputManager.GetInputKey(i).ToString();
            }
        }
        else // the video menu is open
        {
            masterVolumeSlider.value = 0.8f;
            qualityDrop.value = 5;
            fullscreenToggle.isOn = true;
        }
    }

    public void SetEscapeMenuState(bool state)
    {
        guiMgr.ChangeEscapeMenuState(state);
    }
}