这篇文章主要介绍了Unity3D基于OnGUI实时显示FPS,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
帧率(Frame rate)是用于测量显示帧数的量度。所谓的测量单位为每秒显示帧数(Frames per Second,简称:FPS)或“赫兹”(Hz)。此词多用于影视制作和电子游戏。由于人类眼睛的特殊生理结构,如果所看画面之帧率高于16的时候,就会认为是连贯的,此现象称之为视觉暂留。
每秒的帧数(fps)或者说帧率表示图形处理器处理场时每秒钟能够更新的次数。高的帧率可以得到更流畅、更逼真的动画。一般来说30fps就是可以接受的,但是将性能提升至60fps则可以明显提升交互感和逼真感,但是一般来说超过75fps一般就不容易察觉到有明显的流畅度提升了。如果帧率超过屏幕刷新率只会浪费图形处理的能力,因为监视器不能以这么快的速度更新,这样超过刷新率的帧率就浪费掉了。
以下是在Unity3D中显示fps的代码。
using UnityEngine;
using System.Collections;
[AddComponentMenu( "Utilities/HUDFPS")]
public class FPSCounter : MonoBehaviour
{
//fps 显示的初始位置和大小
public Rect startRect=new Rect(512, 10f, 75f, 50f );
//fps 过低时是否改变UI颜色
public bool updateColor = true;
//fps UI 是否允许拖动
public bool allowDrag = true;
//fps 更新的频率
public float frequency = 0.5F;
//fps 显示的精度
public int nbDecimal = 1;
//一定时间内的fps数量
private float accum = 0f;
//fps计算的时间
private int frames = 0;
//GUI 依赖fps的颜色 fps<10 红色 fps<30 黄色 fps>=30 绿色
private Color color = Color.white;
//fps
private string sFPS = "";
//GUI 的样式
private GUIStyle style;
void Start()
{
StartCoroutine(FPS());
}
void Update()
{
accum += Time.timeScale/ Time.deltaTime;
++frames;
}
IEnumerator FPS()
{
while( true )
{
//更新fps
float fps = accum/frames;
sFPS = fps.ToString( "f" + Mathf.Clamp( nbDecimal, 0, 10 ) );
//更新颜色
color = (fps >= 30) ? Color.green : ((fps > 10) ? Color.yellow : Color.red);
accum = 0.0F;
frames = 0;
yield return new WaitForSeconds( frequency );
}
}
void OnGUI()
{
if( style == null ){
style = new GUIStyle( GUI.skin.label );
style.normal.textColor = Color.white;
style.alignment = TextAnchor.MiddleCenter;
}
GUI.color = updateColor ? color : Color.white;
startRect = GUI.Window(0, startRect, DoMyWindow, "");
}
void DoMyWindow(int windowID)
{
GUI.Label( new Rect(0, 0, startRect.width, startRect.height), sFPS + " FPS", style );
if( allowDrag ) GUI.DragWindow(new Rect(0, 0, Screen.width, Screen.height));
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程学习网。
沃梦达教程
本文标题为:Unity3D基于OnGUI实时显示FPS
猜你喜欢
- Oracle中for循环的使用方法 2023-07-04
- user32.dll 函数说明小结 2022-12-26
- WPF使用DrawingContext实现绘制刻度条 2023-07-04
- 在C# 8中如何使用默认接口方法详解 2023-03-29
- Unity3D实现渐变颜色效果 2023-01-16
- c# 模拟线性回归的示例 2023-03-14
- Unity Shader实现模糊效果 2023-04-27
- .NET CORE DI 依赖注入 2023-09-27
- 如何使用C# 捕获进程输出 2023-03-10
- C# 使用Aspose.Cells 导出Excel的步骤及问题记录 2023-05-16