56 lines
1.6 KiB
C#
56 lines
1.6 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace Misc
|
|
{
|
|
[RequireComponent(typeof(Text))]
|
|
public class FPSMeter : MonoBehaviour
|
|
{
|
|
public float updateInterval = 0.5F;
|
|
|
|
private float accumulator = 0; // FPS accumulated over the interval
|
|
private int frames = 0; // Frames drawn over the interval
|
|
private float timeleft; // Left time for current interval
|
|
|
|
private Text _textComponent;
|
|
|
|
private void Start()
|
|
{
|
|
gameObject.SetActive(Debug.isDebugBuild);
|
|
|
|
_textComponent = GetComponent<Text>();
|
|
timeleft = updateInterval;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
timeleft -= Time.deltaTime;
|
|
accumulator += Time.timeScale/Time.deltaTime;
|
|
++frames;
|
|
|
|
// Interval ended - update GUI text and start new interval
|
|
if (timeleft <= 0.0)
|
|
{
|
|
// Set on top of the display hierarchy
|
|
transform.SetAsLastSibling();
|
|
|
|
// display two fractional digits (f2 format)
|
|
float fps = accumulator/frames;
|
|
string format = System.String.Format("{0:F2} FPS", fps);
|
|
_textComponent.text = format;
|
|
|
|
if (fps < 30)
|
|
_textComponent.color = Color.yellow;
|
|
else if (fps < 10)
|
|
_textComponent.color = Color.red;
|
|
else
|
|
_textComponent.color = Color.green;
|
|
|
|
// DebugConsole.Log(format,level);
|
|
timeleft = updateInterval;
|
|
accumulator = 0.0F;
|
|
frames = 0;
|
|
}
|
|
}
|
|
}
|
|
}
|