指示游戏尝试以指定的帧率渲染。
默认的 targetFrameRate
是一个特殊值 -1,表示游戏应以平台的默认帧率渲染。此默认速率取决于平台:
- 在独立平台上,默认帧率是可实现的最大帧率。
- 在移动平台上,由于需要节省电池电量,默认帧率小于可实现的最大帧率。移动平台上的默认帧率通常为每秒 30 帧。
- 所有移动平台可实现的最大帧率都有固定上限,这等于屏幕的刷新率(60 Hz = 60 fps,40 Hz = 40 fps,...)。Screen.currentResolution 包含屏幕刷新率。
- 此外,所有移动平台都只能在 VBlank 上显示帧。因此,应将 targetFrameRate
设置为 -1,或是等于屏幕刷新率的值,或是刷新率除以一个整数。否则,生成的帧率始终小于 targetFrameRate
。注意:如果将 targetFrameRate
设置为刷新率除以一个整数,则整数除法得到的有效 fps 与将 QualitySettings.vSyncCount 设置为相同整数值时相同。
- iOS 会忽略 QualitySettings.vSyncCount 设置。相反,设备会在帧准备就绪之后的第一个可能 VBlank 上显示帧,应用程序会实现 /targetFrameRate/。
- 在 WebGL 上,默认值允许浏览器选择帧率来匹配其渲染循环时序,这通常会产生最平滑的效果。只有当您想要限制 WebGL 上的 CPU 使用率时,才建议使用非默认值。
- 使用 VR 时,Unity 将使用 SDK 指定的目标帧率并忽略游戏指定的值。
设置 targetFrameRate
不保证会实现帧率。由于平台规格不同,帧率可能会出现波动,或者由于设备速度太慢,游戏可能无法达到帧率。
此外,如果设置了 QualitySettings.vSyncCount 属性,将忽略 /targetFrameRate/,而游戏将使用 vSyncCount 和平台的默认渲染率来确定目标帧率。例如,如果平台的默认渲染速率为每秒 60 帧且 vSyncCount 设置为 2,则游戏将以每秒 30 帧为目标。
在编辑器中,targetFrameRate
仅影响 Game 视图。不会影响其他编辑器窗口。
using UnityEngine;
public class Example { void Start() { // Make the game run as fast as possible Application.targetFrameRate = 300; } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.