首页 > 其他 > 详细

显示游戏FPS帧率的几种计算方式

时间:2014-05-05 22:04:38      阅读:822      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
FPSDisplay.cs

using UnityEngine;
using System.Collections;
 
public class FPSDisplay : MonoBehaviour
{
    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.UpperLeft;
        style.fontSize = h * 2 / 100;
        style.normal.textColor = new Color (0.0f, 0.0f, 0.5f, 1.0f);
        float msec = deltaTime * 1000.0f;
        float fps = 1.0f / deltaTime;
        string text = string.Format("{0:0.0} ms ({1:0.} fps)", msec, fps);
        GUI.Label(rect, text, style);
    }
}


FPSCounter.cs

/* **************************************************************************
 * FPS COUNTER
 * **************************************************************************
 * Written by: Annop "Nargus" Prapasapong
 * Created: 7 June 2012
 * *************************************************************************/
 
using UnityEngine;
using System.Collections;
 
/* **************************************************************************
 * CLASS: FPS COUNTER
 * *************************************************************************/ 
[RequireComponent(typeof(GUIText))]
public class FPSCounter : MonoBehaviour {
    /* Public Variables */
    public float frequency = 0.5f;
 
    /* **********************************************************************
     * PROPERTIES
     * *********************************************************************/
    public int FramesPerSec { get; protected set; }
 
    /* **********************************************************************
     * EVENT HANDLERS
     * *********************************************************************/
    /*
     * EVENT: Start
     */
    private void Start() {
        StartCoroutine(FPS());
    }
 
    /*
     * EVENT: FPS
     */
    private IEnumerator FPS() {
        for(;;){
            // Capture frame-per-second
            int lastFrameCount = Time.frameCount;
            float lastTime = Time.realtimeSinceStartup;
            yield return new WaitForSeconds(frequency);
            float timeSpan = Time.realtimeSinceStartup - lastTime;
            int frameCount = Time.frameCount - lastFrameCount;
 
            // Display it
            FramesPerSec = Mathf.RoundToInt(frameCount / timeSpan);
            gameObject.guiText.text = FramesPerSec.ToString() + " fps";
        }
    }
}


HUDFPS.cs

sing UnityEngine;
using System.Collections;
 
[AddComponentMenu( "Utilities/HUDFPS")]
public class HUDFPS : MonoBehaviour
{
    // Attach this to any object to make a frames/second indicator.
    //
    // It calculates frames/second over each updateInterval,
    // so the display does not keep changing wildly.
    //
    // It is also fairly accurate at very low FPS counts (<10).
    // We do this not by simply counting frames per interval, but
    // by accumulating FPS for each frame. This way we end up with
    // corstartRect overall FPS even if the interval renders something like
    // 5.5 frames.
 
    public Rect startRect = new Rect( 10, 10, 75, 50 ); // The rect the window is initially displayed at.
    public bool updateColor = true; // Do you want the color to change if the FPS gets low
    public bool allowDrag = true; // Do you want to allow the dragging of the FPS window
    public  float frequency = 0.5F; // The update frequency of the fps
    public int nbDecimal = 1; // How many decimal do you want to display
 
    private float accum   = 0f; // FPS accumulated over the interval
    private int   frames  = 0; // Frames drawn over the interval
    private Color color = Color.white; // The color of the GUI, depending of the FPS ( R < 10, Y < 30, G >= 30 )
    private string sFPS = ""; // The fps formatted into a string.
    private GUIStyle style; // The style the text will be displayed at, based en defaultSkin.label.
 
    void Start()
    {
        StartCoroutine( FPS() );
    }
 
    void Update()
    {
        accum += Time.timeScale/ Time.deltaTime;
        ++frames;
    }
 
    IEnumerator FPS()
    {
        // Infinite loop executed every "frenquency" secondes.
        while( true )
        {
            // Update the FPS
            float fps = accum/frames;
            sFPS = fps.ToString( "f" + Mathf.Clamp( nbDecimal, 0, 10 ) );
 
            //Update the color
            color = (fps >= 30) ? Color.green : ((fps > 10) ? Color.red : Color.yellow);
 
            accum = 0.0F;
            frames = 0;
 
            yield return new WaitForSeconds( frequency );
        }
    }
 
    void OnGUI()
    {
        // Copy the default label skin, change the color and the alignement
        if( style == null ){
            style = new GUIStyle( GUI.skin.label );
            style.normal.textColor = Color.white;
            style.alignment = TextAnchor.MiddleCenter;
        }
 
        GUI.color = updateColor ? color : Color.white;
        startRect = GUI.Window(0, startRect, DoMyWindow, "");
    }
 
    void DoMyWindow(int windowID)
    {
        GUI.Label( new Rect(0, 0, startRect.width, startRect.height), sFPS + " FPS", style );
        if( allowDrag ) GUI.DragWindow(new Rect(0, 0, Screen.width, Screen.height));
    }
}

HUDFPS.cs

using UnityEngine;
using System.Collections;
 
public class HUDFPS : MonoBehaviour 
{
 
// Attach this to a GUIText to make a frames/second indicator.
//
// It calculates frames/second over each updateInterval,
// so the display does not keep changing wildly.
//
// It is also fairly accurate at very low FPS counts (<10).
// We do this not by simply counting frames per interval, but
// by accumulating FPS for each frame. This way we end up with
// correct overall FPS even if the interval renders something like
// 5.5 frames.
 
public  float updateInterval = 0.5F;
 
private float accum   = 0; // FPS accumulated over the interval
private int   frames  = 0; // Frames drawn over the interval
private float timeleft; // Left time for current interval
 
void Start()
{
    if( !guiText )
    {
        Debug.Log("UtilityFramesPerSecond needs a GUIText component!");
        enabled = false;
        return;
    }
    timeleft = updateInterval;  
}
 
void Update()
{
    timeleft -= Time.deltaTime;
    accum += Time.timeScale/Time.deltaTime;
    ++frames;
 
    // Interval ended - update GUI text and start new interval
    if( timeleft <= 0.0 )
    {
        // display two fractional digits (f2 format)
    float fps = accum/frames;
    string format = System.String.Format("{0:F2} FPS",fps);
    guiText.text = format;
 
    if(fps < 30)
        guiText.material.color = Color.yellow;
    else 
        if(fps < 10)
            guiText.material.color = Color.red;
        else
            guiText.material.color = Color.green;
    //    DebugConsole.Log(format,level);
        timeleft = updateInterval;
        accum = 0.0F;
        frames = 0;
    }
}
}
bubuko.com,布布扣

 

显示游戏FPS帧率的几种计算方式,布布扣,bubuko.com

显示游戏FPS帧率的几种计算方式

原文:http://www.cnblogs.com/123ing/p/3704876.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!