Sin descripción
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VariableRefreshRateControl.cs 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. #if UNITY_ADAPTIVE_PERFORMANCE_SAMSUNG_ANDROID
  5. using UnityEngine.AdaptivePerformance.Samsung.Android;
  6. #endif
  7. public class VariableRefreshRateControl : MonoBehaviour
  8. {
  9. public Dropdown supportedRefreshRates;
  10. public Text currentRefreshRate;
  11. public Slider targetRefreshRate;
  12. public Text targetRefreshRateLabel;
  13. public GameObject notSupportedPanel;
  14. // How long to run the test (in seconds)
  15. public float timeOut = 80;
  16. #if UNITY_ADAPTIVE_PERFORMANCE_SAMSUNG_ANDROID
  17. bool didVRRSupportChange = false;
  18. #endif
  19. float timeOuttimer = 0;
  20. void Start()
  21. {
  22. timeOuttimer = timeOut;
  23. Application.targetFrameRate = 60;
  24. targetRefreshRate.SetValueWithoutNotify(60);
  25. #if UNITY_ADAPTIVE_PERFORMANCE_SAMSUNG_ANDROID
  26. if (VariableRefreshRate.Instance == null)
  27. {
  28. Debug.Log("[AP VRR] Variable Refresh Rate is not supported on this device.");
  29. notSupportedPanel.SetActive(true);
  30. return;
  31. }
  32. VariableRefreshRate.Instance.RefreshRateChanged += UpdateDropdown;
  33. supportedRefreshRates.onValueChanged.AddListener(delegate {
  34. if (!VariableRefreshRate.Instance.SetRefreshRateByIndex(supportedRefreshRates.value))
  35. UpdateDropdown();
  36. });
  37. #else
  38. notSupportedPanel.SetActive(true);
  39. #endif
  40. targetRefreshRate.onValueChanged.AddListener(delegate {
  41. Application.targetFrameRate = (int)targetRefreshRate.value;
  42. });
  43. UpdateDropdown();
  44. }
  45. void Update()
  46. {
  47. #if UNITY_ADAPTIVE_PERFORMANCE_SAMSUNG_ANDROID
  48. notSupportedPanel.SetActive(VariableRefreshRate.Instance == null);
  49. #endif
  50. targetRefreshRateLabel.text = $"Target Refresh Rate: {Application.targetFrameRate} Hz";
  51. timeOuttimer -= Time.deltaTime;
  52. if (timeOuttimer < 0)
  53. {
  54. #if UNITY_EDITOR
  55. UnityEditor.EditorApplication.isPlaying = false;
  56. #else
  57. Application.Quit();
  58. #endif
  59. }
  60. #if UNITY_ADAPTIVE_PERFORMANCE_SAMSUNG_ANDROID
  61. if (VariableRefreshRate.Instance == null)
  62. {
  63. UpdateDropdown();
  64. didVRRSupportChange = true;
  65. currentRefreshRate.text = $"Current refresh rate: - Hz";
  66. return;
  67. }
  68. else
  69. {
  70. if (didVRRSupportChange)
  71. {
  72. didVRRSupportChange = false;
  73. UpdateDropdown();
  74. }
  75. }
  76. currentRefreshRate.text = $"Current refresh rate: {VariableRefreshRate.Instance.CurrentRefreshRate} Hz";
  77. #endif
  78. }
  79. void UpdateDropdown()
  80. {
  81. var options = new List<Dropdown.OptionData>();
  82. supportedRefreshRates.ClearOptions();
  83. var index = -1;
  84. #if UNITY_ADAPTIVE_PERFORMANCE_SAMSUNG_ANDROID
  85. if (VariableRefreshRate.Instance == null)
  86. return;
  87. for (var i = 0; i < VariableRefreshRate.Instance.SupportedRefreshRates.Length; ++i)
  88. {
  89. var rr = VariableRefreshRate.Instance.SupportedRefreshRates[i];
  90. options.Add(new Dropdown.OptionData(rr.ToString()));
  91. if (rr == VariableRefreshRate.Instance.CurrentRefreshRate)
  92. index = i;
  93. }
  94. #endif
  95. supportedRefreshRates.AddOptions(options);
  96. supportedRefreshRates.SetValueWithoutNotify(index);
  97. }
  98. }