FrameRateControls.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System;
  2. using UnityEngine;
  3. namespace Leap.Unity
  4. {
  5. public class FrameRateControls : MonoBehaviour
  6. {
  7. private void Awake()
  8. {
  9. if (QualitySettings.vSyncCount != 0)
  10. {
  11. Debug.LogWarning("vSync will override target frame rate. vSyncCount = " + QualitySettings.vSyncCount);
  12. }
  13. Application.targetFrameRate = this.targetRenderRate;
  14. Time.fixedDeltaTime = 1f / (float)this.fixedPhysicsRate;
  15. }
  16. private void Update()
  17. {
  18. if (Input.GetKey(this.unlockRender))
  19. {
  20. if (Input.GetKeyDown(this.decrease) && this.targetRenderRate > this.targetRenderRateStep)
  21. {
  22. this.targetRenderRate -= this.targetRenderRateStep;
  23. Application.targetFrameRate = this.targetRenderRate;
  24. }
  25. if (Input.GetKeyDown(this.increase))
  26. {
  27. this.targetRenderRate += this.targetRenderRateStep;
  28. Application.targetFrameRate = this.targetRenderRate;
  29. }
  30. if (Input.GetKeyDown(this.resetRate))
  31. {
  32. this.ResetRender();
  33. }
  34. }
  35. if (Input.GetKey(this.unlockPhysics))
  36. {
  37. if (Input.GetKeyDown(this.decrease) && this.fixedPhysicsRate > this.fixedPhysicsRateStep)
  38. {
  39. this.fixedPhysicsRate -= this.fixedPhysicsRateStep;
  40. Time.fixedDeltaTime = 1f / (float)this.fixedPhysicsRate;
  41. }
  42. if (Input.GetKeyDown(this.increase))
  43. {
  44. this.fixedPhysicsRate += this.fixedPhysicsRateStep;
  45. Time.fixedDeltaTime = 1f / (float)this.fixedPhysicsRate;
  46. }
  47. if (Input.GetKeyDown(this.resetRate))
  48. {
  49. this.ResetPhysics();
  50. }
  51. }
  52. }
  53. public void ResetRender()
  54. {
  55. this.targetRenderRate = 60;
  56. Application.targetFrameRate = -1;
  57. }
  58. public void ResetPhysics()
  59. {
  60. this.fixedPhysicsRate = 50;
  61. Time.fixedDeltaTime = 0.02f;
  62. }
  63. public void ResetAll()
  64. {
  65. this.ResetRender();
  66. this.ResetPhysics();
  67. }
  68. public int targetRenderRate = 60;
  69. public int targetRenderRateStep = 1;
  70. public int fixedPhysicsRate = 50;
  71. public int fixedPhysicsRateStep = 1;
  72. public KeyCode unlockRender = KeyCode.RightShift;
  73. public KeyCode unlockPhysics = KeyCode.LeftShift;
  74. public KeyCode decrease = KeyCode.DownArrow;
  75. public KeyCode increase = KeyCode.UpArrow;
  76. public KeyCode resetRate = KeyCode.Backspace;
  77. }
  78. }