unity 动画帧速率_Unity中的精确帧速率

本文探讨了如何在Unity中实现精确的帧速率控制,包括使用QualitySettings.vSyncCount、Application.targetFrameRate以及通过协程WaitForEndOfFrame实现自定义帧率限制。通过Thread.Sleep和CPU自旋循环,可以提高精度,甚至与外部时钟同步(genlock)。此外,使用Time.captureFramerate可以确保游戏时间与外部时钟同步推进。提供的GitHub项目提供了概念验证示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

基于粤嵌gec6818开发板的小游戏有五子棋、2048游戏、扫雷、飞机大战、钢琴游戏、刮刮乐等。这些小游戏都可以通过简单的程序设计和硬件驱动来实现。 五子棋是一种简单而又经典的棋类游戏,玩家通过下棋子来争夺胜利。通过gec6818的触摸屏幕输入,可以实现玩家与电脑的对战,或者两个玩家之间的互动。 2048游戏是一款数学益智类游戏,玩家通过上下左右的滑动操作,将相同数值的方块合并,最终得到2048这个数值的方块。通过gec6818的触摸屏幕输入,可以实现玩家的滑动操作,将方块进行合并。 扫雷是一款经典的益智类游戏,玩家通过点击格子,来揭示隐藏的雷,避免踩雷。通过gec6818的触摸屏幕输入,可以实现玩家的点击操作,将雷揭示出来。 飞机大战是一款横版射击游戏,玩家通过控制飞机,射击敌方飞机和敌方炮台,躲避敌方的攻击。通过gec6818的按键输入,可以实现玩家的控制操作,进行射击和躲避。 钢琴游戏是一款音乐类游戏,玩家通过按下不同的琴键,发出不同的音符,弹奏出美妙的音乐。通过gec6818的按键输入,可以实现玩家的按键操作,发出不同的音符。 刮刮乐是一种益智类游戏,玩家通过刮开卡片上的涂层,获得隐藏的奖品。通过gec6818的触摸屏幕输入,可以实现玩家的刮卡操作,揭开涂层,看到奖品。 以上这些游戏都是基于粤嵌gec6818开发板所设计的,通过合理的硬件和软件开发,可以让人们在休闲娱乐中享受到游戏乐趣。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值