Eonirr

Untitled

May 27th, 2026 (edited)
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. public static readonly List<Res> AvailableResolutions = new List<Res>();
  2.  
  3. public static void RefreshResolutions()
  4. {
  5. AvailableResolutions.Clear();
  6.  
  7. float targetAspect = 16f / 9f;
  8. float tolerance = 0.01f;
  9.  
  10. Dictionary<(int w, int h), int> best = new();
  11.  
  12. foreach (var r in Screen.resolutions)
  13. {
  14. float aspect = (float)r.width / r.height;
  15.  
  16. if (Mathf.Abs(aspect - targetAspect) < tolerance)
  17. {
  18. int hz = Mathf.RoundToInt((float)r.refreshRateRatio.value);
  19. var key = (r.width, r.height);
  20.  
  21. if (!best.ContainsKey(key) || hz > best[key])
  22. best[key] = hz;
  23. }
  24. }
  25.  
  26. foreach (var kvp in best)
  27. {
  28. AvailableResolutions.Add(new Res
  29. {
  30. width = kvp.Key.w,
  31. height = kvp.Key.h,
  32. refreshRate = kvp.Value
  33. });
  34. }
  35. }
  36.  
  37. public static int BestDesktopResolutionIndex()
  38. {
  39. if (AvailableResolutions.Count == 0) return -1;
  40.  
  41. var cur = Screen.currentResolution;
  42. for (int i = 0; i < AvailableResolutions.Count; i++)
  43. if (AvailableResolutions[i].width == cur.width && AvailableResolutions[i].height == cur.height)
  44. return i;
  45.  
  46. return -1;
  47. }
  48.  
  49.  
  50.  
  51.  
  52. public static void ApplyResolution()
  53. {
  54. #if UNITY_EDITOR
  55. return;
  56. #endif
  57. if (ResolutionIndex < 0 || ResolutionIndex >= AvailableResolutions.Count) return;
  58. var r = AvailableResolutions[ResolutionIndex];
  59. Screen.SetResolution(r.width, r.height, Fullscreen, r.refreshRate);
  60. }
Advertisement
Add Comment
Please, Sign In to add comment