Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Simple FPS conter for Unity
using UnityEngine;
/// <summary>
/// ~ Fps counter for unity ~
/// Brief : Calculate the FPS and display it on the screen
/// HowTo : Create empty object at initial scene and attach this script!!!
/// </summary>
public class UniFPSCounter : MonoBehaviour
{
// for ui.
private int screenLongSide;
private Rect boxRect;
private GUIStyle style = new GUIStyle();
// for fps calculation.
private int frameCount;
private float elapsedTime;
private double frameRate;
/// <summary>
/// Initialization
/// </summary>
private void Awake()
{
DontDestroyOnLoad(gameObject);
UpdateUISize();
}
/// <summary>
/// Monitor changes in resolution and calcurate FPS
/// </summary>
private void Update()
{
// FPS calculation
frameCount++;
elapsedTime += Time.deltaTime;
if (elapsedTime > 0.5f)
{
frameRate = System.Math.Round(frameCount / elapsedTime, 1, System.MidpointRounding.AwayFromZero);
frameCount = 0;
elapsedTime = 0;
// Update the UI size if the resolution has changed
if (screenLongSide != Mathf.Max(Screen.width, Screen.height))
{
UpdateUISize();
}
}
}
/// <summary>
/// Resize the UI according to the screen resolution
/// </summary>
private void UpdateUISize()
{
screenLongSide = Mathf.Max(Screen.width, Screen.height);
var rectLongSide = screenLongSide / 10;
boxRect = new Rect(1, 1, rectLongSide, rectLongSide / 3);
style.fontSize = (int)(screenLongSide / 36.8);
style.normal.textColor = Color.white;
}
/// <summary>
/// Display FPS
/// </summary>
private void OnGUI()
{
GUI.Box(boxRect, "");
GUI.Label(boxRect, " " + frameRate + "fps", style);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment