Skip to content

Instantly share code, notes, and snippets.

@HolyFot
Created September 14, 2020 17:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HolyFot/4be46957ba56357305d776c569d6e927 to your computer and use it in GitHub Desktop.
Save HolyFot/4be46957ba56357305d776c569d6e927 to your computer and use it in GitHub Desktop.
FPS Counter + Hardware Info
//Displays: Accurate FPS Counter + Hardware Info
//Author: HolyFot
//License: CC0
using UnityEngine;
using System;
using System.Collections;
public class FPSInfo : MonoBehaviour
{
public KeyCode openFPS = KeyCode.P;
public bool state;
private float deltaTime = 0.0f;
private float fpsTimer = 1.0f;
private float currTimer;
private int fpsCount = 0;
public bool isEnabled = false;
private int currentFPS = 0;
private string memory = "";
private string cpuMake = "";
private string gpuMake = "";
void Awake()
{
cpuMake = SystemInfo.processorType.ToString();
memory = FormatMem(SystemInfo.systemMemorySize));
gpuMake = SystemInfo.graphicsDeviceName);
}
void OnEnable()
{
isEnabled = true;
StartCoroutine("FPSCalc");
}
void OnDisable()
{
isEnabled = false;
StopCoroutine("FPSCalc");
}
void FixedUpdate()
{
//Update FPS (Not 100% accurate)
//deltaTime += (Time.deltaTime - deltaTime) * 0.1f;
//Update FPS Timer
currTimer -= Time.deltaTime;
if (currTimer <= 0.0f) //1 Second
{
//Calculate Accurate FPS & Reset
currentFPS = fpsCount;
fpsCount = 0;
currTimer =fpsTimer; //Reset Timer
}
if (Input.GetKeyDown(openFPS))
{
if (state == false) //Toggle
state = true;
else
state = false;
}
}
private void OnGUI()
{
if (state)
{
//CALC FPS & UPDATE TIME
float msec = deltaTime * 1000.0f;
float fps = 1.0f / deltaTime;
string text = string.Format("{0:0.0} ms ({1:0.} fps)", msec, fps); //currentFPS
GUILayout.Label("FPS: " + text);
//Monitor Info
GUILayout.Label("Resolution: " + Screen.width + "x" + Screen.height + ": " + Screen.currentResolution.refreshRate + " Hz");
//Hardware Info
GUILayout.Label("CPU: " + cpuMake);
GUILayout.Label("Memory: " + memory;
GUILayout.Label("GFX Card: " + gpuMake;
}
}
IEnumerator FPSCalc()
{
while (isEnabled)
{
yield return 0; //Count Each Frame
fpsCount++;
}
}
private string FormatMem(long num)
{
long i = (long)Math.Pow(10, (int)Math.Max(0, Math.Log10(num) - 2));
num = num / i * i;
if (num >= 1000)
return (num / 1000D).ToString("0.##") + "GB";
return num.ToString("#,0") + "MB";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment