68 lines
1.8 KiB
C#
68 lines
1.8 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace Crosstales.UI.Util
|
|
{
|
|
/// <summary>Simple FPS-Counter.</summary>
|
|
public class FPSDisplay : MonoBehaviour
|
|
{
|
|
#region Variables
|
|
|
|
/// <summary>Text component to display the FPS.</summary>
|
|
[Tooltip("Text component to display the FPS.")] public Text FPS;
|
|
|
|
/// <summary>Update every set frame.</summary>
|
|
[Tooltip("Update every set frame."), Range(1, 300)] public int FrameUpdate = 5;
|
|
|
|
private float deltaTime;
|
|
private float elapsedTime;
|
|
|
|
private float msec;
|
|
private float fps;
|
|
|
|
private const string wait = "<i>...calculating <b>FPS</b>...</i>";
|
|
private const string red = "<color=#E57373><b>FPS: {0:0.}</b> ({1:0.0} ms)</color>";
|
|
private const string orange = "<color=#FFB74D><b>FPS: {0:0.}</b> ({1:0.0} ms)</color>";
|
|
private const string green = "<color=#81C784><b>FPS: {0:0.}</b> ({1:0.0} ms)</color>";
|
|
|
|
#endregion
|
|
|
|
|
|
#region MonoBehaviour methods
|
|
|
|
private void Update()
|
|
{
|
|
deltaTime += (Time.deltaTime - deltaTime) * 0.1f;
|
|
elapsedTime += Time.deltaTime;
|
|
|
|
if (elapsedTime > 1f)
|
|
{
|
|
if (Time.frameCount % FrameUpdate == 0)
|
|
{
|
|
msec = deltaTime * 1000f;
|
|
fps = 1f / deltaTime;
|
|
|
|
if (fps < 15f)
|
|
{
|
|
FPS.text = string.Format(red, fps, msec);
|
|
}
|
|
else if (fps < 29f)
|
|
{
|
|
FPS.text = string.Format(orange, fps, msec);
|
|
}
|
|
else
|
|
{
|
|
FPS.text = string.Format(green, fps, msec);
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
FPS.text = wait;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|
|
// © 2017-2020 crosstales LLC (https://www.crosstales.com) |