【教程】Unity 帧数上限设置

开发平台:Unity 2017版本以上
编程平台:Visual Studio 2017以上
修改日期:

  • 第一版 2023/04/06
  • 第二版 2024/03/28 修改早期笔记中错误内容

帧数参考表


旧设备最低限制(Phone)次低限制标准(PC)最佳更高
帧数24Hz30Hz45Hz60Hz120Hz144Hz
  • 通常情况以 30Hz/60Hz/120Hz 作为分水岭

设置帧数上限目的


  • 解决帧数波动大造成的视觉感官眩晕

准备阶段:关闭 垂直同步


  1. 打开工程项目,前往 [菜单栏] “Edit -> Project Setting”
  2. 打开 “Quality” 分栏
    • 在 “Other” 分区中,将 [选项] V Sync Count(垂直同步计数)设置为 Don't Sync
    • 详细选项说明见下列表:
选项Don’t SyncEvery V BlankEvery 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:单位帧数时间
  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值