-
Notifications
You must be signed in to change notification settings - Fork 0
/
FPSScript.cs
51 lines (44 loc) · 1.12 KB
/
FPSScript.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
45
46
47
48
49
50
51
using UnityEngine;
using System.Collections;
public class FPSScript : MonoBehaviour {
public float updateInterval = 0.5F;
public GUIText text;
private float accum = 0; // FPS accumulated over the interval
private int frames = 0; // Frames drawn over the interval
private float timeleft; // Left time for current interval
void Start()
{
if( !text )
{
Debug.Log("UtilityFramesPerSecond needs a GUIText component!");
enabled = false;
return;
}
timeleft = updateInterval;
}
void Update()
{
timeleft -= Time.deltaTime;
accum += Time.timeScale/Time.deltaTime;
++frames;
// Interval ended - update GUI text and start new interval
if( timeleft <= 0.0 )
{
// display two fractional digits (f2 format)
float fps = accum/frames;
string format = System.String.Format("{0:F2} FPS",fps);
text.text = format;
if(fps < 30)
text.material.color = Color.yellow;
else
if(fps < 10)
text.material.color = Color.red;
else
text.material.color = Color.green;
// DebugConsole.Log(format,level);
timeleft = updateInterval;
accum = 0.0F;
frames = 0;
}
}
}