-
Notifications
You must be signed in to change notification settings - Fork 450
/
Copy pathFPSCounter.cs
44 lines (35 loc) · 1002 Bytes
/
FPSCounter.cs
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
using UnityEngine;
namespace UnityLibrary
{
public class FPSCounter : MonoBehaviour
{
[SerializeField]
private float updateInterval = 0.1f;
private float accum = 0.0f;
private int frames = 0;
private float timeleft;
int qty;
float fps;
float avgFps;
void Update()
{
timeleft -= Time.deltaTime;
accum += Time.timeScale / Time.deltaTime;
++frames;
if (timeleft <= 0.0)
{
fps = (accum / frames);
timeleft = updateInterval;
accum = 0f;
frames = 0;
}
qty++;
avgFps += (fps - avgFps) / qty;
}
void OnGUI()
{
GUI.Label(new Rect(Screen.width - 150, 0, 150, 20), "FPS: " + fps.ToString("f2"));
GUI.Label(new Rect(Screen.width - 150, 20, 150, 20), "Avg FPS: " + avgFps.ToString("f2"));
}
}
}