UIStartGraphicOptionController.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using System;
  2. using I2.Loc;
  3. using UnityEngine;
  4. public class UIStartGraphicOptionController : MonoBehaviour
  5. {
  6. private void Start()
  7. {
  8. this._fps = new UIStartGraphicOptionController.FpsOptions();
  9. this.UpdateResolutionLabel();
  10. this.UpdateFullScreenLabel(Screen.fullScreen);
  11. this.UpdateVSyncLabel();
  12. this.UpdateFpsLabel();
  13. this.UpdateFpsWarningLabel();
  14. }
  15. private void OnEnable()
  16. {
  17. this.UpdateResolutionLabel();
  18. }
  19. private void OnDisable()
  20. {
  21. R.Settings.Save();
  22. }
  23. public void OnResolutionNext()
  24. {
  25. this.OnResolutionChange(1);
  26. }
  27. public void OnResolutionPrevious()
  28. {
  29. this.OnResolutionChange(-1);
  30. }
  31. public void OnFpsNext()
  32. {
  33. this.OnFpsChange(1);
  34. }
  35. public void OnFpsPrevious()
  36. {
  37. this.OnFpsChange(-1);
  38. }
  39. private void OnFpsChange(int step)
  40. {
  41. this._fps.SetFpsByStep(step);
  42. this.UpdateFpsLabel();
  43. this.UpdateFpsWarningLabel();
  44. }
  45. private void OnResolutionChange(int step)
  46. {
  47. Singleton<ResolutionOption>.Instance.SetResolutionByOffset(step);
  48. this.UpdateResolutionLabel();
  49. this.UpdateFpsWarningLabel();
  50. }
  51. private void OnFullScreenClick()
  52. {
  53. this.UpdateFullScreenLabel(!Screen.fullScreen);
  54. Screen.fullScreen = !Screen.fullScreen;
  55. }
  56. private void OnVSyncChange(int step)
  57. {
  58. QualitySettings.vSyncCount = (QualitySettings.vSyncCount + step + 3) % 3;
  59. this.UpdateVSyncLabel();
  60. R.Settings.VSync = QualitySettings.vSyncCount;
  61. R.Settings.Save();
  62. }
  63. private void UpdateResolutionLabel()
  64. {
  65. this._resolutionLabel.text = Singleton<ResolutionOption>.Instance.ResolutionString;
  66. }
  67. private void UpdateFpsLabel()
  68. {
  69. this._fpsLabel.text = Application.targetFrameRate.ToString();
  70. }
  71. private void UpdateFpsWarningLabel()
  72. {
  73. bool active = Application.targetFrameRate == this._fps.MaxFps && QualitySettings.GetQualityLevel() == 2;
  74. this._fpsWarningGameObject.SetActive(active);
  75. }
  76. private void UpdateFullScreenLabel(bool isFullScreen)
  77. {
  78. this._fullScreenLocalize.Term = ((!isFullScreen) ? "ui/start/off" : "ui/start/on");
  79. }
  80. private void UpdateVSyncLabel()
  81. {
  82. switch (QualitySettings.vSyncCount)
  83. {
  84. case 0:
  85. this._vSyncLocalize.Term = "ui/start/off";
  86. break;
  87. case 1:
  88. this._vSyncLocalize.Term = "ui/start/on";
  89. break;
  90. case 2:
  91. this._vSyncLocalize.Term = "ui/start/half";
  92. break;
  93. default:
  94. throw new ArgumentOutOfRangeException();
  95. }
  96. }
  97. [SerializeField]
  98. private GameObject _fullScreenButton;
  99. [SerializeField]
  100. private GameObject _fpsWarningGameObject;
  101. [SerializeField]
  102. private Localize _fullScreenLocalize;
  103. [SerializeField]
  104. private GameObject _resolutionButton;
  105. [SerializeField]
  106. private UILabel _resolutionLabel;
  107. [SerializeField]
  108. private GameObject _vSyncButton;
  109. [SerializeField]
  110. private Localize _vSyncLocalize;
  111. [SerializeField]
  112. private UILabel _fpsLabel;
  113. private UIStartGraphicOptionController.FpsOptions _fps;
  114. private class FpsOptions
  115. {
  116. public FpsOptions()
  117. {
  118. int targetFrameRate = Application.targetFrameRate;
  119. if (targetFrameRate != 30)
  120. {
  121. if (targetFrameRate == 60)
  122. {
  123. this._fpsIndex = 0;
  124. }
  125. }
  126. else
  127. {
  128. this._fpsIndex = 1;
  129. }
  130. }
  131. public void SetFpsByStep(int step)
  132. {
  133. this._fpsIndex = Mathf.Clamp(this._fpsIndex + step, 0, this._fpsArray.Length - 1);
  134. Application.targetFrameRate = this._fpsArray[this._fpsIndex];
  135. R.Settings.FPS = this._fpsArray[this._fpsIndex];
  136. }
  137. private readonly int[] _fpsArray = new int[]
  138. {
  139. 30,
  140. 60
  141. };
  142. private int _fpsIndex;
  143. public readonly int MaxFps = 60;
  144. }
  145. }