Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static readonly List<Res> AvailableResolutions = new List<Res>();
- public static void RefreshResolutions()
- {
- AvailableResolutions.Clear();
- float targetAspect = 16f / 9f;
- float tolerance = 0.01f;
- Dictionary<(int w, int h), int> best = new();
- foreach (var r in Screen.resolutions)
- {
- float aspect = (float)r.width / r.height;
- if (Mathf.Abs(aspect - targetAspect) < tolerance)
- {
- int hz = Mathf.RoundToInt((float)r.refreshRateRatio.value);
- var key = (r.width, r.height);
- if (!best.ContainsKey(key) || hz > best[key])
- best[key] = hz;
- }
- }
- foreach (var kvp in best)
- {
- AvailableResolutions.Add(new Res
- {
- width = kvp.Key.w,
- height = kvp.Key.h,
- refreshRate = kvp.Value
- });
- }
- }
- public static int BestDesktopResolutionIndex()
- {
- if (AvailableResolutions.Count == 0) return -1;
- var cur = Screen.currentResolution;
- for (int i = 0; i < AvailableResolutions.Count; i++)
- if (AvailableResolutions[i].width == cur.width && AvailableResolutions[i].height == cur.height)
- return i;
- return -1;
- }
- public static void ApplyResolution()
- {
- #if UNITY_EDITOR
- return;
- #endif
- if (ResolutionIndex < 0 || ResolutionIndex >= AvailableResolutions.Count) return;
- var r = AvailableResolutions[ResolutionIndex];
- Screen.SetResolution(r.width, r.height, Fullscreen, r.refreshRate);
- }
Advertisement
Add Comment
Please, Sign In to add comment