Skip to content

Instantly share code, notes, and snippets.

@angavrilov
Last active January 3, 2016 11:59
Show Gist options
  • Save angavrilov/8459884 to your computer and use it in GitHub Desktop.
Save angavrilov/8459884 to your computer and use it in GitHub Desktop.
A trivial FPS indicator for KSP
// LICENSE: Public Domain
using System;
using UnityEngine;
namespace FPS
{
[KSPAddon(KSPAddon.Startup.EveryScene, false)]
public class FPSIndicator : MonoBehaviour
{
// Compute average FPS over this many frames
private const int NUM_SAMPLES = 10;
private float[] times;
private int cur_sample = 0;
private Rect pos;
private string fps;
private GUIStyle style = null;
FPSIndicator()
{
times = new float[NUM_SAMPLES];
float time = Time.realtimeSinceStartup;
for (int i = 0; i < NUM_SAMPLES; i++)
times[i] = time;
}
public void LateUpdate()
{
float time = Time.realtimeSinceStartup;
float delta = time - times[cur_sample];
times[cur_sample] = time;
cur_sample = (cur_sample+1) % NUM_SAMPLES;
fps = (NUM_SAMPLES / delta).ToString("F1");
}
public void OnGUI()
{
if (style == null)
{
pos = new Rect(Screen.width - 60, 0, 50, 60);
style = new GUIStyle(GUI.skin.label);
style.alignment = TextAnchor.UpperRight;
style.normal.textColor = new Color(0.7f, 0.7f, 0.7f, 0.5f);
}
GUI.Label(pos, fps, style);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment