
unity 动画帧速率
Ever wonder if it’s possible to make Unity follow a precise frame rate and potentially even follow an external clock source (commonly known as genlock)? This post will discuss how Unity natively maintains frame rates and how user code can be added to tightly control it. This can be vital in an environment like a broadcast studio where synchronization between Unity and other equipment is crucial.
有没有想过是否有可能使Unity遵循精确的帧速率,甚至可能遵循外部时钟源(通常称为 genlock )? 这篇文章将讨论Unity如何本地保持帧速率,以及如何添加用户代码以对其进行严格控制。 这在广播工作室等环境中至关重要,在该环境中,Unity与其他设备之间的同步至关重要。
Normally, out of the box, a Unity project will attempt to run your project as fast as possible. Frames will be rendered as quickly as they can while generally being limited by your display device’s refresh rate (see V Sync Count). The simplest way to start controlling frame rate is to explicitly set the QualitySettings.vSyncCount so that rendering will occur at an interval related to the display device’s refresh rate (e.g., for a 60Hz display, setting vSyncCount=2 will cause Unity to render at 30fps in sync with the display). This may not give granular enough control, however, as you are limited to submultiples of the display refresh rate.
通常,开箱即用,Unity项目将尝试尽快运行您的项目。 帧将尽快渲染,而通常受显示设备的刷新率限制(请参见 V Sync Count )。 开始控制帧频的最简单方法是显式设置 QualitySettings.vSyncCount, 以便在与显示设备的刷新率相关的时间间隔进行渲染(例如,对于60Hz显示器,设置vSyncCount = 2将导致Unity以30fps的速度渲染与显示同步)。 但是,这可能无法提供足够的精细控制,因为您只能使用显示刷新率的整数倍。
The next simplest solution would be to set QualitySettings.vSyncCount=0 and use Application.targetFrameRate to target a frame rate independent of the display’s refresh rate. With this set, Unity will throttle back its rendering loop to approximately this rate (note that tearing may occur since Unity will no longer be rendering in sync with the display). This is done in a low-cost manner so as not to unnecessarily burn CPU resources. The downside is that this approach may not yield the required precision for every use case.
下一个最简单的解决方案是设置 QualitySettings.vSyncCount = 0并使用 Application.targetFrameRate 来定位独立于显示器刷新率的帧速率。 设置此设置后,Unity会将其渲染循环的速度降低到大约此速率(请注意, 由于Unity将不再与显示同步进行渲染,因此可能会发生 撕裂 )。 这是以低成本方式完成的,以免不必要地消耗CPU资源。 缺点是,这种方法可能无法为每个用例提供所需的精度。
Fear not, coroutines can help improve precision. Rather than rely on Unity’s built-in frame rate throttling, you can control it yourself via script code. In order to do so, you must let Unity try to run as fast as possible by setting QualitySettings.vSyncCount=0 and Application.targetFrameRate to a very high value, and then, using a WaitForEndOfFrame coroutine, slow it down to precisely the rate you are looking for by refusing to allow the next frame to start rendering until