Last active
January 3, 2016 11:59
-
-
Save angavrilov/8459884 to your computer and use it in GitHub Desktop.
A trivial FPS indicator for KSP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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