Skip to content

Commit 9c5cea5

Browse files
committed
Added FPS Counter => Calculates FPS and Avg FPS
1 parent 625d686 commit 9c5cea5

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

Scripts/Helpers/FPSCounter.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using UnityEngine;
2+
3+
public class FPSCounter : MonoBehaviour
4+
{
5+
[SerializeField]
6+
private float updateInterval = 0.1f;
7+
8+
private float accum = 0.0f;
9+
private int frames = 0;
10+
private float timeleft;
11+
12+
int qty;
13+
14+
float fps;
15+
float avgFps;
16+
17+
void Update()
18+
{
19+
timeleft -= Time.deltaTime;
20+
accum += Time.timeScale / Time.deltaTime;
21+
++frames;
22+
23+
if (timeleft <= 0.0)
24+
{
25+
fps = (accum / frames);
26+
timeleft = updateInterval;
27+
accum = 0f;
28+
frames = 0;
29+
}
30+
31+
qty++;
32+
33+
avgFps += (fps - avgFps) / qty;
34+
}
35+
36+
void OnGUI()
37+
{
38+
GUI.Label(new Rect(Screen.width - 150, 0, 150, 20), "FPS: " + fps.ToString("f2"));
39+
GUI.Label(new Rect(Screen.width - 150, 20, 150, 20), "Avg FPS: " + avgFps.ToString("f2"));
40+
}
41+
42+
}

0 commit comments

Comments
 (0)