Unity帧率、屏幕刷新率

帧率

帧:影像动画中最小单位的单副影像画面,相当于电影胶片上的每一个镜头

帧率:图形处理器每秒更新帧的次数

帧率单位:帧每秒(F/S, Frame pre second,简称 FPS)

帧率(Frame rate) = 帧数(Frames)/时间(Time)

帧率大小影响视觉上画面的平滑度,如果帧率过小(<16FPS)那么就能够感到画面明显的卡顿,早期影视动画要求画面帧率保持在(18-24FPS),帧率越大在屏幕上显示的画面越平滑,一般超过75FPS就不容易察觉到有明显的流畅度提升了。

帧率是每秒播放的画面数量,一般每帧的时间不是固定的,显卡性能越好,每秒能处理的帧数就越高。反之,显卡性能越差,那么每秒处理的画面就降低,帧率也就降低了。

Unity 中如何控制帧率,以及如何计算帧率

Unity 每帧都会执行 void Update() 方法

先来计算帧率

在此 Update 方法中记录帧次数,同时统计时间,最后用帧次数/统计时间 得到帧率,代码如下

using UnityEngine;

public class Frame : MonoBehaviour
{
    // 记录帧数
    private int _frame;
    // 上一次计算帧率的时间
    private float _lastTime;
    // 平均每一帧的时间
    private float _frameDeltaTime;
    // 间隔多长时间(秒)计算一次帧率
    private float _Fps;
    private const float _timeInterval = 0.5f;

    void Start()
    {
        _lastTime = Time.realtimeSinceStartup;
    }

    void Update()
    {
        FrameCalculate();
    }

    private void FrameCalculate()
    {
        _frame++;
        if (Time.realtimeSinceStartup - _lastTime < _timeInterval)
        {
            return;
        }

        float time = Time.realtimeSinceStartup - _lastTime;
        _Fps = _frame / time;
        _frameDeltaTime = time / _frame;

        _lastTime = Time.realtimeSinceStartup;
        _frame = 0;
    }

    private void OnGUI()
    {
        string msg = string.Format("Fps:{0}  FpsDeltaTime:{1}", _Fps, _frameDeltaTime);
        GUI.Label(new Rect(0, 0, 300, 50), msg);
    }
}

如何人为控制帧率

Project Settings... -> Quality->VSync Count

当 VSync Count 设置为 Every V Black 此时就是 60 帧

当 VSync Count 设置为 Every Second V Black 此时就是 30 帧

当 VSync Count 设置为 Don't Sync ,通过代码设置帧数

Application.targetFrameRate = 50;

Unity 手册上对 Application.targetFrameRate 的解释如下

Application.targetFrameRate

Leave feedback

public static int targetFrameRate;

Description

Instructs the game to try to render at a specified frame rate.

The default targetFrameRate is a special value of -1, which indicates that the game should render at the platform's default frame rate. This default rate depends on the platform:

- On standalone platforms the default frame rate is the maximum achievable frame rate.

- On mobile platforms the default frame rate is less than the maximum achievable frame rate due to the need to conserve battery power. Typically on mobile platforms the default frame rate is 30 frames per second.

- All mobile platforms have a fix cap for their maximum achievable frame rate, that is equal to the refresh rate of the screen (60 Hz = 60 fps, 40 Hz = 40 fps, ...). Screen.currentResolution contains the screen's refresh rate.

- Additionally, all mobile platforms can only display frames on a VBlank. Therefore, you should set the targetFrameRate to either -1, or a value equal to the screen's refresh rate, or the refresh rate divided by an integer. Otherwise, the resulting frame rate is always lower than targetFrameRate. Note: If you set the targetFrameRate to the refresh rate divided by an integer, the integer division leads to the same effective fps as setting QualitySettings.vSyncCount to the same value as that integer.

- iOS ignores the QualitySettings.vSyncCount setting. Instead, the device displays frames on the first possible VBlank after the frame is ready and your application achieves the targetFrameRate.

- On WebGL the default value lets the browser choose the frame rate to match its render loop timing which generally produces the smoothest results. Non-default values are only recommended if you want to throttle CPU usage on WebGL.

- When using VR Unity will use the target frame rate specified by the SDK and ignores values specified by the game.

Setting targetFrameRate does not guarantee that frame rate will be achieved. There can be fluctuations due to platform specifics, or the game might not achieve the frame rate because the device is too slow.

Additionally if the QualitySettings.vSyncCount property is set, the targetFrameRate will be ignored and instead the game will use the vSyncCount and the platform's default render rate to determine the target frame rate. For example, if the platform's default render rate is 60 frames per second and vSyncCount is set to 2, the game will target 30 frames per second.

指示游戏尝试以指定的帧速率进行渲染。

默认targetFrameRate值为 -1 的特殊值,表示游戏应以平台的默认帧速率渲染。此默认速率取决于平台:

- 在独立平台上,默认帧速率是可实现的最大帧速率。

- 在移动平台上,由于需要节省电池电量,默认帧速率低于可实现的最大帧速率。通常在移动平台上,默认帧速率为每秒 30 帧。

- 所有移动平台都有其最大可实现帧速率的固定上限,即等于屏幕的刷新率(Screen.currentResolution)(60 Hz = 60 fps,40 Hz = 40 fps,...)。Screen.currentResolution包含屏幕的刷新率。

- 此外,所有移动平台只能在 VBlank 上显示帧。因此,您应该将 设置targetFrameRate为 -1,或者等于屏幕刷新率的值,或者刷新率除以一个整数。否则,生成的帧速率始终低于targetFrameRate. 注意:如果将 设置targetFrameRate为刷新率除以整数,则整数除法导致的有效 fps 与将QualitySettings.vSyncCount设置为与该整数相同的值相同。

- iOS 忽略QualitySettings.vSyncCount设置。相反,设备在帧准备好后,在第一个可能的VBlank 上显示帧


- 在 WebGL 上,默认值允许浏览器选择帧速率以匹配其渲染循环时间,这通常会产生最平滑的结果。只有当你想限制WebGL上的CPU使用时,才建议使用非默认值。


- 在使用VR时,Unity将使用SDK指定的目标帧率,并忽略游戏指定的值。

设置targetFrameRate并不能保证会达到帧速率。由于平台的具体情况可能会出现波动,或者游戏可能因为设备太慢而无法达到帧速率。

此外,如果设置了QualitySettings.vSyncCount属性,则targetFrameRate将被忽略,而游戏将使用 vSyncCount 和平台的默认渲染速率来确定目标帧速率。例如,如果平台的默认渲染速率为每秒 60 帧,并且 vSyncCount 设置为 2,则游戏的目标为每秒 30 帧。

屏幕刷新率

显示器屏幕刷新率就是屏幕每秒画面被刷新的次数,放电影就是将一个个画面不断的更替播放出来,因为有视觉停留效应,前一副画面在大脑中的印象还没有消失,紧接着后边一副画面就播放了,由于时间间隔足够短,我们就觉得画面在动了。

屏幕刷新率是固定的,每次刷新的间隔是完全一样的。帧率每一帧的时间是不固定的。

市场上显示器的刷新率多种多样,如60Hz、144Hz、240Hz等,这些数值就是显示器的物理刷新速度,比如60Hz的屏幕就可以在一秒刷新60张画面。

游戏帧数和屏幕刷新率是相关的

如果显示器屏幕刷新率是60Hz,就算游戏帧率是 100+Hz,显示器也只能每秒刷新60张画面,导致性能浪费。

反之显示器屏幕刷新频率是144Hz,游戏帧率是 60Hz,显示器依然是刷新144张画面,但是会出现连续几次刷新现实的是同一帧的画面,因为帧数提供的画面少

游戏帧率受下面几个因素影响

显卡>内存>处理器>网络>硬盘

显示器屏幕刷新率:Hz取决于显示器的物理条件

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值