Advertisement
Pieridae

MenuController Script (Unity)

Mar 15th, 2022 (edited)
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.78 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.SceneManagement;
  6. using TMPro;
  7.  
  8. public class MenuController : MonoBehaviour
  9. {
  10. [Header("Volume Setting")]
  11. [SerializeField] private Slider volumeSlider = null;
  12. [SerializeField] private float defaultVolume = 1.0f;
  13.  
  14. [Header("Gameplay Settings")]
  15. [SerializeField] private Slider controllerSenSlider = null;
  16. [SerializeField] private int defaultSen = 4;
  17. public int mainControllerSen = 4;
  18.  
  19. [Header("Toggle Settings")]
  20. [SerializeField] private Toggle invertYToggle = null;
  21.  
  22. [Header("Graphics Settings")]
  23. [SerializeField] private Slider brightnessSlider = null;
  24. [SerializeField] private float defaultBrightness = 1;
  25.  
  26. private int _qualityLevel;
  27. private bool _isFullScreen;
  28. private float _brightnessLevel;
  29.  
  30. [Space(10)]
  31. public TMP_Dropdown qualityDropdown;
  32. [SerializeField] private Toggle fullscreenToggle;
  33.  
  34. [Header("Confirmation")]
  35. [SerializeField] private GameObject confirmationPrompt = null;
  36.  
  37. [Header("Levels to Load")]
  38. public string _newGameLevel;
  39. private string levelToLoad;
  40. [SerializeField] private GameObject noSavedGameDialog = null;
  41.  
  42. [Header("Resolution Dropdowns")]
  43. public TMP_Dropdown resolutionDropdown;
  44. private Resolution[] resolutions;
  45.  
  46. private void Start()
  47. {
  48. resolutions = Screen.resolutions;
  49. resolutionDropdown.ClearOptions();
  50.  
  51. List<string> options = new List<string>();
  52.  
  53. int currentResolutionIndex = 0;
  54.  
  55. for (int i = 0; i < resolutions.Length; i++)
  56. {
  57. string option = resolutions[i].width + " x " + resolutions[i].height;
  58. options.Add(option);
  59.  
  60. if(resolutions[i].width == Screen.width && resolutions[i].height == Screen.height)
  61. {
  62. currentResolutionIndex = i;
  63. }
  64. }
  65. resolutionDropdown.AddOptions(options);
  66. resolutionDropdown.value = currentResolutionIndex;
  67. resolutionDropdown.RefreshShownValue();
  68. }
  69.  
  70. public void SetResolution(int resolutionIndex)
  71. {
  72. Resolution resolution = resolutions[resolutionIndex];
  73. Screen.SetResolution(resolution.width, resolution.height, Screen.fullScreen);
  74. }
  75.  
  76. public void NewGameDialogYes()
  77. {
  78. SceneManager.LoadScene(_newGameLevel);
  79. }
  80.  
  81. public void LoadGameDialogYes()
  82. {
  83. if (PlayerPrefs.HasKey("SavedLevel"))
  84. {
  85. levelToLoad = PlayerPrefs.GetString("SavedLevel");
  86. SceneManager.LoadScene(levelToLoad);
  87. }
  88. else
  89. {
  90. noSavedGameDialog.SetActive(true);
  91. }
  92. }
  93.  
  94. public void ExitButton()
  95. {
  96. Application.Quit();
  97. }
  98.  
  99. public void SetVolume(float volume)
  100. {
  101. AudioListener.volume = volume;
  102. }
  103.  
  104. public void VolumeApply()
  105. {
  106. PlayerPrefs.SetFloat("masterVolume", AudioListener.volume);
  107. StartCoroutine(ConfirmationBox());
  108. }
  109.  
  110. public void SetControllerSen(float sensitivity)
  111. {
  112. mainControllerSen = Mathf.RoundToInt(sensitivity);
  113. }
  114.  
  115. public void GameplayApply()
  116. {
  117. if (invertYToggle.isOn)
  118. {
  119. PlayerPrefs.SetInt("masterInvertY", 1);
  120. }
  121. else
  122. {
  123. PlayerPrefs.SetInt("masterInvertY", 0);
  124. }
  125.  
  126. PlayerPrefs.SetFloat("masterSen", mainControllerSen);
  127. StartCoroutine(ConfirmationBox());
  128. }
  129.  
  130. public void SetBrightness(float brightness)
  131. {
  132. _brightnessLevel = brightness;
  133. }
  134.  
  135. public void setFullScreen(bool isFullscreen)
  136. {
  137. _isFullScreen = isFullscreen;
  138. }
  139.  
  140. public void SetQuality(int qualityIndex)
  141. {
  142. _qualityLevel = qualityIndex;
  143. }
  144.  
  145. public void GraphicsApply()
  146. {
  147. PlayerPrefs.SetFloat("masterBrightness", _brightnessLevel);
  148.  
  149. PlayerPrefs.SetInt("masterQuality", _qualityLevel);
  150. QualitySettings.SetQualityLevel(_qualityLevel);
  151.  
  152. PlayerPrefs.SetInt("masterFullscreen", (_isFullScreen ? 1 : 0));
  153. Screen.fullScreen = _isFullScreen;
  154.  
  155. StartCoroutine(ConfirmationBox());
  156. }
  157.  
  158. public void ResetButton()
  159. {
  160.  
  161. brightnessSlider.value = defaultBrightness;
  162. Debug.Log("Current Brightness: " + defaultBrightness.ToString());
  163.  
  164. qualityDropdown.value = 2;
  165. QualitySettings.SetQualityLevel(1);
  166. Debug.Log("Current Quality: " + qualityDropdown.value.ToString());
  167.  
  168. fullscreenToggle.isOn = false;
  169. Screen.fullScreen = false;
  170. Debug.Log("Fullscreen or Windowed?: " + fullscreenToggle.ToString());
  171.  
  172. Resolution currentResolution = Screen.currentResolution;
  173. Screen.SetResolution(currentResolution.width, currentResolution.height, Screen.fullScreen);
  174. resolutionDropdown.value = resolutions.Length;
  175. GraphicsApply();
  176. Debug.Log("Current Resolution: " + currentResolution.ToString());
  177.  
  178. AudioListener.volume = defaultVolume;
  179. volumeSlider.value = defaultVolume;
  180. VolumeApply();
  181. Debug.Log("Current Volume: " + volumeSlider.value.ToString());
  182.  
  183. controllerSenSlider.value = defaultSen;
  184. mainControllerSen = defaultSen;
  185. invertYToggle.isOn = false;
  186. GameplayApply();
  187. Debug.Log("Current Sensitivity: " + defaultSen.ToString());
  188. Debug.Log("Inverted Y-Axis?: " + invertYToggle.ToString());
  189. }
  190.  
  191. public IEnumerator ConfirmationBox()
  192. {
  193. confirmationPrompt.SetActive(true);
  194. yield return new WaitForSeconds(2);
  195. confirmationPrompt.SetActive(false);
  196. }
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement