当前位置: 移动技术网 > IT编程>开发语言>c# > Unity OnGUI实时显示游戏FPS

Unity OnGUI实时显示游戏FPS

2020年03月09日  | 移动技术网IT编程  | 我要评论

fps是什么?

fps (每秒传输帧数(frames per second))【摘自百度百科】

fps是图像领域中的定义,是指画面每秒传输帧数,通俗来讲就是指动画或视频的画面数。fps是测量用于保存、显示动态视频的信息数量。每秒钟帧数愈多,所显示的动作就会越流畅。通常,要避免动作不流畅的最低是30。某些计算机视频格式,每秒只能提供15帧。

fps”也可以理解为我们常说的“刷新率(单位为hz)”,例如我们常在cs游戏里说的“fps值”。我们在装机选购显卡和显示器的时候,都会注意到“刷新率”。一般我们设置缺省刷新率都在75hz(即75帧/秒)以上。例如:75hz的刷新率刷也就是指屏幕一秒内只扫描75次,即75帧/秒。而当刷新率太低时我们肉眼都能感觉到屏幕的闪烁,不连贯,对图像显示效果和视觉感观产生不好的影响。

电影以每秒24张画面的速度播放,也就是一秒钟内在屏幕上连续投射出24张静止画面。有关动画播放速度的单位是fps,其中的f就是英文单词frame(画面、帧),p就是per(每),s就是second(秒)。用中文表达就是多少帧每秒,或每秒多少帧。电影是24fps,通常简称为24帧。

常见媒体的fps帧率:

电影:24fps
电视(pal):25fps
电视(ntsl):30fps
crt显示器:75hz以上
液晶显示器:一般为60hz

在游戏过程中一般人不觉得卡顿的fps频率大约是30hz,想要达到流畅等级则需要60hz

下面是实例代码

using system.collections;
using system.collections.generic;
using unityengine;

/// <summary>
/// fps 显示于ongui 
/// </summary>
public class fpsonguitext : monobehaviour {

 float updateinterval = 1.0f;   //当前时间间隔
 private float accumulated = 0.0f;  //在此期间累积 
 private float frames = 0;    //在间隔内绘制的帧 
 private float timeremaining;   //当前间隔的剩余时间
 private float fps = 15.0f;    //当前帧 current fps
 private float lastsample;

 void start()
 {
  dontdestroyonload(this.gameobject); //不销毁此游戏对象,在哪个场景都可以显示,,不需要则注释
  timeremaining = updateinterval;
  lastsample = time.realtimesincestartup; //实时自启动
 }
 
 void update()
 {
  ++frames;
  float newsample = time.realtimesincestartup;
  float deltatime = newsample - lastsample;
  lastsample = newsample;
  timeremaining -= deltatime;
  accumulated += 1.0f / deltatime;
 
  if (timeremaining <= 0.0f)
  {
   fps = accumulated / frames;
   timeremaining = updateinterval;
   accumulated = 0.0f;
   frames = 0;
  }
 }

 void ongui()
 {
  guistyle style = new guistyle
  {
   border = new rectoffset(10, 10, 10, 10),
   fontsize = 50,
   fontstyle = fontstyle.boldanditalic,   
  };
  //自定义宽度 ,高度大小 颜色,style
  gui.label(new rect(screen.width/2-50, screen.height - 100, 200, 200), "<color=#00ff00><size=30>" + "fps:" + fps.tostring("f2")+ "</size></color>", style);
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网