Island demo FPSCounter js -> C#

Post here all your questions related with SmartFoxServer .Net/Unity3D API

Moderators: Lapo, Bax

appels
Posts: 464
Joined: 28 Jul 2010, 02:12
Contact:

Island demo FPSCounter js -> C#

Postby appels » 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.

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;
        }
    }

}
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Postby ThomasLund » 14 Nov 2010, 08:13

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

/Thomas
appels
Posts: 464
Joined: 28 Jul 2010, 02:12
Contact:

Postby appels » 14 Nov 2010, 22:20

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.

Eddy.
appels
Posts: 464
Joined: 28 Jul 2010, 02:12
Contact:

Postby appels » 14 Nov 2010, 22:30

float deltaTime = newSample - (float)lastSample;
seems to do the conversion bu the problem still excists.

Any other ideas ?
appels
Posts: 464
Joined: 28 Jul 2010, 02:12
Contact:

Postby appels » 14 Nov 2010, 23:22

Solved, wrong class name but didn't get the error in the editor.

Return to “.Net / Unity3D API”

Who is online

Users browsing this forum: No registered users and 2 guests