Unity3d:UI显示FPS

本文介绍了一个Unity脚本,用于实时监测并显示游戏运行时的帧率(FPS)。通过设置目标帧率和统计时间间隔,该脚本能够计算并更新实际帧率,适用于游戏性能调优和实时反馈。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 Application.targetFrameRate:重点=-1是默认显示

 

  1. public class FPSTest : MonoBehaviour {
  2.     public float timeCount = 1f; //更新帧显示帧数的时间    
  3.     public  int   defaultFPS = 30; //默认帧频     
  4.     private float time = 0;    
  5.     private int   frameCount=0;//每次统计时间之间运行多少针    
  6.     private float currentlFPS;//实际帧数     
  7.     public UILabel label;
  8.     void Awake(){        
  9.         
  10.         
  11.     }    
  12.     void Start () {
  13.         Application.targetFrameRate = defaultFPS;         //设置帧率 
  14.         
  15.     }        
  16.    
  17.     void Update () {        //实时显示帧率
  18.         frameCount++;
  19.         time += Time.deltaTime;
  20.         if(time >= timeCount){            
  21.             currentlFPS = frameCount/time;
  22.             
  23.             label.text = ((int)currentlFPS).ToString();//NGUI label显示 去除小数点
  24.               
  25.             frameCount = 0;
  26.             time = 0;
  27.         }
  28.     }     
     
  29.  
  30. }
     
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值