开发平台:Unity 2017版本以上
编程平台:Visual Studio 2017以上
修改日期:
- 第一版
2023/04/06 - 第二版 2024/03/28 修改早期笔记中错误内容
帧数参考表
旧设备 | 最低限制(Phone) | 次低限制 | 标准(PC) | 最佳 | 更高 | |
---|---|---|---|---|---|---|
帧数 | 24Hz | 30Hz | 45Hz | 60Hz | 120Hz | 144Hz |
- 通常情况以 30Hz/60Hz/120Hz 作为分水岭
设置帧数上限目的
- 解决帧数波动大造成的视觉感官眩晕
准备阶段:关闭 垂直同步
- 打开工程项目,前往 [菜单栏] “Edit -> Project Setting”
- 打开 “Quality” 分栏
- 在 “Other” 分区中,将 [选项] V Sync Count(垂直同步计数)设置为
Don't Sync
。 - 详细选项说明见下列表:
- 在 “Other” 分区中,将 [选项] V Sync Count(垂直同步计数)设置为
选项 | Don’t Sync | Every V Blank | Every Second V Blank |
---|---|---|---|
不作垂直同步 | 每帧垂直同步 | 每帧取垂直同步一半的值 |
注意: 如果开启 V Sync Count,则上限限制帧数失效。详细参考 Unity 文档 设置帧数。
程序:设置上限值
void Start() {
//帧数限制为 30
Application.targetFrameRate = 30;
}
- 核心代码:
Application.targetFrameRate
用于设置帧数上限
四、观察帧数是否锁定
- 新建并添加
FPSShow.cs
至 GameController 对象上 - 在
FPSShow.cs
添加下列代码内容:
using System;
using UnityEngine;
/// <summary>
/// 帧数显示器
/// </summary>
public class FPSShow : MonoBehaviour
{
/// <summary>
/// 单位统计时间
/// </summary>
private const float fpsMeasureTime = 1f;
/// <summary>
/// 单位帧数统计时间
/// </summary>
private const int fpsMeasureFrame = 30;
/// <summary>
/// 帧数统计
/// </summary>
private int fpsCount;
/// <summary>
/// 计时器
/// </summary>
private float timer;
/// <summary>
/// 单位时间帧数
/// </summary>
private int fps;
/// <summary>
/// 单位帧数耗时
/// </summary>
private float timeUse;
private string result;
private GUIStyle style;
private Rect rect;
/// <summary>
/// FPS统计类型
/// </summary>
public enum FPSType
{
FixedTime,
FixedFrame
}
/// <summary>
/// FPS在屏幕中的位置
/// </summary>
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();
}
}
/// <summary>
/// 固定帧数时间法
/// </summary>
private void FixedFPSTime()
{
timer += Time.deltaTime;
fpsCount += 1;
if (timer >= fpsMeasureTime)
{
fps = Mathf.RoundToInt(fpsCount / timer);
result = "FPS:" + fps.ToString();
fpsCount = 0;
timer = 0f;
}
}
/// <summary>
/// 固定时间帧数法
/// </summary>
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;
}
}
/// <summary>
/// GUI可视化窗口
/// </summary>
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:单位帧数时间