ScreenResolutionConfigOption.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. public int minWidth = 720;
  12. public int minHeigth = 480;
  13. //[PropertyOrder(10)] public int defaultResolutionIndex;
  14. [PropertyOrder(11), ListDrawerSettings(ShowPaging = false), PropertySpace(4,4)]
  15. public List<ResolutionData> resolutionList;
  16. [PropertyOrder(12), TextArea(), HideLabel, PropertySpace(0, 4)] public string description;
  17. private int currentResolutionIndex;
  18. private bool showDebug = false;
  19. public override ConfigOptionUiData GetUiData()
  20. {
  21. var options = new List<string>();
  22. for (int i = 0; i < resolutionList.Count; i++)
  23. {
  24. options.Add(resolutionList[i].title);
  25. }
  26. var dropdownData = new ConfigOptionUiData.DropdownData(currentResolutionIndex, options);
  27. return new ConfigOptionUiData(title, description, dropdownData);
  28. }
  29. public override void LoadValue(bool debug)
  30. {
  31. showDebug = debug;
  32. SetResolutions();
  33. if(resolutionList.Count == 0) return;
  34. currentResolutionIndex = PlayerPrefs.GetInt(id, resolutionList.Count - 1);
  35. ApplyConfig();
  36. //if(debug) Debug.Log($"{title} changed to {resolutionList[currentResolutionIndex].title}.");
  37. }
  38. public override void SetDefaultValue()
  39. {
  40. SetResolutions();
  41. if(resolutionList.Count == 0) return;
  42. currentResolutionIndex = PlayerPrefs.GetInt(id, resolutionList.Count - 1);
  43. PlayerPrefs.SetInt(id, currentResolutionIndex);
  44. ApplyConfig();
  45. }
  46. public override void SetValue(ConfigOptionData data, bool save = false)
  47. {
  48. PlayerPrefs.SetInt(id, data.intValue);
  49. currentResolutionIndex = data.intValue;
  50. ApplyConfig();
  51. if(save) PlayerPrefs.SetFloat(id, currentResolutionIndex);
  52. }
  53. private void ApplyConfig()
  54. {
  55. Resolution res = resolutionList[currentResolutionIndex].ToResolution();
  56. if(Screen.currentResolution.ToString() != res.ToString())
  57. {
  58. Screen.SetResolution(res.width, res.height, Screen.fullScreen, res.refreshRate);
  59. if(showDebug) Debug.Log($"Changing resolution to {res.ToString()}");
  60. }
  61. else if(showDebug) Debug.Log($"Current resolution is already set to {res.ToString()}");
  62. }
  63. [Button("Load 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. if(resolutionList.Count == 0)
  74. {
  75. if(showDebug) Debug.LogError("No resolutions found for this screen.");
  76. }
  77. }
  78. private Resolution[] FilteredResolutions()
  79. {
  80. Resolution[] resolutions = Screen.resolutions;
  81. List<Resolution> filtered = new List<Resolution>();
  82. for (int i = 0; i < resolutions.Length; i++)
  83. {
  84. if(resolutions[i].width < minWidth) continue;
  85. if(resolutions[i].height < minHeigth) continue;
  86. if(resolutions[i].refreshRate == 59) continue;
  87. if(resolutions[i].refreshRate == 50) continue;
  88. if(resolutions[i].refreshRate == 30) continue;
  89. if(resolutions[i].refreshRate == 29) continue;
  90. if(resolutions[i].refreshRate == 25) continue;
  91. if(resolutions[i].refreshRate == 24) continue;
  92. if(resolutions[i].refreshRate == 23) continue;
  93. filtered.Add(resolutions[i]);
  94. }
  95. return filtered.ToArray();
  96. }
  97. [System.Serializable]
  98. public class ResolutionData
  99. {
  100. [ReadOnly, HideLabel] public string title;
  101. [HideInInspector] public int width;
  102. [HideInInspector] public int height;
  103. [HideInInspector] public int refreshRate;
  104. public ResolutionData(string title, int width, int height, int refreshRate)
  105. {
  106. this.title = title;
  107. this.width = width;
  108. this.height = height;
  109. this.refreshRate = refreshRate;
  110. }
  111. public Resolution ToResolution()
  112. {
  113. var res = new Resolution();
  114. res.width = this.width;
  115. res.height = this.height;
  116. res.refreshRate = this.refreshRate;
  117. return res;
  118. }
  119. }
  120. }
  121. }