1. FPSDisplay.cs
  2.  
  3. using UnityEngine;
  4. using System.Collections;
  5.  
  6. public class FPSDisplay : MonoBehaviour
  7. {
  8. float deltaTime = 0.0f;
  9.  
  10. void Update()
  11. {
  12. deltaTime += (Time.deltaTime - deltaTime) * 0.1f;
  13. }
  14.  
  15. void OnGUI()
  16. {
  17. int w = Screen.width, h = Screen.height;
  18.  
  19. GUIStyle style = new GUIStyle();
  20.  
  21. Rect rect = new Rect(, , w, h * / );
  22. style.alignment = TextAnchor.UpperLeft;
  23. style.fontSize = h * / ;
  24. style.normal.textColor = new Color (0.0f, 0.0f, 0.5f, 1.0f);
  25. float msec = deltaTime * 1000.0f;
  26. float fps = 1.0f / deltaTime;
  27. string text = string.Format("{0:0.0} ms ({1:0.} fps)", msec, fps);
  28. GUI.Label(rect, text, style);
  29. }
  30. }
  31.  
  32. FPSCounter.cs
  33.  
  34. /* **************************************************************************
  35. * FPS COUNTER
  36. * **************************************************************************
  37. * Written by: Annop "Nargus" Prapasapong
  38. * Created: 7 June 2012
  39. * *************************************************************************/
  40.  
  41. using UnityEngine;
  42. using System.Collections;
  43.  
  44. /* **************************************************************************
  45. * CLASS: FPS COUNTER
  46. * *************************************************************************/
  47. [RequireComponent(typeof(GUIText))]
  48. public class FPSCounter : MonoBehaviour {
  49. /* Public Variables */
  50. public float frequency = 0.5f;
  51.  
  52. /* **********************************************************************
  53. * PROPERTIES
  54. * *********************************************************************/
  55. public int FramesPerSec { get; protected set; }
  56.  
  57. /* **********************************************************************
  58. * EVENT HANDLERS
  59. * *********************************************************************/
  60. /*
  61. * EVENT: Start
  62. */
  63. private void Start() {
  64. StartCoroutine(FPS());
  65. }
  66.  
  67. /*
  68. * EVENT: FPS
  69. */
  70. private IEnumerator FPS() {
  71. for(;;){
  72. // Capture frame-per-second
  73. int lastFrameCount = Time.frameCount;
  74. float lastTime = Time.realtimeSinceStartup;
  75. yield return new WaitForSeconds(frequency);
  76. float timeSpan = Time.realtimeSinceStartup - lastTime;
  77. int frameCount = Time.frameCount - lastFrameCount;
  78.  
  79. // Display it
  80. FramesPerSec = Mathf.RoundToInt(frameCount / timeSpan);
  81. gameObject.guiText.text = FramesPerSec.ToString() + " fps";
  82. }
  83. }
  84. }
  85.  
  86. HUDFPS.cs
  87.  
  88. sing UnityEngine;
  89. using System.Collections;
  90.  
  91. [AddComponentMenu( "Utilities/HUDFPS")]
  92. public class HUDFPS : MonoBehaviour
  93. {
  94. // Attach this to any object to make a frames/second indicator.
  95. //
  96. // It calculates frames/second over each updateInterval,
  97. // so the display does not keep changing wildly.
  98. //
  99. // It is also fairly accurate at very low FPS counts (<10).
  100. // We do this not by simply counting frames per interval, but
  101. // by accumulating FPS for each frame. This way we end up with
  102. // corstartRect overall FPS even if the interval renders something like
  103. // 5.5 frames.
  104.  
  105. public Rect startRect = new Rect( , , , ); // The rect the window is initially displayed at.
  106. public bool updateColor = true; // Do you want the color to change if the FPS gets low
  107. public bool allowDrag = true; // Do you want to allow the dragging of the FPS window
  108. public float frequency = 0.5F; // The update frequency of the fps
  109. public int nbDecimal = ; // How many decimal do you want to display
  110.  
  111. private float accum = 0f; // FPS accumulated over the interval
  112. private int frames = ; // Frames drawn over the interval
  113. private Color color = Color.white; // The color of the GUI, depending of the FPS ( R < 10, Y < 30, G >= 30 )
  114. private string sFPS = ""; // The fps formatted into a string.
  115. private GUIStyle style; // The style the text will be displayed at, based en defaultSkin.label.
  116.  
  117. void Start()
  118. {
  119. StartCoroutine( FPS() );
  120. }
  121.  
  122. void Update()
  123. {
  124. accum += Time.timeScale/ Time.deltaTime;
  125. ++frames;
  126. }
  127.  
  128. IEnumerator FPS()
  129. {
  130. // Infinite loop executed every "frenquency" secondes.
  131. while( true )
  132. {
  133. // Update the FPS
  134. float fps = accum/frames;
  135. sFPS = fps.ToString( "f" + Mathf.Clamp( nbDecimal, , ) );
  136.  
  137. //Update the color
  138. color = (fps >= ) ? Color.green : ((fps > ) ? Color.red : Color.yellow);
  139.  
  140. accum = 0.0F;
  141. frames = ;
  142.  
  143. yield return new WaitForSeconds( frequency );
  144. }
  145. }
  146.  
  147. void OnGUI()
  148. {
  149. // Copy the default label skin, change the color and the alignement
  150. if( style == null ){
  151. style = new GUIStyle( GUI.skin.label );
  152. style.normal.textColor = Color.white;
  153. style.alignment = TextAnchor.MiddleCenter;
  154. }
  155.  
  156. GUI.color = updateColor ? color : Color.white;
  157. startRect = GUI.Window(, startRect, DoMyWindow, "");
  158. }
  159.  
  160. void DoMyWindow(int windowID)
  161. {
  162. GUI.Label( new Rect(, , startRect.width, startRect.height), sFPS + " FPS", style );
  163. if( allowDrag ) GUI.DragWindow(new Rect(, , Screen.width, Screen.height));
  164. }
  165. }
  166.  
  167. HUDFPS.cs
  168.  
  169. using UnityEngine;
  170. using System.Collections;
  171.  
  172. public class HUDFPS : MonoBehaviour
  173. {
  174.  
  175. // Attach this to a GUIText to make a frames/second indicator.
  176. //
  177. // It calculates frames/second over each updateInterval,
  178. // so the display does not keep changing wildly.
  179. //
  180. // It is also fairly accurate at very low FPS counts (<10).
  181. // We do this not by simply counting frames per interval, but
  182. // by accumulating FPS for each frame. This way we end up with
  183. // correct overall FPS even if the interval renders something like
  184. // 5.5 frames.
  185.  
  186. public float updateInterval = 0.5F;
  187.  
  188. private float accum = ; // FPS accumulated over the interval
  189. private int frames = ; // Frames drawn over the interval
  190. private float timeleft; // Left time for current interval
  191.  
  192. void Start()
  193. {
  194. if( !guiText )
  195. {
  196. Debug.Log("UtilityFramesPerSecond needs a GUIText component!");
  197. enabled = false;
  198. return;
  199. }
  200. timeleft = updateInterval;
  201. }
  202.  
  203. void Update()
  204. {
  205. timeleft -= Time.deltaTime;
  206. accum += Time.timeScale/Time.deltaTime;
  207. ++frames;
  208.  
  209. // Interval ended - update GUI text and start new interval
  210. if( timeleft <= 0.0 )
  211. {
  212. // display two fractional digits (f2 format)
  213. float fps = accum/frames;
  214. string format = System.String.Format("{0:F2} FPS",fps);
  215. guiText.text = format;
  216.  
  217. if(fps < )
  218. guiText.material.color = Color.yellow;
  219. else
  220. if(fps < )
  221. guiText.material.color = Color.red;
  222. else
  223. guiText.material.color = Color.green;
  224. // DebugConsole.Log(format,level);
  225. timeleft = updateInterval;
  226. accum = 0.0F;
  227. frames = ;
  228. }
  229. }
  230. }

显示游戏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. Java文件签名与验证

    数字签名与验证只需要用户输入三个参数: Ø         原文件 Ø         签名信息文件 Ø         用户名 签名过程: 1.         首先从用户名对应的用户注册文件中读取 ...

  2. Topcoder Srm 726 Div1 Hard

    Topcoder Srm 726 Div1 Hard 解题思路: 问题可以看做一个二分图,左边一个点向右边一段区间连边,匹配了左边一个点就能获得对应的权值,最大化所得到的权值的和. 然后可以证明一个结 ...

  3. Stirling数,Bell数,Catalan数,Bernoulli数

    组合数学的实质还是DP,但是从通式角度处理的话有利于FFT等的实现. 首先推荐$Candy?$的球划分问题集合: http://www.cnblogs.com/candy99/p/6400735.ht ...

  4. 网络流小结+[jzyzoj p1320] patrol

    一个不能更清楚的网络流介绍 ↑虽然不是我写的但是观摩一下总是没问题的嗯   看到晗神学的是神奇的ek算法. 但是看起来还是Ford-Fulkerson比较简单..所以我就学了这个...嗯其他的先看看. ...

  5. 【贪心】BZOJ3668-[NOI2014]起床困难综合症

    [题目大意] 给定n次操作(与,或,异或),在0~m中选择一个数,使这个数经过n次操作后得到的值最大. [思路] 水题orz 枚举这个数每一位的取值是0还是1,然后根据它经过n次操作后的结果判断: ( ...

  6. [AHOI2009]同类分布

    题目大意: 问在区间[l,r]内的正整数中,有多少数能被其个位数字之和整除. 思路: 数位DP. 极端情况下,每一位都是9,所以各位数字之和不超过9*18.(为了方便这里用了9*19) f[i][j] ...

  7. [转]怎么把一个textview的背景图片设置成圆角的?

        在drawable文件夹下新建一个文件设置背景样式代码:在drawable文件夹下面新建text_view_border.xml<?xml version="1.0" ...

  8. ReactNative-地图导航-iOS

    需求描述 项目中,要求接入导航功能,包括“百度map.高德map”. 方案分析 原生开发角度分析 从原生开发的角度分析的话,常规的思路可能是 分别取百度.高德官网,下载对应的SDK然后集成到本地: 创 ...

  9. 《C# to IL》第三章 选择和循环

    在IL中,标号(label)是一个末尾带有冒号(即:)的名称.它使我们能够从代码的一部分无条件地跳转到另一部分.我们经常在由反编译器生成的IL代码中看到这个标号.例如: IL_0000: ldstr  ...

  10. Spring JdbcTemplate+JdbcDaoSupport实例

    在Spring JDBC开发中,可以使用 JdbcTemplate 和 JdbcDaoSupport 类来简化整个数据库的操作过程. 在本教程中,我们将重复上一篇文章Spring+JDBC例子,看之前 ...