Engine.is_editor_hint
2026/4/14大约 4 分钟
最后更新日期:2026-04-16
最后同步日期:2026-04-15 | Godot 官方原文 — Engine.is_editor_hint
Engine.is_editor_hint
定义
Engine.is_editor_hint 返回一个布尔值,告诉你当前代码是否在 Godot 编辑器中运行——而不是在导出后的正式游戏中运行。
打个比方:你是一个演员,平时在排练室(编辑器)排练和正式上台演出(导出的游戏)会做不同的事情。比如排练时你可能需要对着镜子检查动作,但正式演出时不需要。IsEditorHint() 就像帮你判断"现在是在排练还是在正式演出"。
在游戏开发中,这个功能非常重要,因为很多代码在编辑器中和在游戏中需要不同的行为——比如编辑器中显示辅助线、预览效果,但游戏中不应该显示这些。
函数签名
C#
// Engine.IsEditorHint 检查是否在编辑器中运行
public static bool IsEditorHint()GDScript
static func is_editor_hint() -> bool参数说明
此方法没有参数。
返回值
bool —— true 表示当前在编辑器中运行,false 表示在导出的游戏中运行。
代码示例
基础用法:判断运行环境
C#
using Godot;
public partial class MyScene : Node
{
public override void _Ready()
{
if (Engine.IsEditorHint())
{
GD.Print("当前在编辑器中运行");
// 运行结果(编辑器中): 当前在编辑器中运行
}
else
{
GD.Print("当前在导出的游戏中运行");
// 运行结果(导出后): 当前在导出的游戏中运行
}
}
}GDScript
extends Node
func _ready():
if Engine.is_editor_hint():
print("当前在编辑器中运行")
# 运行结果(编辑器中): 当前在编辑器中运行
else:
print("当前在导出的游戏中运行")
# 运行结果(导出后): 当前在导出的游戏中运行实际场景:编辑器中显示调试辅助线
C#
using Godot;
public partial class PatrolPath : Node2D
{
[Export] public float ExPatrolRadius = 100.0f;
private Line2D _debugLine;
public override void _Ready()
{
if (Engine.IsEditorHint())
{
// 编辑器中:创建辅助线显示巡逻范围
_debugLine = new Line2D();
_debugLine.DefaultColor = Colors.Cyan;
AddChild(_debugLine);
_DrawPatrolCircle();
// 运行结果(编辑器中): 显示一个青色的巡逻范围圆圈
}
}
private void _DrawPatrolCircle()
{
// 绘制巡逻范围的辅助圆圈(只在编辑器中可见)
for (int i = 0; i <= 32; i++)
{
float angle = i * Mathf.Tau / 32;
float x = Mathf.Cos(angle) * ExPatrolRadius;
float y = Mathf.Sin(angle) * ExPatrolRadius;
_debugLine?.AddPoint(new Vector2(x, y));
}
}
}GDScript
extends Node2D
@export var patrol_radius: float = 100.0
var _debug_line: Line2D
func _ready():
if Engine.is_editor_hint():
# 编辑器中:创建辅助线显示巡逻范围
_debug_line = Line2D.new()
_debug_line.default_color = Color.CYAN
add_child(_debug_line)
_draw_patrol_circle()
# 运行结果(编辑器中): 显示一个青色的巡逻范围圆圈
func _draw_patrol_circle():
# 绘制巡逻范围的辅助圆圈(只在编辑器中可见)
for i in range(33):
var angle = i * TAU / 32
var x = cos(angle) * patrol_radius
var y = sin(angle) * patrol_radius
_debug_line.add_point(Vector2(x, y))进阶用法:跳过编辑器中不需要的逻辑
C#
using Godot;
public partial class AudioManager : Node
{
[Export] public float ExMasterVolume = 1.0f;
public override void _Ready()
{
// 编辑器中跳过音频初始化,避免编辑器里也能听到游戏音效
if (Engine.IsEditorHint())
{
return;
}
// 正式游戏中初始化音频系统
AudioServer.SetBusVolumeDb(0, Mathf.LinearToDb(ExMasterVolume));
GD.Print($"音频系统已初始化,音量: {ExMasterVolume}");
// 运行结果(导出后): 音频系统已初始化,音量: 1
}
public override void _Process(double delta)
{
// 编辑器中不执行游戏逻辑
if (Engine.IsEditorHint())
{
return;
}
// 正式游戏中的音频处理逻辑...
}
}GDScript
extends Node
@export var master_volume: float = 1.0
func _ready():
# 编辑器中跳过音频初始化,避免编辑器里也能听到游戏音效
if Engine.is_editor_hint():
return
# 正式游戏中初始化音频系统
AudioServer.set_bus_volume_db(0, linear_to_db(master_volume))
print("音频系统已初始化,音量: ", master_volume)
# 运行结果(导出后): 音频系统已初始化,音量: 1.0
func _process(delta):
# 编辑器中不执行游戏逻辑
if Engine.is_editor_hint():
return
# 正式游戏中的音频处理逻辑...
pass注意事项
_Ready()在编辑器中也会被调用:当场景在编辑器中被加载时,_Ready()中的代码也会执行。所以需要在_Ready()开头检查IsEditorHint()。_Process()在编辑器中默认不运行:除非你给节点设置了@tool(GDScript)或[Tool](C#)属性。但_Ready()不同,它总是会被调用。- 使用
@tool/[Tool]时特别注意:如果你标记了一个脚本为@tool,它的_Process()在编辑器中也会运行。这时必须用IsEditorHint()来区分编辑器和游戏行为。 - 导出后始终返回
false:在导出的游戏中,IsEditorHint()永远返回false,所以不需要担心残留的编辑器逻辑泄漏到正式版。
