To limit the frame rate in Unity, you primarily use the Application.targetFrameRate
property in a script.
Using Application.targetFrameRate
The most common and recommended way to set a target frame rate in Unity is by using the Application.targetFrameRate
property. This tells Unity to attempt to render the game at a specific number of frames per second.
Here's a simple example of how to implement this in a C# script:
using UnityEngine;
public class FrameRateLimiter : MonoBehaviour
{
void Awake()
{
// Set the target frame rate to 60 FPS
Application.targetFrameRate = 60;
}
// Alternatively, you can use Start()
// void Start()
// {
// // Set the target frame rate to 60 FPS
// Application.targetFrameRate = 60;
// }
}
How to Implement:
- Create a new C# Script: In your Unity project, right-click in the Project window, select
Create > C# Script
, and name it something likeFrameRateLimiter
. - Open the Script: Double-click the script to open it in your code editor (like Visual Studio).
- Add the Code: Paste the code above into the script file. You can place
Application.targetFrameRate = 60;
inside theAwake()
orStart()
method.Awake()
is often preferred as it runs beforeStart()
. - Save the Script: Save the script file.
- Attach to an Object: Go back to Unity. As mentioned in the reference, don't forget to add this script to an object of your scene. You can attach it to any GameObject that exists when the scene starts, such as the Main Camera or a dedicated GameManager object.
- Run the Game: When you press play, the script will run, setting the target frame rate.
You can replace 60
with any desired frame rate (e.g., 30
, 90
). Setting Application.targetFrameRate
to -1
tells the engine not to limit the frame rate, allowing it to render as fast as possible (often limited by VSync).
Why Limit Frame Rate?
Limiting the frame rate can be beneficial for several reasons:
- Performance Consistency: Helps prevent spikes and drops, providing a smoother experience, especially on lower-end devices.
- Resource Management: Reduces CPU/GPU usage, which can lead to lower power consumption and less heat generation, particularly important for mobile devices.
- Avoiding Tearing: When combined with VSync, it helps synchronize frames with the monitor's refresh rate.
- Predictable Physics/Game Logic: While Unity's physics (
FixedUpdate
) is tied to a fixed time step, game logic inUpdate
runs every frame. Capping the frame rate can sometimes make frame-dependent logic more predictable.
Monitoring Your Frame Rate
After implementing the script and pressing play, you can monitor your current frame rate using the Stats window in the Unity Editor. This window provides real-time information about rendering performance, including FPS. As the reference states, you can see in the stats window that your FPS are indeed capped (e.g., at 60).
Other Considerations: VSync
While Application.targetFrameRate
sets a target, VSync (QualitySettings.vSyncCount
) can override or interact with this setting.
QualitySettings.vSyncCount = 0;
(VSync Off): The game will attempt to render atApplication.targetFrameRate
. IftargetFrameRate
is -1, it renders as fast as possible.QualitySettings.vSyncCount = 1;
(VSync On - waits for every vertical blank): The frame rate will be capped at the monitor's refresh rate (e.g., 60 FPS on a 60Hz monitor), overridingApplication.targetFrameRate
.QualitySettings.vSyncCount = 2;
(VSync On - waits for every second vertical blank): The frame rate will be capped at half the monitor's refresh rate (e.g., 30 FPS on a 60Hz monitor), overridingApplication.targetFrameRate
.
It's generally recommended to set QualitySettings.vSyncCount = 0;
when using Application.targetFrameRate
to ensure the target rate is respected.
Summary Table
Here's a quick look at the key properties:
Property | Description | Effect on Frame Rate |
---|---|---|
Application.targetFrameRate |
Sets the desired frame rate the application attempts to achieve. | Caps FPS at the specified value (if VSync is off). -1 means no engine limit. |
QualitySettings.vSyncCount |
Determines if the application waits for vertical blanks before rendering. | 0 : VSync Off (respects targetFrameRate ). 1 : Caps FPS at monitor refresh rate. 2 : Caps FPS at half monitor refresh rate. |
By placing a script like the example onto a GameObject in your scene and setting Application.targetFrameRate
, it will work to limit your game's frame rate.