123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Sirenix.OdinInspector;
- using UnityEngine.UI;
- namespace KairoEngine.Core.ConfigOptions
- {
- [CreateAssetMenu(fileName = "ScreenResolutionConfigOption", menuName = "KairoEngine/ConfigOptions/Screen Resolution", order = 1), HideMonoScript]
- public class ScreenResolutionConfigOption : ConfigOptionBase
- {
- public int minWidth = 720;
- public int minHeigth = 480;
- //[PropertyOrder(10)] public int defaultResolutionIndex;
- [PropertyOrder(11), ListDrawerSettings(ShowPaging = false), PropertySpace(4,4)]
- public List<ResolutionData> resolutionList;
- [PropertyOrder(12), TextArea(), HideLabel, PropertySpace(0, 4)] public string description;
- private int currentResolutionIndex;
- private bool showDebug = false;
- public override ConfigOptionUiData GetUiData()
- {
- var options = new List<string>();
- for (int i = 0; i < resolutionList.Count; i++)
- {
- options.Add(resolutionList[i].title);
- }
- var dropdownData = new ConfigOptionUiData.DropdownData(currentResolutionIndex, options);
- return new ConfigOptionUiData(title, description, dropdownData);
- }
- public override void LoadValue(bool debug)
- {
- showDebug = debug;
- SetResolutions();
- if(resolutionList.Count == 0) return;
- currentResolutionIndex = PlayerPrefs.GetInt(id, resolutionList.Count - 1);
- ApplyConfig();
- //if(debug) Debug.Log($"{title} changed to {resolutionList[currentResolutionIndex].title}.");
- }
- public override void SetDefaultValue()
- {
- SetResolutions();
- if(resolutionList.Count == 0) return;
- currentResolutionIndex = PlayerPrefs.GetInt(id, resolutionList.Count - 1);
- PlayerPrefs.SetInt(id, currentResolutionIndex);
- ApplyConfig();
- }
- public override void SetValue(ConfigOptionData data, bool save = false)
- {
- PlayerPrefs.SetInt(id, data.intValue);
- currentResolutionIndex = data.intValue;
- ApplyConfig();
- if(save) PlayerPrefs.SetFloat(id, currentResolutionIndex);
- }
- private void ApplyConfig()
- {
- Resolution res = resolutionList[currentResolutionIndex].ToResolution();
- if(Screen.currentResolution.ToString() != res.ToString())
- {
- Screen.SetResolution(res.width, res.height, Screen.fullScreen, res.refreshRate);
- if(showDebug) Debug.Log($"Changing resolution to {res.ToString()}");
- }
- else if(showDebug) Debug.Log($"Current resolution is already set to {res.ToString()}");
- }
- [Button("Load Resolution List"), PropertyOrder(14), ButtonGroup("Actions")]
- private void SetResolutions()
- {
- resolutionList.Clear();
- var resolutions = FilteredResolutions();
- for (int i = 0; i < resolutions.Length; i++)
- {
- var res = resolutions[i];
- resolutionList.Add(new ResolutionData(res.ToString(), res.width, res.height, res.refreshRate));
- }
- if(resolutionList.Count == 0)
- {
- if(showDebug) Debug.LogError("No resolutions found for this screen.");
- }
- }
- private Resolution[] FilteredResolutions()
- {
- Resolution[] resolutions = Screen.resolutions;
- List<Resolution> filtered = new List<Resolution>();
- for (int i = 0; i < resolutions.Length; i++)
- {
- if(resolutions[i].width < minWidth) continue;
- if(resolutions[i].height < minHeigth) continue;
- if(resolutions[i].refreshRate == 59) continue;
- if(resolutions[i].refreshRate == 50) continue;
- if(resolutions[i].refreshRate == 30) continue;
- if(resolutions[i].refreshRate == 29) continue;
- if(resolutions[i].refreshRate == 25) continue;
- if(resolutions[i].refreshRate == 24) continue;
- if(resolutions[i].refreshRate == 23) continue;
- filtered.Add(resolutions[i]);
- }
- return filtered.ToArray();
- }
- [System.Serializable]
- public class ResolutionData
- {
- [ReadOnly, HideLabel] public string title;
- [HideInInspector] public int width;
- [HideInInspector] public int height;
- [HideInInspector] public int refreshRate;
- public ResolutionData(string title, int width, int height, int refreshRate)
- {
- this.title = title;
- this.width = width;
- this.height = height;
- this.refreshRate = refreshRate;
- }
- public Resolution ToResolution()
- {
- var res = new Resolution();
- res.width = this.width;
- res.height = this.height;
- res.refreshRate = this.refreshRate;
- return res;
- }
- }
- }
- }
|