找回密码
 立即注册
楼主: Unity联盟

[GUI] Unity3D获取FPS帧率代码 测试帧率

[复制链接]
发表于 2013-3-28 16:53 | 显示全部楼层 |阅读模式
JavaScript - FramesPerSecond.js

  1. // Attach this to a GUIText to make a frames/second indicator.
  2. //
  3. // It calculates frames/second over each updateInterval,
  4. // so the display does not keep changing wildly.
  5. //
  6. // It is also fairly accurate at very low FPS counts (<10).
  7. // We do this not by simply counting frames per interval, but
  8. // by accumulating FPS for each frame. This way we end up with
  9. // correct overall FPS even if the interval renders something like
  10. // 5.5 frames.

  11. var updateInterval = 0.5;

  12. private var accum = 0.0; // FPS accumulated over the interval
  13. private var frames = 0; // Frames drawn over the interval
  14. private var timeleft : float; // Left time for current interval

  15. function Start()
  16. {
  17.     if( !guiText )
  18.     {
  19.         print ("FramesPerSecond needs a GUIText component!");
  20.         enabled = false;
  21.         return;
  22.     }
  23.     timeleft = updateInterval;  
  24. }

  25. function Update()
  26. {
  27.     timeleft -= Time.deltaTime;
  28.     accum += Time.timeScale/Time.deltaTime;
  29.     ++frames;

  30.     // Interval ended - update GUI text and start new interval
  31.     if( timeleft <= 0.0 )
  32.     {
  33.         // display two fractional digits (f2 format)
  34.         guiText.text = "" + (accum/frames).ToString("f2");
  35.         timeleft = updateInterval;
  36.         accum = 0.0;
  37.         frames = 0;
  38.     }
  39. }
复制代码
CSharp HUDFPS.cs

  1. A C# implementation of the above converted by Opless. The main difference is the colour change when FPS dips too low.

  2. using UnityEngine;
  3. using System.Collections;

  4. public class HUDFPS : MonoBehaviour
  5. {

  6. // Attach this to a GUIText to make a frames/second indicator.
  7. //
  8. // It calculates frames/second over each updateInterval,
  9. // so the display does not keep changing wildly.
  10. //
  11. // It is also fairly accurate at very low FPS counts (<10).
  12. // We do this not by simply counting frames per interval, but
  13. // by accumulating FPS for each frame. This way we end up with
  14. // correct overall FPS even if the interval renders something like
  15. // 5.5 frames.

  16. public  float updateInterval = 0.5F;

  17. private float accum   = 0; // FPS accumulated over the interval
  18. private int   frames  = 0; // Frames drawn over the interval
  19. private float timeleft; // Left time for current interval

  20. void Start()
  21. {
  22.     if( !guiText )
  23.     {
  24.         Debug.Log("UtilityFramesPerSecond needs a GUIText component!");
  25.         enabled = false;
  26.         return;
  27.     }
  28.     timeleft = updateInterval;  
  29. }

  30. void Update()
  31. {
  32.     timeleft -= Time.deltaTime;
  33.     accum += Time.timeScale/Time.deltaTime;
  34.     ++frames;

  35.     // Interval ended - update GUI text and start new interval
  36.     if( timeleft <= 0.0 )
  37.     {
  38.         // display two fractional digits (f2 format)
  39.     float fps = accum/frames;
  40.     string format = System.String.Format("{0:F2} FPS",fps);
  41.     guiText.text = format;

  42.     if(fps < 30)
  43.         guiText.material.color = Color.yellow;
  44.     else
  45.         if(fps < 10)
  46.             guiText.material.color = Color.red;
  47.         else
  48.             guiText.material.color = Color.green;
  49.     //  DebugConsole.Log(format,level);
  50.         timeleft = updateInterval;
  51.         accum = 0.0F;
  52.         frames = 0;
  53.     }
  54. }
  55. }
复制代码
Boo FPS_Display.boo

  1. A Boo implementation of the above converted by Philbywhizz.

  2. import UnityEngine

  3. class FPS_Display (MonoBehaviour):

  4.     public updateInterval as single = 0.5

  5.     private accum as single = 0 // FPS accumulated over the interval
  6.     private frames as int = 0 // Frames drawn over the interval
  7.     private timeleft as single // Left time for current interval

  8.     def Start ():
  9.         if(not guiText):
  10.             Debug.Log("FPS Display needs a GUIText component!")
  11.             enabled = false
  12.         timeleft = updateInterval

  13.     def Update ():
  14.         timeleft -= Time.deltaTime
  15.         accum += Time.timeScale/Time.deltaTime
  16.         ++frames

  17.         if (timeleft <= 0.0):
  18.             fps = accum/frames
  19.             format = System.String.Format("FPS: {0:F2}", fps);
  20.             guiText.text = format

  21.             if(fps < 30):
  22.                 guiText.material.color = Color.yellow
  23.             elif(fps < 10):
  24.                 guiText.material.color = Color.red
  25.             else:
  26.                 guiText.material.color = Color.green

  27.             timeleft = updateInterval
  28.             accum = 0.0
  29.             frames = 0
复制代码

相关帖子

发表于 2013-3-29 12:34 | 显示全部楼层
就是不知道怎么使用
发表于 2014-4-26 10:20 | 显示全部楼层
好东西 学习 学习
发表于 2017-2-12 10:03 | 显示全部楼层
很不错
发表于 2017-2-12 10:10 | 显示全部楼层
顶顶多好
发表于 2017-2-12 09:35 | 显示全部楼层
真心顶
发表于 2017-2-12 09:45 | 显示全部楼层
说的非常好
发表于 2017-2-12 09:37 | 显示全部楼层
LZ真是人才
发表于 2017-2-25 12:09 | 显示全部楼层
楼主是超人
发表于 2017-2-25 12:14 | 显示全部楼层
顶顶多好
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-11-22 19:47 , Processed in 0.072920 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表