Trim comments and use implicit null checks for Unity objects
Reduce comments to non-obvious "why" notes and convert UnityEngine.Object null checks to implicit bool (if (obj) / if (!obj)). Input System devices (Mouse.current, Keyboard.current) and plain arrays keep explicit != null. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
3da613e1a1
commit
f758c96dc5
8 files changed
+220
-427
No files matched your search
@@ -1,58 +1,38 @@
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// Limits the game's frame rate.
|
||||
///
|
||||
/// Sets <see cref="Application.targetFrameRate"/> and adjusts VSync
|
||||
/// (<see cref="QualitySettings.vSyncCount"/>) accordingly. IMPORTANT: as long as
|
||||
/// VSync is active, Unity ignores targetFrameRate - the limit then only takes
|
||||
/// effect when VSync is off. This script therefore disables VSync automatically
|
||||
/// as soon as an FPS limit is set (see <see cref="disableVSyncWhenLimiting"/>).
|
||||
///
|
||||
/// Changes apply live: in the editor via OnValidate (also in Play mode while
|
||||
/// tweaking values in the Inspector).
|
||||
///
|
||||
/// Simply attach to any (ideally persistent) GameObject in the scene.
|
||||
/// Caps the frame rate via <see cref="Application.targetFrameRate"/>. VSync is
|
||||
/// disabled automatically while limiting, because it would otherwise override
|
||||
/// targetFrameRate. Changes apply live (also in Play mode) via OnValidate.
|
||||
/// </summary>
|
||||
[DisallowMultipleComponent]
|
||||
public class FrameRateLimiter : MonoBehaviour
|
||||
{
|
||||
[Header("Limit")]
|
||||
[Tooltip("ON = the frame rate is capped to 'Target FPS'. OFF = unlimited " +
|
||||
"(targetFrameRate = -1, platform default).")]
|
||||
[Tooltip("Cap the frame rate to 'Target FPS' (off = unlimited).")]
|
||||
public bool limitFrameRate = true;
|
||||
|
||||
[Tooltip("Target frame rate in frames per second when the limit is active.")]
|
||||
[Tooltip("Target frame rate when the limit is active.")]
|
||||
[Range(1, 360)]
|
||||
public int targetFps = 60;
|
||||
|
||||
[Header("VSync")]
|
||||
[Tooltip("When an FPS limit is set, disable VSync automatically. " +
|
||||
"Otherwise VSync would override targetFrameRate and the limit " +
|
||||
"would have no effect. Normally leave this ON.")]
|
||||
[Tooltip("Disable VSync while limiting (VSync would otherwise override the FPS limit). Normally ON.")]
|
||||
public bool disableVSyncWhenLimiting = true;
|
||||
|
||||
[Tooltip("VSync count to use when NO limit is active " +
|
||||
"(0 = VSync off, 1 = every screen refresh, 2 = every second refresh).")]
|
||||
[Tooltip("VSync count when no limit is active (0 = off, 1 = every refresh, 2 = every second refresh).")]
|
||||
[Range(0, 2)]
|
||||
public int vSyncCountWhenUnlimited = 0;
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
Apply();
|
||||
}
|
||||
void OnEnable() => Apply();
|
||||
|
||||
void OnValidate()
|
||||
{
|
||||
// Live adjustment when changing values in the Inspector (also in Play mode).
|
||||
if (Application.isPlaying)
|
||||
Apply();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Applies the current limit (FPS + VSync) to the Unity settings.
|
||||
/// Can also be called externally.
|
||||
/// </summary>
|
||||
/// <summary>Applies the current FPS + VSync settings.</summary>
|
||||
public void Apply()
|
||||
{
|
||||
if (limitFrameRate)
|
||||
@@ -65,7 +45,7 @@ public class FrameRateLimiter : MonoBehaviour
|
||||
else
|
||||
{
|
||||
QualitySettings.vSyncCount = vSyncCountWhenUnlimited;
|
||||
Application.targetFrameRate = -1; // platform default / unlimited
|
||||
Application.targetFrameRate = -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user