您的位置:首页 > 编程语言

FPS的Log代码

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

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

FPS.cs

using UnityEngine;
using System.Collections;

public class FPS : MonoBehaviour {

public float f_updatInterval = 0.3f;
private float f_LastInterval;
private int i_Frames =0;
private float f_Fps;

void Start () {
f_LastInterval = Time.realtimeSinceStartup;
i_Frames = 0;
}

// Update is called once per frame
void Update () {
++i_Frames;
if (Time.realtimeSinceStartup > f_LastInterval + f_updatInterval)
{
f_Fps = i_Frames / (Time.realtimeSinceStartup - f_LastInterval);
i_Frames = 0;
f_LastInterval = Time.realtimeSinceStartup;
}
}

void OnGUI()
{
GUI.Label (new Rect(0,100,200,200),"FPS"+ f_Fps.ToString("f2"));
}
}


ShowFPS.cs

using UnityEngine;
using System.Collections;

public class ShowFPS : MonoBehaviour
{
float deltaTime = 0.0f;

void Update()
{
deltaTime += (Time.deltaTime - deltaTime) * 0.1f;
}

void OnGUI()
{
int w = Screen.width;
int h = Screen.height;

GUIStyle style = new GUIStyle ();
Rect rect = new Rect (0, 0, 100, 50);
style.alignment = TextAnchor.UpperLeft;
style.normal.textColor = new Color (0.0f, 0.0f, 0.5f,1.0f);
float msec = deltaTime * 1000.0f;
float fps = 1.0f / deltaTime;
string text = string.Format ("{0:0.0}ms({1:0})fps",msec,fps);
GUI.Label (rect,text,style);
}
}


每秒刷新60帧,手机帧频可以到理想状态。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: