UnityGameProjectsCode / AbandondedGame / SettingsManager.cs
SettingsManager.cs
Raw
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using UnityEngine.Rendering.PostProcessing;
using UnityEngine.Audio;

public class SettingsManager : MonoBehaviour
{
    public List<Slider> volumeSliders = new List<Slider>();
    public List<TextMeshProUGUI> volumeValues = new List<TextMeshProUGUI>();

    private float defaultMasterVolume { get; set; }
    private float defaultAmbientVolume { get; set; }
    private float defaultEnvVolume { get; set; }
    private float defaultMenuVolume { get; set; }
    private float defaultEffectsVolume { get; set; }

    public TMP_Dropdown resolutionDrop;
    public TMP_Dropdown aspectDrop;
    public TMP_Dropdown windowModeDrop;
    public TMP_Dropdown qualityDrop;
    public Slider fovSlider;
    public Slider brightnessSlider;
    public TextMeshProUGUI fovValueText;
    public TextMeshProUGUI brightnessValueText;
    public Toggle motionBlurToggle;
    public PostProcessProfile profile;
    public AudioMixer mix;

    private int windowModeID;

    private int[] nineResWidths = new int[] { 1920, 1600, 1366, 1280, 1024, 960, 640 };
    private int[] nineResHeights = new int[] { 1080, 900, 768, 720, 576, 540, 360 };
    private int[] tenResWidths = new int[] { 1920, 1680, 1440, 1280 };
    private int[] tenResHeights = new int[] { 1200, 1050, 900, 800 };
    private int[] threeResWidths = new int[] { 1920, 1856, 1600, 1440, 1400, 1280, 1024, 960, 800 };
    private int[] threeResHeights = new int[] { 1400, 1392, 1200, 1080, 1050, 960, 768, 720, 600 };

    private string[] byNineResolutions = new string[] { "1920 x 1080", "1600 x 900", "1366 x 768", "1280 x 720", "1024 x 576", "960 x 540", "640 x 360" };
    private string[] byTenResolutions = new string[] { "1920 x 1200", "1680 x 1050", "1440 x 900", "1280 x 800" };
    private string[] byThreeResolutions = new string[] { "1920 x 1440", "1856 x 1392", "1600 x 1200", "1440 x 1080", "1400 x 1050", "1280 x 960", "1024 x 768", "960 x 720", "800 x 600" };

    private GameObject playerObject;
    private GameObject menuCamera;

    private void Start()
    {
        defaultMasterVolume = 70;
        defaultAmbientVolume = 60;
        defaultEnvVolume = 50;
        defaultMenuVolume = 40;
        defaultEffectsVolume = 30;

        volumeSliders[0].value = defaultMasterVolume;
        volumeSliders[1].value = defaultAmbientVolume;
        volumeSliders[2].value = defaultEnvVolume;
        volumeSliders[3].value = defaultMenuVolume;
        volumeSliders[4].value = defaultEffectsVolume;

        volumeValues[0].text = volumeSliders[0].value.ToString();
        volumeValues[1].text = volumeSliders[1].value.ToString();
        volumeValues[2].text = volumeSliders[2].value.ToString();
        volumeValues[3].text = volumeSliders[3].value.ToString();
        volumeValues[4].text = volumeSliders[4].value.ToString();

        int currentWidth = Screen.width;
        int currentHeight = Screen.height;
        bool resolutionFound = false;

        for (int i = 0; i < nineResWidths.Length; i++)
        {
            if (currentWidth == nineResWidths[i])
            {
                Debug.Log("Starting resolution is: " + currentWidth + " x " + currentHeight);
                resolutionDrop.value = i;
                resolutionFound = true;
                break;
            }
        }

        if (!resolutionFound)
            Debug.LogWarning("Was not able to identify starting resolution.");

        playerObject = ReferenceManager.GetObject(6);
        menuCamera = ReferenceManager.GetObject(5);

        brightnessSlider.value = 0;
    }

    public void SetResolution () //called from dropdown use
    {
        int index = resolutionDrop.value;
        int aspectValue = aspectDrop.value;
        int width; int height;

        bool fullScreen = false;

        if (windowModeID == 0 || windowModeID == 1)
            fullScreen = true;

        switch (aspectValue)
        {
            default:
                width = nineResWidths[index];
                height = nineResHeights[index];
                break;

            case 1:
                width = tenResWidths[index];
                height = tenResHeights[index];
                break;

            case 2:
                width = threeResWidths[index];
                height = threeResHeights[index];
                break;
        }

        Screen.SetResolution(width, height, fullScreen);

        Debug.Log("Set new resolution to: " + width + " x " + height);
    }

    public void SetAspectRatio()
    {
        int index = aspectDrop.value;

        resolutionDrop.options.Clear();

        switch (index)
        {
            default:
                for (int i = 0; i < byNineResolutions.Length; i++)
                {
                    TMP_Dropdown.OptionData item = new TMP_Dropdown.OptionData();

                    resolutionDrop.options.Add(item);
                    item.text = byNineResolutions[i];
                }
                break;

            case 1:
                for (int i = 0; i < byTenResolutions.Length; i++)
                {
                    TMP_Dropdown.OptionData item = new TMP_Dropdown.OptionData();

                    resolutionDrop.options.Add(item);
                    item.text = byTenResolutions[i];
                }
                break;

            case 2:
                for (int i = 0; i < byThreeResolutions.Length; i++)
                {
                    TMP_Dropdown.OptionData item = new TMP_Dropdown.OptionData();

                    resolutionDrop.options.Add(item);
                    item.text = byThreeResolutions[i];
                }
                break;
        }

        resolutionDrop.value = 0;
    }

    public void SetWindowState()
    {
        windowModeID = windowModeDrop.value;

        Debug.Log(windowModeID);

        switch (windowModeID)
        {
            default:
                Screen.fullScreenMode = FullScreenMode.FullScreenWindow;
                break;

            case 1:
                Screen.fullScreenMode = FullScreenMode.ExclusiveFullScreen;
                break;

            case 2:
                Screen.fullScreenMode = FullScreenMode.MaximizedWindow;
                break;

            case 3:
                Screen.fullScreenMode = FullScreenMode.Windowed;
                break;
        }

        Debug.Log("Window state set to: " + Screen.fullScreenMode);
    }

    public void SetNewQuality()
    {
        int index = qualityDrop.value;

        QualitySettings.SetQualityLevel(index);
    }

    public void SetNewFOV()
    {
        float newFOV = fovSlider.value;

        fovValueText.text = newFOV.ToString();
        playerObject.GetComponent<Camera>().fieldOfView = newFOV;
        menuCamera.GetComponent<Camera>().fieldOfView = newFOV;
        playerObject.GetComponent<CameraZoom>().SetNewFOV(newFOV);
    }

    public void SetNewBrightness()
    {
        float newBrightness = brightnessSlider.value;
        brightnessValueText.text = System.Math.Round(newBrightness, 1).ToString();
        profile.GetSetting<ColorGrading>().gamma.value = new Vector4(newBrightness, newBrightness, newBrightness, newBrightness);
    }

    public void SetMotionBlurState()
    {
        profile.GetSetting<MotionBlur>().active = motionBlurToggle.isOn;
    }

    public void SetVolume(int volumeID)
    {
        float newVolume;

        switch (volumeID)
        {
            default:
                newVolume = Mathf.Log10(volumeSliders[0].value) * 20;
                mix.SetFloat("masterVolume", newVolume);
                volumeValues[0].text = (System.Math.Round(volumeSliders[0].value, 1)).ToString();
                break;

            case 1:
                newVolume = Mathf.Log10(volumeSliders[1].value) * 20;
                mix.SetFloat("ambientVolume", newVolume);
                volumeValues[1].text = (System.Math.Round(volumeSliders[1].value, 1)).ToString();
                break;

            case 2:
                newVolume = Mathf.Log10(volumeSliders[2].value) * 20;
                mix.SetFloat("envVolume", newVolume);
                volumeValues[2].text = (System.Math.Round(volumeSliders[2].value, 1)).ToString();
                break;

            case 3:
                newVolume = Mathf.Log10(volumeSliders[3].value) * 20;
                mix.SetFloat("menuVolume", newVolume);
                volumeValues[3].text = (System.Math.Round(volumeSliders[3].value, 1)).ToString();
                break;

            case 4:
                newVolume = Mathf.Log10(volumeSliders[4].value) * 20;
                mix.SetFloat("effectsVolume", newVolume);
                volumeValues[4].text = (System.Math.Round(volumeSliders[4].value, 1)).ToString();
                break;
        }
    }

    public void ResetToDefaults()
    {
        for (int i = 0; i < volumeSliders.Count; i++)
        {
            volumeSliders[i].value = 1;
        }

        qualityDrop.value = 5;
        fovSlider.value = 70;
        brightnessSlider.value = 0;
        motionBlurToggle.isOn = true;
    }
}