Page 1 of 1

Island demo FPSCounter js -> C#

Posted: 14 Nov 2010, 01:09
by appels

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()
        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;


Posted: 14 Nov 2010, 08:13
by ThomasLund
Only thing could be the "uncontrollable" SendMessage. It can be pretty expensive depending on how many game objects/components it hits.

Try to comment it out and take a look if it fixed anything - or make a hard link to whoever needs to get that message


Posted: 14 Nov 2010, 22:20
by appels
Thanks Thomas,

I tried both ( commented out and hard link ) but the result is the same.
One thing thats different then the unity script is the lastsample var.
It's a double in unityscript and i had to change it to a float in C# for it to work.
I get a missing cast error on :
float deltaTime = newSample - lastSample;
if i leave the Double on lastsample.
I have no idea how to convert it otherwise.


Posted: 14 Nov 2010, 22:30
by appels
float deltaTime = newSample - (float)lastSample;
seems to do the conversion bu the problem still excists.

Any other ideas ?

Posted: 14 Nov 2010, 23:22
by appels
Solved, wrong class name but didn't get the error in the editor.