Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Frames Per Second visual display
- // Robotduck 2010
- using UnityEngine;
- using System.Collections;
- public class FramesPerSecond : MonoBehaviour {
- private float updatePeriod = 0.5f;
- private float[] runningAverages = { 60, 60, 60, 60, 60 };
- private int runningAverageIndex = 0;
- private float fps;
- private float countFrames = 0;
- private float nextAverageTime;
- private float lastSampleTime;
- void Start ()
- {
- float now = Time.realtimeSinceStartup;
- nextAverageTime = now + updatePeriod;
- lastSampleTime = now;
- }
- void Update ()
- {
- float now = Time.realtimeSinceStartup;
- if (now > nextAverageTime)
- {
- float samplePeriod = now - lastSampleTime;
- lastSampleTime = now;
- float periodFPS = countFrames / samplePeriod;
- runningAverageIndex++;
- runningAverageIndex %= runningAverages.Length;
- runningAverages[runningAverageIndex] = periodFPS;
- fps = 0;
- foreach(float entry in runningAverages)
- {
- fps += entry;
- }
- fps /= runningAverages.Length;
- nextAverageTime += updatePeriod;
- countFrames = 0;
- }
- countFrames++;
- }
- void OnGUI ()
- {
- GUI.Label (new Rect (10, 10, 100, 20), ((int) fps)+" FPS");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment