-
C#教程之Unity3D基于OnGUI实时显示FPS
本站最新发布 C#从入门到精通
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
帧率(Frame rate)是用于测量显示帧数的量度。所谓的测量单位为每秒显示帧数(Frames per Second,简称:FPS)或“赫兹”(Hz)。此词多用于影视制作和电子游戏。由于人类眼睛的特殊生理结构,如果所看画面之帧率高于16的时候,就会认为是连贯的,此现象称之为视觉暂留。
每秒的帧数(fps)或者说帧率表示图形处理器处理场时每秒钟能够更新的次数。高的帧率可以得到更流畅、更逼真的动画。一般来说30fps就是可以接受的,但是将性能提升至60fps则可以明显提升交互感和逼真感,但是一般来说超过75fps一般就不容易察觉到有明显的流畅度提升了。如果帧率超过屏幕刷新率只会浪费图形处理的能力,因为监视器不能以这么快的速度更新,这样超过刷新率的帧率就浪费掉了。
以下是在Unity3D中显示fps的代码。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
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)); } } |
栏目列表
最新更新
C# 面向对象
假设客车的座位数是9行4列,使用二维数
C#基于接口设计三层架构Unity篇
C#线程 入门
C#读取静态类常量属性和值
C# 插件式编程
C# 委托与事件有啥区别?
C#队列学习笔记:队列(Queue)和堆栈(Stack
linq 多表分组左连接查询查询统计
C#队列学习笔记:MSMQ入门一
C# 在Word中添加Latex 数学公式和符号
inncheck命令 – 检查语法
基于UDP的服务器端和客户端
再谈UDP和TCP
在socket编程中使用域名
网络数据传输时的大小端问题
socket编程实现文件传输功能
如何优雅地断开TCP连接?
图解TCP四次握手断开连接
详细分析TCP数据的传输过程
SqlServer 利用游标批量更新数据
BOS只读状态修改
SQL Server等待事件—PAGEIOLATCH_EX
数据库多行转换为单一列
获取数据表最后最后访问,修改,更新,
计算经历的时间
SQL查询结果自定义排序
修改数据库默认位置
日期简单加或减
从日期获取年,月或日