找回密码
 立即注册
查看: 8898|回复: 92

[脚本] 如何获取Unity游戏时的FPS?

[复制链接]
发表于 2012-6-7 21:43 | 显示全部楼层 |阅读模式
悬赏10U币未解决
看了很多  太复杂  求高手 给个代码 O(∩_∩)O 我懒 谢谢{:soso__2891809371178566359_2:}

发表于 2012-6-8 15:15 | 显示全部楼层
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.    
  31.     // Interval ended - update GUI text and start new interval
  32.     if( timeleft <= 0.0 )
  33.     {
  34.         // display two fractional digits (f2 format)
  35.         guiText.text = "" + (accum/frames).ToString("f2");
  36.         timeleft = updateInterval;
  37.         accum = 0.0;
  38.         frames = 0;
  39.     }
  40. }
复制代码
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.    
  36.     // Interval ended - update GUI text and start new interval
  37.     if( timeleft <= 0.0 )
  38.     {
  39.         // display two fractional digits (f2 format)
  40.     float fps = accum/frames;
  41.     string format = System.String.Format("{0:F2} FPS",fps);
  42.     guiText.text = format;

  43.     if(fps < 30)
  44.         guiText.material.color = Color.yellow;
  45.     else
  46.         if(fps < 10)
  47.             guiText.material.color = Color.red;
  48.         else
  49.             guiText.material.color = Color.green;
  50.     //  DebugConsole.Log(format,level);
  51.         timeleft = updateInterval;
  52.         accum = 0.0F;
  53.         frames = 0;
  54.     }
  55. }
  56. }
复制代码
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.    
  6.     private accum as single = 0 // FPS accumulated over the interval
  7.     private frames as int = 0 // Frames drawn over the interval
  8.     private timeleft as single // Left time for current interval
  9.    
  10.     def Start ():
  11.         if(not guiText):
  12.             Debug.Log("FPS Display needs a GUIText component!")
  13.             enabled = false
  14.         timeleft = updateInterval
  15.    
  16.     def Update ():
  17.         timeleft -= Time.deltaTime
  18.         accum += Time.timeScale/Time.deltaTime
  19.         ++frames
  20.         
  21.         if (timeleft <= 0.0):
  22.             fps = accum/frames
  23.             format = System.String.Format("FPS: {0:F2}", fps);
  24.             guiText.text = format
  25.             
  26.             if(fps < 30):
  27.                 guiText.material.color = Color.yellow
  28.             elif(fps < 10):
  29.                 guiText.material.color = Color.red
  30.             else:
  31.                 guiText.material.color = Color.green
  32.             
  33.             timeleft = updateInterval
  34.             accum = 0.0
  35.             frames = 0
复制代码
直接贴到一个GUIText 上 {:soso__3529053643505418858_2:}
回复

使用道具 举报

发表于 2012-8-13 10:44 | 显示全部楼层
这个对我真的有帮助!!!
回复

使用道具 举报

发表于 2017-3-16 16:49 | 显示全部楼层
很不错
回复

使用道具 举报

发表于 2017-3-16 17:09 | 显示全部楼层
好帖就是要顶
回复

使用道具 举报

发表于 2017-3-16 17:27 | 显示全部楼层
很好哦
回复

使用道具 举报

发表于 2017-3-16 17:41 | 显示全部楼层
真心顶
回复

使用道具 举报

发表于 2017-3-16 17:47 | 显示全部楼层
不错不错
回复

使用道具 举报

发表于 2017-3-16 21:01 | 显示全部楼层
不错不错
回复

使用道具 举报

发表于 2017-3-16 21:29 | 显示全部楼层
楼主是超人
回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-5-1 03:37 , Processed in 0.094448 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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