开发平台:Unity 2017版本以上 编程平台:Visual Studio 2017以上
一、认识FPS (帧数)FPS (Frames Per Second)是图像领域中的定义,是指画面每秒传输帧数,通俗来讲就是指动画或视频的画面数。
二、常见帧数参考表 帧数描述24Hz极少数旧设备的最低帧数30Hz一般人眼能接受的最低帧数最低限定45Hz60Hz中等流程的帧数推荐120Hz丝滑流畅帧数追求其他说明
- PC端选择60Hz以上。手机端开发受设备闲置应稳定在30Hz以上。24Hz不推荐。
- 帧数过低会导致用户出现头晕等不适症状。应尽可能避免帧数忽高忽低的表现。
- 打开工程项目
- 在菜单栏中 “Edit -> Project Setting”
- 打开 “Quality” 分栏
- 在 “Other” 分区中,调整 V Sync Count(垂直同步计数)。
其中,V Sync Count提供三项选择:
选择项描述Don’t Sync不作垂直同步Every V Blank每帧垂直同步Every Second V Blank每帧取垂直同步一半的值- 限制帧数情况下,选择 “Don’t Sync”
- 新建GameObject对象,命名为GameController
- 新建并添加
FPSLimit.cs
至对象上,并添加下列代码内容:
void Start() {
//帧数限制为 30
Application.targetFrameRate = 30;
}
注:该函数方法由Unity提供。
四、观察帧数是否锁定- 新建并添加
FPSShow.cs
至 GameController 对象上 - 在
FPSShow.cs
添加下列代码内容:
using System;
using UnityEngine;
///
/// 帧数显示器
///
public class FPSShow : MonoBehaviour
{
///
/// 单位统计时间
///
private const float fpsMeasureTime = 1f;
///
/// 单位帧数统计时间
///
private const int fpsMeasureFrame = 30;
///
/// 帧数统计
///
private int fpsCount;
///
/// 计时器
///
private float timer;
///
/// 单位时间帧数
///
private int fps;
///
/// 单位帧数耗时
///
private float timeUse;
private string result;
private GUIStyle style;
private Rect rect;
///
/// FPS统计类型
///
public enum FPSType
{
FixedTime,
FixedFrame
}
///
/// FPS在屏幕中的位置
///
public enum RectType
{
UpLeft,
UpMiddle,
UpRight,
DownLeft,
DownMiddle,
DownRight,
Middle,
MiddleLeft,
MiddleRight
}
public FPSType fpsType;
public RectType rectType;
private void Start()
{
timer = 0f;
fpsCount = 0;
}
private void Update()
{
if (fpsType == FPSType.FixedTime)
{
//固定时间帧数法
FixedTimeFPS();
}
else if (fpsType == FPSType.FixedFrame)
{
//固定帧数时间法
FixedFPSTime();
}
}
///
/// 固定帧数时间法
///
private void FixedFPSTime()
{
timer += Time.deltaTime;
fpsCount += 1;
if (timer >= fpsMeasureTime)
{
fps = Mathf.RoundToInt(fpsCount / timer);
result = "FPS:" + fps.ToString();
fpsCount = 0;
timer = 0f;
}
}
///
/// 固定时间帧数法
///
private void FixedTimeFPS()
{
timer += Time.deltaTime;
fpsCount += 1;
if (fpsCount >= fpsMeasureFrame)
{
timeUse = timer / fpsCount;
result = "TPF:" + Math.Round(timeUse, 2).ToString();
fpsCount = 0;
timer = 0f;
}
}
///
/// GUI可视化窗口
///
private void OnGUI()
{
switch (rectType)
{
case RectType.UpLeft:
rect = new Rect(0, 0, 200, 200);
break;
case RectType.UpMiddle:
rect = new Rect(Screen.width / 2, 0, 200, 200);
break;
case RectType.UpRight:
rect = new Rect(Screen.width - 70, 0, 200, 200);
break;
case RectType.Middle:
rect = new Rect(Screen.width / 2, Screen.height / 2, 200, 200);
break;
case RectType.MiddleLeft:
rect = new Rect(0, Screen.height / 2, 200, 200);
break;
case RectType.MiddleRight:
rect = new Rect(Screen.width - 70, Screen.height / 2, 200, 200);
break;
case RectType.DownLeft:
rect = new Rect(0, Screen.height - 20, 200, 200);
break;
case RectType.DownMiddle:
rect = new Rect(Screen.width / 2, Screen.height - 20, 200,200);
break;
case RectType.DownRight:
rect = new Rect(Screen.width - 70, Screen.height - 20, 200, 200);
break;
default:
break;
}
GUI.Label(rect, result);
}
}
- 在 Game 窗口下出现 FPS:为30。即成功。
- FPS:单位时间帧数 TPF:单位帧数时间