ScreenResolutionConfigOption.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Sirenix.OdinInspector;
  5. using UnityEngine.UI;
  6. namespace KairoEngine.Core.ConfigOptions
  7. {
  8. [CreateAssetMenu(fileName = "ScreenResolutionConfigOption", menuName = "KairoEngine/ConfigOptions/Screen Resolution", order = 1), HideMonoScript]
  9. public class ScreenResolutionConfigOption : ConfigOptionBase
  10. {
  11. [PropertyOrder(10)] public int defaultResolutionIndex;
  12. [PropertyOrder(11), ListDrawerSettings(ShowPaging = false), PropertySpace(4,4)]
  13. public List<ResolutionData> resolutionList;
  14. [PropertyOrder(12), TextArea(), HideLabel, PropertySpace(0, 4)] public string description;
  15. private int currentResolutionIndex;
  16. private bool showDebug = false;
  17. public override ConfigOptionUiData GetUiData()
  18. {
  19. var options = new List<string>();
  20. for (int i = 0; i < resolutionList.Count; i++)
  21. {
  22. options.Add(resolutionList[i].title);
  23. }
  24. var dropdownData = new ConfigOptionUiData.DropdownData(currentResolutionIndex, options);
  25. return new ConfigOptionUiData(title, description, dropdownData);
  26. }
  27. public override void LoadValue(bool debug)
  28. {
  29. currentResolutionIndex = PlayerPrefs.GetInt(id, defaultResolutionIndex);
  30. showDebug = debug;
  31. ApplyConfig();
  32. //if(debug) Debug.Log($"{title} changed to {resolutionList[currentResolutionIndex].title}.");
  33. }
  34. public override void SetDefaultValue()
  35. {
  36. currentResolutionIndex = defaultResolutionIndex;
  37. PlayerPrefs.SetInt(id, currentResolutionIndex);
  38. ApplyConfig();
  39. }
  40. public override void SetValue(ConfigOptionData data, bool save = false)
  41. {
  42. PlayerPrefs.SetInt(id, data.intValue);
  43. currentResolutionIndex = data.intValue;
  44. ApplyConfig();
  45. if(save) PlayerPrefs.SetFloat(id, currentResolutionIndex);
  46. }
  47. private void ApplyConfig()
  48. {
  49. Resolution res = resolutionList[currentResolutionIndex].ToResolution();
  50. if(Screen.currentResolution.ToString() != res.ToString())
  51. {
  52. Screen.SetResolution(res.width, res.height, Screen.fullScreen, res.refreshRate);
  53. if(showDebug) Debug.Log($"Changing resolution to {res.ToString()}");
  54. }
  55. else if(showDebug) Debug.Log($"Current resolution is already set to {res.ToString()}");
  56. }
  57. [Button("Apply Default"), PropertyOrder(13), ButtonGroup("Actions")]
  58. private void ApplyDefaultConfig()
  59. {
  60. currentResolutionIndex = defaultResolutionIndex;
  61. ApplyConfig();
  62. }
  63. [Button("Reset Resolution List"), PropertyOrder(14), ButtonGroup("Actions")]
  64. private void SetResolutions()
  65. {
  66. resolutionList.Clear();
  67. var resolutions = FilteredResolutions();
  68. for (int i = 0; i < resolutions.Length; i++)
  69. {
  70. var res = resolutions[i];
  71. resolutionList.Add(new ResolutionData(res.ToString(), res.width, res.height, res.refreshRate));
  72. }
  73. }
  74. private Resolution[] FilteredResolutions()
  75. {
  76. Resolution[] resolutions = Screen.resolutions;
  77. List<Resolution> filtered = new List<Resolution>();
  78. for (int i = 0; i < resolutions.Length; i++)
  79. {
  80. if(resolutions[i].refreshRate == 59) continue;
  81. if(resolutions[i].refreshRate == 50) continue;
  82. if(resolutions[i].refreshRate == 30) continue;
  83. if(resolutions[i].refreshRate == 29) continue;
  84. if(resolutions[i].refreshRate == 25) continue;
  85. if(resolutions[i].refreshRate == 24) continue;
  86. if(resolutions[i].refreshRate == 23) continue;
  87. filtered.Add(resolutions[i]);
  88. }
  89. return filtered.ToArray();
  90. }
  91. [System.Serializable]
  92. public class ResolutionData
  93. {
  94. [ReadOnly, HideLabel] public string title;
  95. [HideInInspector] public int width;
  96. [HideInInspector] public int height;
  97. [HideInInspector] public int refreshRate;
  98. public ResolutionData(string title, int width, int height, int refreshRate)
  99. {
  100. this.title = title;
  101. this.width = width;
  102. this.height = height;
  103. this.refreshRate = refreshRate;
  104. }
  105. public Resolution ToResolution()
  106. {
  107. var res = new Resolution();
  108. res.width = this.width;
  109. res.height = this.height;
  110. res.refreshRate = this.refreshRate;
  111. return res;
  112. }
  113. }
  114. }
  115. }