Island demo FPSCounter js -> C#
Posted: 14 Nov 2010, 01:09
Hi,
I converted the FPSCounter.js from the Island demo to C#, it's working but i think there is a small error in it since the movement of the players is a bit jerky. If someone could have a look and tell me what has to be changed or can be improved.
I converted the FPSCounter.js from the Island demo to C#, it's working but i think there is a small error in it since the movement of the players is a bit jerky. If someone could have a look and tell me what has to be changed or can be improved.
Code: Select all
using UnityEngine;
using System.Collections;
public class FPSCounter : MonoBehaviour
{
float updateInterval = 1.0f;
private float accum = 0.0f;
private int frames = 0;
private float timeleft;
private float fps = 15.0f;
private float oldFps = 15.0f;
private float lastSample;
private int gotIntervals = 0;
void Start()
{
timeleft = updateInterval;
lastSample = Time.realtimeSinceStartup;
}
float GetFPS() { return fps; }
bool HasFPS() { return gotIntervals > 2; }
void Update()
{
++frames;
float newSample = Time.realtimeSinceStartup;
float deltaTime = newSample - lastSample;
lastSample = newSample;
timeleft -= deltaTime;
accum += 1.0f / deltaTime;
if (timeleft <= 0.0f)
{
fps = accum / frames;
if (fps != oldFps)
{
oldFps = fps;
SendMessage("FPSChanged", fps);
}
guiText.text = fps.ToString("f2");
timeleft = updateInterval;
accum = 0.0f;
frames = 0;
++gotIntervals;
}
}
}