Skip to content

Instantly share code, notes, and snippets.

@GeorgiyRyaposov
Last active March 21, 2017 10:39
Show Gist options
  • Save GeorgiyRyaposov/5be46d5006ea333f34ac592e6b6fc6f4 to your computer and use it in GitHub Desktop.
Save GeorgiyRyaposov/5be46d5006ea333f34ac592e6b6fc6f4 to your computer and use it in GitHub Desktop.
using UnityEngine;
using UnityEngine.UI;
namespace Assets.Scripts.Tools
{
[RequireComponent(typeof(Text))]
public class FPSCounter : MonoBehaviour
{
const float fpsMeasurePeriod = 0.5f;
private int m_FpsAccumulator = 0;
private float m_FpsNextPeriod = 0;
private int m_CurrentFps;
private Text m_Text;
private string[] stringAllocTweak;
private void Start()
{
var maxFps = 300;
stringAllocTweak = new string[maxFps];
for (int i = 0; i < maxFps; i++)
{
stringAllocTweak[i] = i.ToString();// string.Format(display, i.ToString());
}
m_FpsNextPeriod = Time.realtimeSinceStartup + fpsMeasurePeriod;
m_Text = GetComponent<Text>();
}
private void Update()
{
// measure average frames per second
m_FpsAccumulator++;
if (Time.realtimeSinceStartup > m_FpsNextPeriod)
{
m_CurrentFps = (int)(m_FpsAccumulator / fpsMeasurePeriod);
m_FpsAccumulator = 0;
m_FpsNextPeriod += fpsMeasurePeriod;
m_Text.text = stringAllocTweak[m_CurrentFps];
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment