我的FPS计数器为什么不正确? [英] My FPS Counter is in-accurate any ideas why?

查看:161
本文介绍了我的FPS计数器为什么不正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我开始游戏时,它会保持在95-101左右的快速变化,介于所有这些数字之间.但是当我打开统计栏时,我得到的是前200的最低300

When I start up my game it stays around 95-101 rapidly changing, in between all of those numbers.. but when I open up the stats bar I'm getting upper 200's low 300's

所以想知道为什么这对于c#还是很新的,所以对我来说很容易.继承人的代码 像往常一样先感谢^ _ ^.

so wondering why that is still new to c# so be easy on me lol. heres the code thanks in advance as always ^_^.

float deltaTime = 0.0f;

void Update() 
{
    deltaTime += (Time.deltaTime - deltaTime) * 0.1f;
}

void OnGUI()
{
    int w = Screen.width, h = Screen.height;

    GUIStyle style = new GUIStyle ();

    Rect rect = new Rect (0, 0, w, h * 2 / 100);
    style.alignment = TextAnchor.UpperRight;
    style.fontSize = h * 2 / 100;
    style.normal.textColor = new Color (255.0f, 255.0f, 255.0f, 1.0f);
    float msec = deltaTime * 1000.0f;
    float fps = 1f / deltaTime;
    string text = string.Format ("({1:0.} fps)", msec, fps);
    GUI.Label (rect, text, style);
    }
}

推荐答案

要显示有意义的FPS速率,您需要测量在恒定时间内渲染了多少帧 一秒钟.然后只有在该时间段之后,才在屏幕上显示计算出的值.这将提供每秒平均帧,而不是每秒瞬时帧,后者在大多数情况下并不是特别有用,因为它会导致值波动很大

In order to display a meaningful FPS rate you need to measure how many frames were rendered over a constant period of time, for example one second. Then only after that period do you display the calculated value on screen. This will provide for an average frames per second as opposed to an instantaneous frames per second, the latter of which is not particularly useful in most cases as it leads to widely fluctuating values.

首先定义一些字段:

DateTime _lastTime; // marks the beginning the measurement began
int _framesRendered; // an increasing count
int _fps; // the FPS calculated from the last measurement

然后在渲染方法中递增_framesRendered.您还要检查自周期开始以来是否已过去一秒钟:

Then in your render method you increment the _framesRendered. You also check to see if one second has elapsed since the start of the period:

void Update()
{
    _framesRendered++;

    if ((DateTime.Now - _lastTime).TotalSeconds >= 1)
    {
        // one second has elapsed 

        _fps = _framesRendered;                     
        _framesRendered = 0;            
        _lastTime = DateTime.Now;
    }

    // draw FPS on screen here using current value of _fps          
}

跨技术

应该指出的是,以上代码并未对Unity进行特殊使用,同时仍然相当准确,并且与许多框架和API(例如DirectX)兼容; OpenGL; XNA; WPF甚至WinForms.

Cross-technology

It should be pointed out that the above code makes no particular use of Unity whilst still being reasonably accurate and is compatible with many frameworks and APIs such as DirectX; OpenGL; XNA; WPF or even WinForms.

当我开始游戏时,它会保持在95-101左右的快速变化,介于所有这些数字之间.但是当我打开统计栏时,我得到的是前200的最低300

When I start up my game it stays around 95-101 rapidly changing, in between all of those numbers.. but when I open up the stats bar I'm getting upper 200's low 300's

ASUS VG248QE为1ms,它可以执行的最大为144Hz,因此不太可能出现高200到低300"的情况.在非GSYNC监视器上关闭VSYNC时,FPS毫无意义.您的VSYNC是否已打开?

The ASUS VG248QE is 1ms and the max it can do is 144Hz so it is unlikely you are getting "upper 200's low 300's". FPS is meaningless when VSYNC is turned off on a non-GSYNC monitor. Is your VSYNC turned on?

这篇关于我的FPS计数器为什么不正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆