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(, , w, h * / );
style.alignment = TextAnchor.UpperLeft;
style.fontSize = h * / ;
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( , , , ); // 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 = ; // How many decimal do you want to display private float accum = 0f; // FPS accumulated over the interval
private int frames = ; // 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, , ) ); //Update the color
color = (fps >= ) ? Color.green : ((fps > ) ? Color.red : Color.yellow); accum = 0.0F;
frames = ; 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(, startRect, DoMyWindow, "");
} void DoMyWindow(int windowID)
{
GUI.Label( new Rect(, , startRect.width, startRect.height), sFPS + " FPS", style );
if( allowDrag ) GUI.DragWindow(new Rect(, , 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 = ; // FPS accumulated over the interval
private int frames = ; // 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 < )
guiText.material.color = Color.yellow;
else
if(fps < )
guiText.material.color = Color.red;
else
guiText.material.color = Color.green;
// DebugConsole.Log(format,level);
timeleft = updateInterval;
accum = 0.0F;
frames = ;
}
}
}

显示游戏FPS帧率的几种计算方式的更多相关文章

  1. php 关于金额的几种计算方式

    php 关于金额的几种计算方式 平常开始开发过程中,多多少少都会遇到点关于金额的计算,比如设置返利.提现手续费.折扣啊等等诸如此类的比例,然后再计算出之后的实际的费用. 下面,以折扣为例,来实现这类计 ...

  2. mysql中TPS, QPS 的计算方式

    今天突然有个同事问题一个问题, mysqlTPS和QPS的计算公式是什么? 以前确实也没有关注过这个计算公式,所以查了下学习了下: 下面是参考内容.  在做db基准测试的时候,qps,tps 是衡量数 ...

  3. css控制div显示/隐藏方法及2种方法比较原码 - czf164的专栏 - 博客频道 - CSDN.NET

    body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...

  4. 三种计算c#程序运行时间的方法

    三种计算c#程序运行时间的方法 第一种: 利用 System.DateTime.Now // example1: System.DateTime.Now method DateTime dt1 = S ...

  5. Android中EditText显示明文与密文的两种方式

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 记录输入框显示.隐藏密码的简单布局以及实现方式. 效果图    代码分析 方式一 /**方式一:*/ private void sh ...

  6. 【转】STM32: 一种计算CPU使用率的方法及其实现原理

    1  前言出于性能方面的考虑,有的时候,我们希望知道CPU的使用率为多少,进而判断此CPU的负载情况和对于当前运行环境是否足够“胜任”.本文将介绍一种计算CPU占有率的方法以及其实现原理. 2  移植 ...

  7. Android中EditText显示明文与密码的两种方式

    效果图如下所述: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:and ...

  8. 两种计算Java对象大小的方法

    之前想研究一下unsafe类,碰巧在网上看到了这篇文章,觉得写得很好,就转载过来.原文出处是: http://blog.csdn.net/iter_zc/article/details/4182271 ...

  9. 2016/2/24 1,css有几种引入方式 2,div除了可以声明id来控制,还可以声明什么控制? 3,如何让2个div,并排显示。4,清除浮动 clear:left / right / both

    1,css有几种引入方式 使用HTML标签的STYLE属性 将STYLE属性直接加在单个的HTML元素标签上,控制HTML标签的表现样式.这种引入CSS的方式是分散灵活方便,但缺乏整体性和规划性,不利 ...

随机推荐

  1. android studio 汉化 美化 个性化 修改 安卓工作室 2.3.3 最新版

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 先看一下效果. 建议全屏看图,或者新标签看图.

  2. [ZROI 9.16模拟赛] Tutorial

    Link: 传送门 A: 套路题结果想了好久…… 排序二叉树的性质就是中序遍历单调递增 于是只考虑当前树的中序遍历的序列即可,与树的形态无关 将序列改成严格单调增想到最大化不变的数,但直接LIS求的是 ...

  3. pat 打印沙漏

    本题要求你写个程序把给定的符号打印成沙漏的形状.例如给定17个“*”,要求按下列格式打印 ***** *** * *** ***** 所谓“沙漏形状”,是指每行输出奇数个符号:各行符号中心对齐:相邻两 ...

  4. java下划线与驼峰命名互转

    方式一: 下划线与驼峰命名转换: public class Tool { private static Pattern linePattern = Pattern.compile("_(\\ ...

  5. 不思议迷宫:逆向后的放置play

    前言 前置准备 目标分析 逆向加密逻辑 定位sign与key 解密luac 反编译luajit的bytecode 开启上帝模式 前言     看了fatezero的<阴阳师:一个非酋的逆向旅程& ...

  6. bzoj 1051 强连通分量

    反建图,计算强连通分量,将每个分量看成一个点,缩点后的图是一个DAG,如果是一棵树,则根代表的连通分量的大小就是答案,否则答案为0. 收获: 图的东西如果不好解决,可以尝试缩点(有向图将每个强连通分量 ...

  7. bzoj 1626: [Usaco2007 Dec]Building Roads 修建道路 -- 最小生成树

    1626: [Usaco2007 Dec]Building Roads 修建道路 Time Limit: 5 Sec  Memory Limit: 64 MB Description Farmer J ...

  8. CSS的flex布局(转载)

    我们之前已经学过一些布局模型,比如说浮动,绝对定位等等,但是这些布局方式一是不够简洁,而是使用的范围确实是太窄了. flex模型拥有比较多的属性,来设置多样的布局方式,接下来我们就详细介绍各种属性对布 ...

  9. spring aop 理解

    aop简介 aop是spring 的两大特性之一,还有IOC.主要提供面向切面的编程思想,区分于面向对象编程. aop原理(动态代理+反射) 在一个方法体中,可能会存在很多其他的方法调用,我们可以把每 ...

  10. 利用站点ip引导提高站点权重的可行方案

    如题,利用站点每天高数额的ip訪问量来提高站点权重,首先在谈论这个话题之前,我举个样例.我们知道想要一个站点权重非常高,首先它站点本身的内容一定是有价值的,而且受大众欢迎的,人们会常常訪问这个站点来寻 ...