Advertisement
Guest User

Settings Script

a guest
Apr 22nd, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.75 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Audio;
  5. using UnityEngine.UI;
  6.  
  7. public class SettingManager : MonoBehaviour
  8. {
  9.     public Toggle fullScreenToggle;
  10.     public Dropdown resolutionDropdown;
  11.     public Dropdown textureQualityDropdown;
  12.     public Dropdown antialiasingDropdown;
  13.     public Dropdown vSyncDropdown;
  14.     public Slider masterVolumeSlider;
  15.     public Slider musicVolumeSlider;
  16.     public Slider sfxVolumeSlider;
  17.     public Button applyButton;
  18.  
  19.     public Resolution[] resolutions;
  20.     float master, music, sfx;
  21.  
  22.     public AudioMixer audioMixer;
  23.  
  24.     void OnEnable()
  25.     {
  26.         LoadSettings();
  27.  
  28.         fullScreenToggle.onValueChanged.AddListener(delegate { OnFullScreenToggle(); });
  29.         resolutionDropdown.onValueChanged.AddListener(delegate { OnResolutionChange(); });
  30.         textureQualityDropdown.onValueChanged.AddListener(delegate { OnTextureQualityChange(); });
  31.         antialiasingDropdown.onValueChanged.AddListener(delegate { OnAntialiasingChange(); });
  32.         vSyncDropdown.onValueChanged.AddListener(delegate { OnVSyncChange(); });
  33.         applyButton.onClick.AddListener(delegate { OnApplyButtonClick(); });
  34.  
  35.         resolutions = Screen.resolutions;
  36.         foreach (Resolution resolution in resolutions)
  37.         {
  38.             resolutionDropdown.options.Add(new Dropdown.OptionData(resolution.ToString()));
  39.         }
  40.     }
  41.  
  42.     public void OnFullScreenToggle()
  43.     {
  44.         Screen.fullScreen = fullScreenToggle.isOn;
  45.     }
  46.  
  47.     public void OnResolutionChange()
  48.     {
  49.         Screen.SetResolution(resolutions[resolutionDropdown.value].width, resolutions[resolutionDropdown.value].height, Screen.fullScreen);
  50.     }
  51.  
  52.     public void OnTextureQualityChange()
  53.     {
  54.         QualitySettings.masterTextureLimit = textureQualityDropdown.value;
  55.     }
  56.  
  57.     public void OnAntialiasingChange()
  58.     {
  59.         QualitySettings.antiAliasing = (int)Mathf.Pow(2f, antialiasingDropdown.value);
  60.     }
  61.  
  62.     public void OnVSyncChange()
  63.     {
  64.         QualitySettings.vSyncCount = vSyncDropdown.value;
  65.     }
  66.  
  67.     public void SetMasterVolume(float masterVol)
  68.     {
  69.         audioMixer.SetFloat("masterVol", masterVol);
  70.     }
  71.  
  72.     public void SetMusicVolume(float musicVol)
  73.     {
  74.         audioMixer.SetFloat("musicVol", musicVol);
  75.     }
  76.  
  77.     public void SetSfxVolume(float sfxVol)
  78.     {
  79.         audioMixer.SetFloat("sfxVol", sfxVol);
  80.     }
  81.  
  82.     public void OnApplyButtonClick()
  83.     {
  84.         SaveSettings();
  85.     }
  86.  
  87.     public void SaveSettings()
  88.     {
  89.         PlayerPrefs.SetInt("fullscreen", Screen.fullScreen ? 1 : 0);
  90.         PlayerPrefs.SetInt("resolutionDropdownValue", resolutionDropdown.value);
  91.         PlayerPrefs.SetInt("width", resolutions[resolutionDropdown.value].width);
  92.         PlayerPrefs.SetInt("height", resolutions[resolutionDropdown.value].height);
  93.         PlayerPrefs.SetInt("textureQuality", QualitySettings.masterTextureLimit);
  94.         PlayerPrefs.SetInt("antialiasingDropdownValue", antialiasingDropdown.value);
  95.         PlayerPrefs.SetInt("antiAliasing", QualitySettings.antiAliasing);
  96.         PlayerPrefs.SetInt("vSync", QualitySettings.vSyncCount);
  97.         audioMixer.GetFloat("masterVol", out master);
  98.         PlayerPrefs.SetFloat("masterVolume", master);
  99.         audioMixer.GetFloat("musicVol", out music);
  100.         PlayerPrefs.SetFloat("musicVolume", music);
  101.         audioMixer.GetFloat("sfxVol", out sfx);
  102.         PlayerPrefs.SetFloat("sfxVolume", sfx);
  103.         PlayerPrefs.Save();
  104.     }
  105.  
  106.     public void LoadSettings()
  107.     {
  108.         Screen.fullScreen = PlayerPrefs.GetInt("fullscreen") == 1 ? true : false;
  109.         //Screen.SetResolution(PlayerPrefs.GetInt("width"), PlayerPrefs.GetInt("height"), Screen.fullScreen);
  110.         QualitySettings.masterTextureLimit = PlayerPrefs.GetInt("textureQuality");
  111.         QualitySettings.antiAliasing = PlayerPrefs.GetInt("antiAliasing");
  112.         QualitySettings.vSyncCount = PlayerPrefs.GetInt("vSync");
  113.         audioMixer.SetFloat("masterVol", PlayerPrefs.GetFloat("masterVolume"));
  114.         audioMixer.SetFloat("musicVol", PlayerPrefs.GetFloat("musicVolume"));
  115.         audioMixer.SetFloat("sfxVol", PlayerPrefs.GetFloat("sfxVolume"));
  116.  
  117.         fullScreenToggle.isOn = Screen.fullScreen;
  118.         //resolutionDropdown.value = PlayerPrefs.GetInt("resolutionDropdownValue");
  119.         textureQualityDropdown.value = QualitySettings.masterTextureLimit;
  120.         antialiasingDropdown.value = PlayerPrefs.GetInt("antialiasingDropdownValue");
  121.         vSyncDropdown.value = QualitySettings.vSyncCount;
  122.         masterVolumeSlider.value = PlayerPrefs.GetFloat("masterVolume");
  123.         musicVolumeSlider.value = PlayerPrefs.GetFloat("masterVolume");
  124.         sfxVolumeSlider.value = PlayerPrefs.GetFloat("sfxVolume");
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement