FrameRateControls.cs
3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/******************************************************************************
* Copyright (C) Leap Motion, Inc. 2011-2017. *
* Leap Motion proprietary and confidential. *
* *
* Use subject to the terms of the Leap Motion SDK Agreement available at *
* https://developer.leapmotion.com/sdk_agreement, or another agreement *
* between Leap Motion and you, your company or other organization. *
******************************************************************************/
using UnityEngine;
using System.Collections;
namespace Leap.Unity{
/// <summary>
/// Provides control of target frame rate.
/// </summary>
/// <remarks>
/// This utility is useful for verifying frame-rate independence of behaviors.
/// </remarks>
public class FrameRateControls : MonoBehaviour {
public int targetRenderRate = 60; // must be > 0
public int targetRenderRateStep = 1;
public int fixedPhysicsRate = 50; // must be > 0
public int fixedPhysicsRateStep = 1;
public KeyCode unlockRender = KeyCode.RightShift;
public KeyCode unlockPhysics = KeyCode.LeftShift;
public KeyCode decrease = KeyCode.DownArrow;
public KeyCode increase = KeyCode.UpArrow;
public KeyCode resetRate = KeyCode.Backspace;
// Use this for initialization
void Awake () {
if (QualitySettings.vSyncCount != 0) {
Debug.LogWarning ("vSync will override target frame rate. vSyncCount = " + QualitySettings.vSyncCount);
}
Application.targetFrameRate = targetRenderRate;
Time.fixedDeltaTime = 1f/((float)fixedPhysicsRate);
}
// Update is called once per frame
void Update () {
if (Input.GetKey (unlockRender)) {
if (Input.GetKeyDown (decrease)) {
if (targetRenderRate > targetRenderRateStep) {
targetRenderRate -= targetRenderRateStep;
Application.targetFrameRate = targetRenderRate;
}
}
if (Input.GetKeyDown (increase)) {
targetRenderRate += targetRenderRateStep;
Application.targetFrameRate = targetRenderRate;
}
if (Input.GetKeyDown (resetRate)) {
ResetRender();
}
}
if (Input.GetKey (unlockPhysics)) {
if (Input.GetKeyDown (decrease)) {
if (fixedPhysicsRate > fixedPhysicsRateStep) {
fixedPhysicsRate -= fixedPhysicsRateStep;
Time.fixedDeltaTime = 1f/((float)fixedPhysicsRate);
}
}
if (Input.GetKeyDown (increase)) {
fixedPhysicsRate += fixedPhysicsRateStep;
Time.fixedDeltaTime = 1f/((float)fixedPhysicsRate);
}
if (Input.GetKeyDown (resetRate)) {
ResetPhysics();
}
}
}
public void ResetRender() {
targetRenderRate = 60;
Application.targetFrameRate = -1;
}
public void ResetPhysics() {
fixedPhysicsRate = 50;
Time.fixedDeltaTime = 0.02f;
}
public void ResetAll() {
ResetRender ();
ResetPhysics ();
}
}
}