summaryrefslogtreecommitdiff
path: root/FPSmeter.cs
blob: f762fe7a6add00966cbb30e1bed1b54775e29619 (plain)
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
using UnityEngine;

public class FPSmeter : MonoBehaviour
{
	public float updateInterval = 0.5f;

	private float lastInterval;

	private int frames;

	public static float fps;

	public bool showFPS;

	private void Start()
	{
		lastInterval = Time.realtimeSinceStartup;
		frames = 0;
	}

	private void OnGUI()
	{
		if (showFPS)
		{
			GUI.Label(new Rect(10f, 10f, 100f, 20f), string.Empty + Mathf.Round(fps * 100f) / 100f);
		}
	}

	private void Update()
	{
		frames++;
		float realtimeSinceStartup = Time.realtimeSinceStartup;
		if (realtimeSinceStartup > lastInterval + updateInterval)
		{
			fps = (float)frames / (realtimeSinceStartup - lastInterval);
			frames = 0;
			lastInterval = realtimeSinceStartup;
		}
	}
}