Advertisement
Guest User

update graphics options

a guest
Oct 11th, 2023
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.86 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. namespace AC
  7. {
  8.  
  9. public class GraphicOptions : MonoBehaviour
  10. {
  11.  
  12. #region Variables
  13.  
  14. [SerializeField] private string globalStringVariable = "GraphicOptionsData";
  15. [SerializeField] private Dropdown resolutionDropdown = null;
  16. [SerializeField] private Toggle fullScreenToggle = null;
  17. [SerializeField] private Dropdown qualityPresetDropdown = null;
  18. [SerializeField] private Dropdown antiAliasingDropdown = null;
  19. [SerializeField] private Dropdown textureQualityDropdown = null;
  20. [SerializeField] private Dropdown vSyncDropdown = null;
  21. [SerializeField]
  22. private List<float> supportedAspectRatios = new List<float> {
  23. 16/9f,
  24. 16/10f,
  25. 21/9f
  26. };
  27. [SerializeField] private List<Resolution> supportedResolutions = new List<Resolution>();
  28. private int nonCustomQualityLevel;
  29.  
  30. #endregion
  31.  
  32.  
  33. #region UnityStandards
  34.  
  35. private void Awake()
  36. {
  37. //filter for supported aspect ratios
  38. for (int i = 0; i < Screen.resolutions.Length; i++)
  39. {
  40. if (supportedAspectRatios.Contains((float)Screen.resolutions[i].width / (float)Screen.resolutions[i].height))
  41. {
  42. supportedResolutions.Add(Screen.resolutions[i]);
  43. }
  44. }
  45. }
  46.  
  47. private void OnEnable()
  48. {
  49. UpdateUIValues();
  50. }
  51.  
  52. #endregion
  53.  
  54.  
  55. #region PublicFunctions
  56.  
  57. public void SaveAndApply()
  58. {
  59. GVar gVar = GlobalVariables.GetVariable(globalStringVariable);
  60. if (gVar != null && gVar.type == VariableType.String)
  61. {
  62. bool usingAdvancedOptions = qualityPresetDropdown.value == qualityPresetDropdown.options.Count - 1;
  63. GraphicOptionsData graphicOptionsData = new GraphicOptionsData(resolutionDropdown.value, fullScreenToggle.isOn, usingAdvancedOptions ? nonCustomQualityLevel : qualityPresetDropdown.value, usingAdvancedOptions, antiAliasingDropdown.value, textureQualityDropdown.value, vSyncDropdown.value);
  64. gVar.TextValue = JsonUtility.ToJson(graphicOptionsData);
  65. Options.SavePrefs();
  66. }
  67. else
  68. {
  69. ACDebug.LogWarning("Could not apply Graphic Options data because no Global String variable was found", this);
  70. }
  71.  
  72. Apply();
  73. }
  74.  
  75.  
  76. public void Apply()
  77. {
  78. GraphicOptionsData graphicOptionsData = GetSaveData();
  79. if (graphicOptionsData != null)
  80. {
  81. graphicOptionsData.Apply(supportedResolutions);
  82. }
  83. }
  84.  
  85.  
  86. public void OnSetAdvancedOption()
  87. {
  88. SetDropdownValue(qualityPresetDropdown, qualityPresetDropdown.options.Count - 1);
  89. }
  90.  
  91.  
  92. public void OnSetQualityPreset()
  93. {
  94. if (qualityPresetDropdown.value <= qualityPresetDropdown.options.Count - 1)
  95. {
  96. QualitySettings.SetQualityLevel(qualityPresetDropdown.value, false);
  97. UpdateAdvancedUIValues();
  98. QualitySettings.SetQualityLevel(nonCustomQualityLevel, false);
  99.  
  100. nonCustomQualityLevel = qualityPresetDropdown.value;
  101. }
  102. }
  103.  
  104.  
  105. public static int QualityIndexToLevel(int index)
  106. {
  107. return (int)Mathf.Pow(2, index);
  108. }
  109.  
  110.  
  111. public static int QualityLevelToIndex(int level)
  112. {
  113. return (int) Math.Log(level, 2);
  114. }
  115.  
  116. #endregion
  117.  
  118.  
  119. #region PrivateFunctions
  120.  
  121. private void UpdateUIValues()
  122. {
  123. // Advanced options
  124. GraphicOptionsData graphicOptionsData = GetSaveData();
  125. bool usingAdvancedOptions = (graphicOptionsData != null) ? graphicOptionsData.UsingAdvancedOptions : false;
  126.  
  127. // Resolution
  128. if (resolutionDropdown)
  129. {
  130. resolutionDropdown.options.Clear();
  131. int resolutionIndex = -1;
  132. for (int i = 0; i < supportedResolutions.Count; i++)
  133. {
  134. if (supportedResolutions[i].width == Screen.width &&
  135. supportedResolutions[i].height == Screen.height &&
  136. supportedResolutions[i].refreshRate == Screen.currentResolution.refreshRate)
  137. {
  138. resolutionIndex = i;
  139. }
  140.  
  141. string label = supportedResolutions[i].width.ToString() + " x " + supportedResolutions[i].height.ToString() + " " + supportedResolutions[i].refreshRate.ToString() + " hz";
  142. resolutionDropdown.options.Add(new Dropdown.OptionData(label));
  143. }
  144. resolutionDropdown.RefreshShownValue();
  145. if (resolutionIndex >= 0) SetDropdownValue(resolutionDropdown, resolutionIndex);
  146. }
  147.  
  148. // Full-screen
  149. if (fullScreenToggle)
  150. {
  151. fullScreenToggle.isOn = Screen.fullScreen;
  152. }
  153.  
  154. // Quality preset
  155. if (qualityPresetDropdown)
  156. {
  157. qualityPresetDropdown.options.Clear();
  158. foreach (string qualityName in QualitySettings.names)
  159. {
  160. qualityPresetDropdown.options.Add(new Dropdown.OptionData(qualityName));
  161. }
  162. qualityPresetDropdown.options.Add(new Dropdown.OptionData("Custom"));
  163. qualityPresetDropdown.RefreshShownValue();
  164. if (usingAdvancedOptions)
  165. {
  166. SetDropdownValue(qualityPresetDropdown, qualityPresetDropdown.options.Count - 1);
  167. }
  168. else
  169. {
  170. SetDropdownValue(qualityPresetDropdown, QualitySettings.GetQualityLevel());
  171. }
  172. nonCustomQualityLevel = QualitySettings.GetQualityLevel();
  173. }
  174.  
  175. UpdateAdvancedUIValues();
  176. }
  177.  
  178.  
  179. private void UpdateAdvancedUIValues()
  180. {
  181. // Anti-aliasing
  182. int antiAliasingValue = QualityLevelToIndex(QualitySettings.antiAliasing);
  183. SetDropdownValue(antiAliasingDropdown, antiAliasingValue);
  184.  
  185. // Texture quality
  186. SetDropdownValue(textureQualityDropdown, QualitySettings.masterTextureLimit);
  187.  
  188. // Vsync
  189. SetDropdownValue(vSyncDropdown, QualitySettings.vSyncCount);
  190. }
  191.  
  192.  
  193. private GraphicOptionsData GetSaveData()
  194. {
  195. GVar gVar = GlobalVariables.GetVariable(globalStringVariable);
  196. if (gVar != null && gVar.type == VariableType.String)
  197. {
  198. string optionsDataString = gVar.TextValue;
  199. if (!string.IsNullOrEmpty(optionsDataString))
  200. {
  201. GraphicOptionsData graphicOptionsData = JsonUtility.FromJson<GraphicOptionsData>(optionsDataString);
  202. return graphicOptionsData;
  203. }
  204. return null;
  205. }
  206. else
  207. {
  208. ACDebug.LogWarning("Could not apply Graphic Options data because no Global String variable was found", this);
  209. return null;
  210. }
  211. }
  212.  
  213.  
  214. private void SetDropdownValue(Dropdown dropdown, int value)
  215. {
  216. if (dropdown)
  217. {
  218. dropdown.SetValueWithoutNotify(value);
  219. }
  220. }
  221.  
  222. #endregion
  223.  
  224. }
  225.  
  226.  
  227. [Serializable]
  228. public class GraphicOptionsData
  229. {
  230.  
  231. #region Variables
  232.  
  233. [SerializeField] private int screenResolutionIndex;
  234. [SerializeField] private bool isFullScreen;
  235. [SerializeField] private int qualityPresetIndex;
  236. [SerializeField] private bool usingAdvancedOptions;
  237. [SerializeField] private int antiAliasingLevel;
  238. [SerializeField] private int textureQualityLevel;
  239. [SerializeField] private int vSyncCount;
  240.  
  241. #endregion
  242.  
  243.  
  244. #region Constructors
  245.  
  246. public GraphicOptionsData(int _screenResolutionIndex, bool _isFullScreen, int _qualityPresetIndex, bool _usingAdvancedOptions, int _antiAliasingLevel, int _textureQualityLevel, int _vSyncCount)
  247. {
  248. screenResolutionIndex = _screenResolutionIndex;
  249. isFullScreen = _isFullScreen;
  250. qualityPresetIndex = _qualityPresetIndex;
  251. usingAdvancedOptions = _usingAdvancedOptions;
  252. antiAliasingLevel = _antiAliasingLevel;
  253. textureQualityLevel = _textureQualityLevel;
  254. vSyncCount = _vSyncCount;
  255. }
  256.  
  257. #endregion
  258.  
  259.  
  260. #region PublicFunctions
  261.  
  262. public void Apply(List<Resolution> supportedResolutions)
  263. {
  264. Resolution chosenResolution = supportedResolutions[screenResolutionIndex];
  265. Screen.SetResolution(chosenResolution.width, chosenResolution.height, isFullScreen);
  266. QualitySettings.SetQualityLevel(qualityPresetIndex, true);
  267. if (usingAdvancedOptions)
  268. {
  269. QualitySettings.antiAliasing = GraphicOptions.QualityIndexToLevel(antiAliasingLevel);
  270. QualitySettings.masterTextureLimit = textureQualityLevel;
  271. QualitySettings.vSyncCount = vSyncCount;
  272. }
  273.  
  274. KickStarter.playerMenus.RecalculateAll();
  275. }
  276.  
  277. #endregion
  278.  
  279.  
  280. #region GetSet
  281.  
  282. public bool UsingAdvancedOptions { get { return usingAdvancedOptions; } }
  283.  
  284. #endregion
  285.  
  286. }
  287.  
  288. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement