Engine.get_frames_per_second
2026/4/14大约 3 分钟
最后更新日期:2026-04-16
最后同步日期:2026-04-15 | Godot 官方原文 — Engine.get_frames_per_second
Engine.get_frames_per_second
定义
Engine.get_frames_per_second 返回游戏当前每秒渲染的帧数(FPS,Frames Per Second)——告诉你"游戏现在跑得有多流畅"。
打个比方:FPS 就像翻页动画的"翻页速度"。如果每秒翻 60 页(60 FPS),动画看起来非常流畅;如果每秒只翻 15 页(15 FPS),动画就会一卡一卡的。GetFps() 就是告诉你当前翻了多少页。
在游戏开发中,FPS 是衡量游戏性能的最直观指标。60 FPS 被认为是流畅的标准,30 FPS 是可接受的最低限度,低于 30 FPS 就会让玩家感到卡顿。
函数签名
C#
// Engine.GetFps 获取当前帧率
public static float GetFps()GDScript
static func get_frames_per_second() -> float参数说明
此方法没有参数。
返回值
float —— 当前每秒帧数(FPS)。注意这个值会有波动,它反映的是最近一小段时间的平均帧率。
代码示例
基础用法:显示当前帧率
C#
using Godot;
public partial class MyScene : Node
{
public override void _Ready()
{
float fps = Engine.GetFps();
GD.Print($"当前帧率: {fps} FPS");
// 运行结果: 当前帧率: 60.0 FPS
}
}GDScript
extends Node
func _ready():
var fps = Engine.get_frames_per_second()
print("当前帧率: ", fps, " FPS")
# 运行结果: 当前帧率: 60.0 FPS实际场景:实时 FPS 监控显示
C#
using Godot;
public partial class FpsDisplay : Label
{
private float _updateTimer = 0;
public override void _Process(double delta)
{
// 每 0.5 秒更新一次显示(避免数字跳动太快)
_updateTimer += (float)delta;
if (_updateTimer >= 0.5f)
{
_updateTimer = 0;
float fps = Engine.GetFps();
Text = $"FPS: {fps:F0}";
// 如果帧率低于 30,变红警告
if (fps < 30)
{
AddThemeColorOverride("font_color", Colors.Red);
// 运行结果: 文字变红,显示 "FPS: 24"
}
else
{
AddThemeColorOverride("font_color", Colors.Green);
// 运行结果: 文字绿色,显示 "FPS: 60"
}
}
}
}GDScript
extends Label
var _update_timer: float = 0
func _process(delta):
# 每 0.5 秒更新一次显示(避免数字跳动太快)
_update_timer += delta
if _update_timer >= 0.5:
_update_timer = 0
var fps = Engine.get_frames_per_second()
text = "FPS: %.0f" % fps
# 如果帧率低于 30,变红警告
if fps < 30:
add_theme_color_override("font_color", Color.RED)
# 运行结果: 文字变红,显示 "FPS: 24"
else:
add_theme_color_override("font_color", Color.GREEN)
# 运行结果: 文字绿色,显示 "FPS: 60"进阶用法:性能自适应——低帧率时降低画质
C#
using Godot;
public partial class PerformanceManager : Node
{
[Export] public int ExTargetFps = 60;
[Export] public int ExLowFpsThreshold = 30;
private int _qualityLevel = 2; // 0=低, 1=中, 2=高
private float _checkTimer = 0;
public override void _Process(double delta)
{
// 每 2 秒检查一次帧率
_checkTimer += (float)delta;
if (_checkTimer < 2.0f) return;
_checkTimer = 0;
float fps = Engine.GetFps();
if (fps < ExLowFpsThreshold && _qualityLevel > 0)
{
_qualityLevel--;
_ApplyQualitySettings();
GD.Print($"帧率过低({fps:F0} FPS),降低画质到: {_QualityName()}");
// 运行结果: 帧率过低(22 FPS),降低画质到: 中
}
}
private void _ApplyQualitySettings()
{
switch (_qualityLevel)
{
case 0:
// 低画质设置
GetViewport().Msaa2D = Viewport.Msaa.Disabled;
break;
case 1:
// 中画质设置
GetViewport().Msaa2D = Viewport.Msaa.Msaa2X;
break;
case 2:
// 高画质设置
GetViewport().Msaa2D = Viewport.Msaa.Msaa4X;
break;
}
}
private string _QualityName() => _qualityLevel switch
{
0 => "低", 1 => "中", _ => "高"
};
}GDScript
extends Node
@export var target_fps: int = 60
@export var low_fps_threshold: int = 30
var _quality_level: int = 2 # 0=低, 1=中, 2=高
var _check_timer: float = 0
func _process(delta):
# 每 2 秒检查一次帧率
_check_timer += delta
if _check_timer < 2.0:
return
_check_timer = 0
var fps = Engine.get_frames_per_second()
if fps < low_fps_threshold and _quality_level > 0:
_quality_level -= 1
_apply_quality_settings()
print("帧率过低(%.0f FPS),降低画质到: %s" % [fps, _quality_name()])
# 运行结果: 帧率过低(22 FPS),降低画质到: 中
func _apply_quality_settings():
match _quality_level:
0:
get_viewport().msaa_2d = Viewport.MSAA_DISABLED
1:
get_viewport().msaa_2d = Viewport.MSAA_2X
2:
get_viewport().msaa_2d = Viewport.MSAA_4X
func _quality_name() -> String:
match _quality_level:
0: return "低"
1: return "中"
_: return "高"注意事项
- FPS 有波动是正常的:帧率不是固定不变的,它取决于每帧的工作量。偶尔掉到 55-58 是正常的,不需要恐慌。
- 引擎启动初期 FPS 不准确:游戏刚启动的头几秒,FPS 可能偏低或不稳定,因为引擎还在加载资源。
- 在编辑器中 FPS 会偏低:编辑器本身也在运行,会占用一些性能,导出后的游戏 FPS 通常会更高。
- 不要每帧都用 FPS 做判断:FPS 是一个采样平均值,短期波动很大。如果要根据性能做决策,建议取多帧的平均值或使用定时检查。
