Attention: Here be dragons

This is the latest (unstable) version of this documentation, which may document features not available in or compatible with released stable versions of Godot.

EditorInterface

继承: Object

Godot 编辑器的接口。

描述

EditorInterface 允许控制 Godot 编辑器的窗口,包括自定义窗口,保存和重新加载场景,渲染网格预览,检查和编辑资源和对象,并提供对 EditorSettingsEditorFileSystemEditorResourcePreviewScriptEditor ,编辑器视口以及场景信息的访问。

注意: 这个类不应该被直接实例化。而是,直接通过名称访问单例。

var editor_settings = EditorInterface.get_editor_settings()

属性

bool

distraction_free_mode

bool

movie_maker_enabled

方法

void

add_root_node(node: Node)

Error

close_scene()

void

edit_node(node: Node)

void

edit_resource(resource: Resource)

void

edit_script(script: Script, line: int = -1, column: int = 0, grab_focus: bool = true)

Control

get_base_control() const

EditorCommandPalette

get_command_palette() const

String

get_current_directory() const

String

get_current_feature_profile() const

String

get_current_path() const

Node

get_edited_scene_root() const

String

get_editor_language() const

VBoxContainer

get_editor_main_screen() const

EditorPaths

get_editor_paths() const

float

get_editor_scale() const

EditorSettings

get_editor_settings() const

Theme

get_editor_theme() const

EditorToaster

get_editor_toaster() const

EditorUndoRedoManager

get_editor_undo_redo() const

SubViewport

get_editor_viewport_2d() const

SubViewport

get_editor_viewport_3d(idx: int = 0) const

FileSystemDock

get_file_system_dock() const

EditorInspector

get_inspector() const

float

get_node_3d_rotate_snap() const

float

get_node_3d_scale_snap() const

float

get_node_3d_translate_snap() const

Array[Node]

get_open_scene_roots() const

PackedStringArray

get_open_scenes() const

String

get_playing_scene() const

EditorFileSystem

get_resource_filesystem() const

EditorResourcePreview

get_resource_previewer() const

ScriptEditor

get_script_editor() const

PackedStringArray

get_selected_paths() const

EditorSelection

get_selection() const

PackedStringArray

get_unsaved_scenes() const

void

inspect_object(object: Object, for_property: String = "", inspector_only: bool = false)

bool

is_multi_window_enabled() const

bool

is_node_3d_snap_enabled() const

bool

is_object_edited(object: Object) const

bool

is_playing_scene() const

bool

is_plugin_enabled(plugin: String) const

Array[Texture2D]

make_mesh_previews(meshes: Array[Mesh], preview_size: int)

void

mark_scene_as_unsaved()

void

open_scene_from_path(scene_filepath: String, set_inherited: bool = false)

void

play_current_scene()

void

play_custom_scene(scene_filepath: String)

void

play_main_scene()

void

popup_create_dialog(callback: Callable, base_type: StringName = "", current_type: String = "", dialog_title: String = "", type_blocklist: Array[StringName] = [])

void

popup_dialog(dialog: Window, rect: Rect2i = Rect2i(0, 0, 0, 0))

void

popup_dialog_centered(dialog: Window, minsize: Vector2i = Vector2i(0, 0))

void

popup_dialog_centered_clamped(dialog: Window, minsize: Vector2i = Vector2i(0, 0), fallback_ratio: float = 0.75)

void

popup_dialog_centered_ratio(dialog: Window, ratio: float = 0.8)

void

popup_method_selector(object: Object, callback: Callable, current_value: String = "")

void

popup_node_selector(callback: Callable, valid_types: Array[StringName] = [], current_value: Node = null)

void

popup_property_selector(object: Object, callback: Callable, type_filter: PackedInt32Array = PackedInt32Array(), current_value: String = "")

void

popup_quick_open(callback: Callable, base_types: Array[StringName] = [])

void

reload_scene_from_path(scene_filepath: String)

void

restart_editor(save: bool = true)

void

save_all_scenes()

Error

save_scene()

void

save_scene_as(path: String, with_preview: bool = true)

void

select_file(file: String)

void

set_current_feature_profile(profile_name: String)

void

set_main_screen_editor(name: String)

void

set_object_edited(object: Object, edited: bool)

void

set_plugin_enabled(plugin: String, enabled: bool)

void

stop_playing_scene()


属性说明

bool distraction_free_mode 🔗

  • void set_distraction_free_mode(value: bool)

  • bool is_distraction_free_mode_enabled()

如果为 true,将启用专注模式,该模式会隐藏侧边面板,增加主视图的可用空间。


bool movie_maker_enabled 🔗

  • void set_movie_maker_enabled(value: bool)

  • bool is_movie_maker_enabled()

如果为 true,则编辑器启用了 Movie Maker 模式。详见 MovieWriter


方法说明

void add_root_node(node: Node) 🔗

使 node 成为当前打开的场景的根。仅当场景为空时才有效。如果 node 是场景实例,则会创建一个继承场景。


Error close_scene() 🔗

关闭当前活动的场景,关闭过程中忽略未保存的更改。成功时返回 @GlobalScope.OK,如果没有可关闭的场景则返回 @GlobalScope.ERR_DOES_NOT_EXIST


void edit_node(node: Node) 🔗

编辑给定的 Node。如果该节点在场景树内,将被选中。


void edit_resource(resource: Resource) 🔗

编辑给定的 Resource。如果该资源是 Script,你还可以使用 edit_script() 编辑,指定行列位置。


void edit_script(script: Script, line: int = -1, column: int = 0, grab_focus: bool = true) 🔗

编辑给定的 Script。还可以指定所打开脚本的行和列。打开脚本所使用的编辑器是由用户为该脚本的语言所配置,可能是外部编辑器。


Control get_base_control() const 🔗

返回 Godot 编辑器窗口的主容器。例如,你可以用它来检索容器的大小并相应地放置你的控件。

警告:删除和释放这个节点将使编辑器失效,并可能导致崩溃。


EditorCommandPalette get_command_palette() const 🔗

返回编辑器的 EditorCommandPalette 实例。

警告:删除和释放此节点,将使编辑器的一部分失去作用,并可能导致崩溃。


String get_current_directory() const 🔗

返回 FileSystemDock 中当前正在查看的目录。如果选择了一个文件,则将使用 String.get_base_dir() 返回其基本目录。


String get_current_feature_profile() const 🔗

返回当前激活的功能配置文件的名称。如果默认配置文件当前处于活动状态,则返回空字符串。

为了获取对 EditorFeatureProfile 的引用,你必须使用 EditorFeatureProfile.load_from_file() 加载该功能配置文件。

注意:通过用户界面创建的功能配置文件将从 feature_profiles 目录中的扩展名为 .profile 的文件加载。可以使用 EditorPaths.get_config_dir() 找到编辑器配置文件夹。


String get_current_path() const 🔗

返回在 FileSystemDock 中查看的当前路径。


Node get_edited_scene_root() const 🔗

返回正在编辑的(当前)场景的根 Node


String get_editor_language() const 🔗

返回编辑器界面当前使用的语言。


VBoxContainer get_editor_main_screen() const 🔗

返回负责主屏幕插件和工具的编辑器控件。将其与实现了 EditorPlugin._has_main_screen() 的插件一起使用。

注意:该节点是一个 VBoxContainer,这意味着如果向其添加 Control 子节点,则需要将子节点的 Control.size_flags_vertical 设置为 Control.SIZE_EXPAND_FILL,以使其使用全部可用空间。

警告:移除和释放这个节点将使编辑器的一部分失去作用,并可能导致崩溃。


EditorPaths get_editor_paths() const 🔗

返回 EditorPaths 单例。


float get_editor_scale() const 🔗

返回编辑器 UI(用户界面)的实际缩放比例(1.0 代表 100% 的原始大小)。这个方法非常适合用来调整插件所添加的 UI 元素的位置和尺寸。

注意: 这个数值是由 EditorSettings.interface/editor/appearance/display_scaleEditorSettings.interface/editor/appearance/custom_display_scale 这两个设置项共同决定的。修改设置后,必须重启编辑器才能使更改完全生效。


EditorSettings get_editor_settings() const 🔗

返回编辑器的 EditorSettings 实例。


Theme get_editor_theme() const 🔗

返回编辑器的 Theme

注意:创建自定义编辑器 UI 时,请优先直接通过 GUI 节点的 get_theme_* 方法来访问主题项目。


EditorToaster get_editor_toaster() const 🔗

返回编辑器的 EditorToaster


EditorUndoRedoManager get_editor_undo_redo() const 🔗

返回编辑器的 EditorUndoRedoManager


SubViewport get_editor_viewport_2d() const 🔗

返回 2D 编辑器 SubViewport。视口中没有相机。视图是直接进行变换的,可以使用 Viewport.global_canvas_transform 访问。


SubViewport get_editor_viewport_3d(idx: int = 0) const 🔗

返回指定的 3D 编辑器 SubViewport,编号从 03。可以通过 Viewport.get_camera_3d() 访问视口中的活动编辑器相机。


FileSystemDock get_file_system_dock() const 🔗

返回编辑器的文件系统面板 FileSystemDock 实例。

警告:移除和释放此节点将使编辑器的一部分失去作用,并可能导致崩溃。


EditorInspector get_inspector() const 🔗

返回编辑器的属性检查器 EditorInspector实例。

警告:删除和释放这个节点将使编辑器的一部分失去作用,并可能导致崩溃。


float get_node_3d_rotate_snap() const 🔗

返回 3D 编辑器旋转吸附功能设置的度数。


float get_node_3d_scale_snap() const 🔗

返回 3D 编辑器缩放吸附功能设置的单位数量。


float get_node_3d_translate_snap() const 🔗

返回 3D 编辑器平移吸附功能设置的单位数量。


Array[Node] get_open_scene_roots() const 🔗

返回包含当前所有已打开场景的根节点的数组。


PackedStringArray get_open_scenes() const 🔗

返回包含当前所有已打开场景的文件路径的数组。


String get_playing_scene() const 🔗

返回正在播放的场景名称。如果当前没有场景正在播放,返回一个空字符串。


EditorFileSystem get_resource_filesystem() const 🔗

返回编辑器的 EditorFileSystem 实例。


EditorResourcePreview get_resource_previewer() const 🔗

返回编辑器的 EditorResourcePreview 实例。


ScriptEditor get_script_editor() const 🔗

返回编辑器的脚本编辑器 ScriptEditor 实例。

警告:删除和释放这个节点将使编辑器的一部分失去作用,并可能导致崩溃。


PackedStringArray get_selected_paths() const 🔗

返回一个包含了 FileSystemDock 中当前所选文件(和目录)路径的数组。


EditorSelection get_selection() const 🔗

返回编辑器的 EditorSelection 实例。


PackedStringArray get_unsaved_scenes() const 🔗

返回当前所有未保存场景的文件路径数组。


void inspect_object(object: Object, for_property: String = "", inspector_only: bool = false) 🔗

在编辑器的检查器面板中显示给定 object 的属性。如果 inspector_onlytrue ,插件将不会试图编辑 object


bool is_multi_window_enabled() const 🔗

如果在编辑器中启用了多窗口支持,则返回 true。只有当以下 所有 条件都成立时,多窗口支持才会被启用:

EditorSettings.interface/multi_window/enabletrue

EditorSettings.interface/editor/display/single_window_modefalse

Viewport.gui_embed_subwindowsfalse。在不支持多窗口的平台(例如 Web 端),或者使用了 --single-window 命令行参数 时,Viewport.gui_embed_subwindows 会被强制设为 true


bool is_node_3d_snap_enabled() const 🔗

如果 3D 编辑器当前启用了吸附模式,则返回 true;否则返回 false


bool is_object_edited(object: Object) const 🔗

如果对象已通过 set_object_edited() 标记为已编辑,则返回 true


bool is_playing_scene() const 🔗

如果场景正在播放,返回 true,否则返回 false。暂停的场景将被视为正在播放。


bool is_plugin_enabled(plugin: String) const 🔗

如果启用了指定的 plugin,则返回 true。插件名称与其目录名称相同。


Array[Texture2D] make_mesh_previews(meshes: Array[Mesh], preview_size: int) 🔗

将以给定大小渲染的网格预览返回为元素类型为 Texture2D 的一个 Array


void mark_scene_as_unsaved() 🔗

将当前场景选项卡标记为未保存。


void open_scene_from_path(scene_filepath: String, set_inherited: bool = false) 🔗

打开位于给定路径的场景。如果 set_inheritedtrue,则会创建新的继承场景。


void play_current_scene() 🔗

播放当前活动的场景。


void play_custom_scene(scene_filepath: String) 🔗

播放文件路径所指定的场景。


void play_main_scene() 🔗

播放主场景。


void popup_create_dialog(callback: Callable, base_type: StringName = "", current_type: String = "", dialog_title: String = "", type_blocklist: Array[StringName] = []) 🔗

实验性: 未来版本中可能会修改或移除该方法。

弹出用于创建对象的编辑器对话框。

callback 必须接受一个类型为 String 的参数,其中包含所选对象的类型名称(如果类型是从脚本创建的,则包含该类型的脚本路径),如果没有选择任何项目,则为空字符串。

base_type 指定要显示对象的基础类型。例如设为“Resource”就会在创建对话框中显示所有派生自 Resource 的类型。

current_type 会传递给创建对话框中的搜索框,弹出对话框时会立即选中指定的类型。如果 current_type 不是从 base_type 派生的,则对话框中不会有该类型对应的结果。

dialog_title 可以为对话框自定义标题。适用于需要精准描述对话框用途的场合。如果 dialog_title 为空字符串,则对话框会使用默认标题“新建‘Base Type’”。

type_blocklist 包含的是类型名称列表,创建对话框会隐藏拦截列表中的类型。

注意:尝试在 type_blocklist 中列出基类型将会在创建对话框中隐藏所有从该基类型派生的类型。


void popup_dialog(dialog: Window, rect: Rect2i = Rect2i(0, 0, 0, 0)) 🔗

使用 Window.popup_exclusive() 在编辑器 UI 中弹出 dialog。该对话框目前必须没有父级,否则该方法失败。

另见 Window.set_unparent_when_invisible()


void popup_dialog_centered(dialog: Window, minsize: Vector2i = Vector2i(0, 0)) 🔗

使用 Window.popup_exclusive_centered() 在编辑器 UI 中弹出 dialog。该对话框目前必须没有父级,否则该方法失败。

另见 Window.set_unparent_when_invisible()


void popup_dialog_centered_clamped(dialog: Window, minsize: Vector2i = Vector2i(0, 0), fallback_ratio: float = 0.75) 🔗

使用 Window.popup_exclusive_centered_clamped() 在编辑器 UI 中弹出 dialog。该对话框目前必须没有父级,否则该方法失败。

另见 Window.set_unparent_when_invisible()


void popup_dialog_centered_ratio(dialog: Window, ratio: float = 0.8) 🔗

使用 Window.popup_exclusive_centered_ratio() 在编辑器 UI 中弹出 dialog。该对话框目前必须没有父级,否则该方法失败。

另见 Window.set_unparent_when_invisible()


void popup_method_selector(object: Object, callback: Callable, current_value: String = "") 🔗

弹出编辑器对话框,用于选择 object 的方法。callback 回调必须接受单个 String 类型的参数,其中包含的是所选方法的名称,取消对话框时则为空。如果提供了 current_value 且对应方法存在,则会在方法列表中自动选中该方法。


void popup_node_selector(callback: Callable, valid_types: Array[StringName] = [], current_value: Node = null) 🔗

弹出编辑器对话框,用于选择所编辑场景中的 Nodecallback 必须接受单个类型为 NodePath 的参数。调用回调时会传入所选 NodePath,取消对话框时则为空路径 ^""。如果提供了 valid_types,则对话框只会显示与所列 Node 类型匹配的 Node。

示例:节点首次加入场景树时显示节点选择对话框:

func _ready():
    if Engine.is_editor_hint():
        EditorInterface.popup_node_selector(_on_node_selected, ["Button"])

func _on_node_selected(node_path):
    if node_path.is_empty():
        print("节点选择已取消")
    else:
        print("选中 ", node_path)

void popup_property_selector(object: Object, callback: Callable, type_filter: PackedInt32Array = PackedInt32Array(), current_value: String = "") 🔗

弹出编辑器对话框,用于选择 object 的属性。callback 必须接受单个类型为 NodePath 的参数。调用回调时会传入所选属性路径(见 NodePath.get_as_property_path()),取消对话框时则为空路径 ^""。如果提供了 type_filter,则对话框只会显示与所列 Variant.Type 值匹配的属性。

func _ready():
    if Engine.is_editor_hint():
        EditorInterface.popup_property_selector(this, _on_property_selected, [TYPE_INT])

func _on_property_selected(property_path):
    if property_path.is_empty():
        print("属性选择已取消")
    else:
        print("选中 ", property_path)

void popup_quick_open(callback: Callable, base_types: Array[StringName] = []) 🔗

弹出编辑器对话框,用于快速选择资源文件。callback 回调必须接受单个 String 类型的参数,其中包含的是所选资源的路径,取消对话框时则为空。如果提供了 base_types,则对话框中只会显示与这些类型相匹配的资源。仅支持派生自 Resource 的类型。


void reload_scene_from_path(scene_filepath: String) 🔗

重新加载指定路径的场景。如果该场景当前没有打开,操作将会失败。


void restart_editor(save: bool = true) 🔗

重启编辑器。编辑器会关闭,然后再打开相同项目。如果 savetrue,则重启前会保存项目。


void save_all_scenes() 🔗

保存编辑器中打开的所有场景。


Error save_scene() 🔗

保存当前活动场景。返回 @GlobalScope.OK@GlobalScope.ERR_CANT_CREATE


void save_scene_as(path: String, with_preview: bool = true) 🔗

将当前活动的场景保存为位于 path 的文件。


void select_file(file: String) 🔗

在文件系统面板中选中文件,路径由 file 提供。


void set_current_feature_profile(profile_name: String) 🔗

使用给定的 profile_name 选择并激活指定的功能配置文件。将 profile_name 设置为空字符串以重置为默认功能配置文件。

可以使用 EditorFeatureProfile 类以编程方式创建功能配置文件。

注意:激活的功能配置文件必须位于 feature_profiles 目录中,且该文件的扩展名为 .profile。如果找不到配置文件,则会发生错误。可以使用 EditorPaths.get_config_dir() 找到编辑器配置文件夹。


void set_main_screen_editor(name: String) 🔗

将编辑器的当前主屏幕切换为 name 参数中指定的界面。name 必须与对应标签页的标题完全匹配(例如,对于默认标签页,应使用 2D3DScriptGameAsset Store)。


void set_object_edited(object: Object, edited: bool) 🔗

如果 editedtrue,则该对象被标记为已编辑。

注意:这主要由编辑器用于基于 Resource 的对象,以跟踪其修改状态。例如,对打开的场景、检查器中的资源或编辑的脚本的任何更改都会导致该方法被调用,并返回 true。保存场景、脚本或资源会使用 false 调用该方法,从而重置编辑状态。

注意:对该方法的每次调用都会增加该对象的编辑版本号。这用于在编辑器中跟踪更改,并在需要时触发资源的缩略图重新生成。


void set_plugin_enabled(plugin: String, enabled: bool) 🔗

设置插件的启用状态。插件名称与其目录名称相同。


void stop_playing_scene() 🔗

停止当前正在播放的场景。