silraks

Reddit help

Jun 16th, 2017
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. using System.Collections;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. using System.IO;
  5.  
  6. public class SettingManager : MonoBehaviour{
  7. public Toggle fullscreenToggle;
  8. public Dropdown resolutionDropdown;
  9. public Dropdown textureQualityDropdown;
  10. public Dropdown antialiasingDropdown;
  11. public Dropdown vSyncDropdown;
  12. public Slider musicVolumeSlider;
  13. public Button applyButton;
  14.  
  15. public AudioSource musicSource;
  16. public Resolution[] resolutions;
  17. public GameSettings gameSettings;
  18.  
  19.  
  20. void OnEnable()
  21. {
  22. gameSettings = new GameSettings();
  23.  
  24. fullscreenToggle.onValueChanged.AddListener(delegate { OnFullscreenToggle(); });
  25. resolutionDropdown.onValueChanged.AddListener(delegate { OnResolutionChange(); });
  26. textureQualityDropdown.onValueChanged.AddListener(delegate { OnTextureQualityChange(); });
  27. antialiasingDropdown.onValueChanged.AddListener(delegate { OnAntialiasingChange(); });
  28. vSyncDropdown.onValueChanged.AddListener(delegate { OnVSyncChange(); });
  29. musicVolumeSlider.onValueChanged.AddListener(delegate { OnMusicVolumeChange(); });
  30. applyButton.onClick.AddListener(delegate { OnApplyButtonClick(); });
  31.  
  32.  
  33. resolutions = Screen.resolutions;
  34. foreach (Resolution resolution in resolutions)
  35. {
  36. resolutionDropdown.options.Add(new Dropdown.OptionData(resolution.ToString()));
  37. }
  38.  
  39. if (File.Exists(Application.persistentDataPath + "/gamesettings.json") == true)
  40. {
  41. LoadSettings();
  42. }
  43.  
  44. }
  45.  
  46. public void OnFullscreenToggle()
  47. {
  48. gameSettings.fullscreen = Screen.fullScreen = fullscreenToggle.isOn;
  49. }
  50.  
  51. public void OnResolutionChange()
  52. {
  53. Screen.SetResolution(resolutions[resolutionDropdown.value].width, resolutions[resolutionDropdown.value].height, Screen.fullScreen);
  54. }
  55.  
  56. public void OnTextureQualityChange()
  57. {
  58. QualitySettings.masterTextureLimit = gameSettings.textureQuality = textureQualityDropdown.value;
  59. }
  60.  
  61. public void OnAntialiasingChange()
  62. {
  63. QualitySettings.antiAliasing = gameSettings.antialiasing = (int)Mathf.Pow(2, antialiasingDropdown.value);
  64. }
  65.  
  66. public void OnVSyncChange()
  67. {
  68. QualitySettings.vSyncCount = gameSettings.vSync = vSyncDropdown.value;
  69. }
  70.  
  71. public void OnMusicVolumeChange()
  72. {
  73. musicSource.volume = gameSettings.musicVolume = musicVolumeSlider.value;
  74. }
  75.  
  76.  
  77.  
  78.  
  79.  
  80. public void OnApplyButtonClick()
  81. {
  82. SaveSettings();
  83. }
  84.  
  85.  
  86.  
  87.  
  88.  
  89. public void SaveSettings()
  90. {
  91. string jsonData = JsonUtility.ToJson(gameSettings, true);
  92. File.WriteAllText(Application.persistentDataPath + "gamesettings.json", jsonData);
  93. }
  94.  
  95. public void LoadSettings()
  96. {
  97. File.ReadAllText(Application.persistentDataPath + "/gamesettings.json");
  98. gameSettings = JsonUtility.FromJson<GameSettings>(File.ReadAllText(Application.persistentDataPath + "/gamesettings.json"));
  99. musicVolumeSlider.value = gameSettings.musicVolume;
  100. antialiasingDropdown.value = gameSettings.antialiasing;
  101. vSyncDropdown.value = gameSettings.vSync;
  102. textureQualityDropdown.value = gameSettings.resolutionIndex;
  103. fullscreenToggle.isOn = gameSettings.fullscreen;
  104. }
  105.  
  106.  
  107. }
Add Comment
Please, Sign In to add comment