EditorInterface
继承: Object
Godot 编辑器的接口。
描述
EditorInterface 允许控制 Godot 编辑器的窗口,包括自定义窗口,保存和重新加载场景,渲染网格预览,检查和编辑资源和对象,并提供对 EditorSettings , EditorFileSystem , EditorResourcePreview , ScriptEditor ,编辑器视口以及场景信息的访问。
注意: 这个类不应该被直接实例化。而是,直接通过名称访问单例。
var editor_settings = EditorInterface.get_editor_settings()
// 在 C# 中,可以通过静态 Singleton 属性来访问。
EditorSettings settings = EditorInterface.Singleton.GetEditorSettings();
属性
方法
属性说明
如果为 true,将启用专注模式,该模式会隐藏侧边面板,增加主视图的可用空间。
如果为 true,则编辑器启用了 Movie Maker 模式。详见 MovieWriter。
方法说明
编辑给定的 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。
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 的位置和尺寸。
注意:这个值是通过 interface/editor/display_scale 和 interface/editor/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,编号从 0 到 3。可以通过 Viewport.get_camera_3d() 访问视口中的活动编辑器相机。
FileSystemDock get_file_system_dock() const 🔗
返回编辑器的文件系统面板 FileSystemDock 实例。
警告:移除和释放此节点将使编辑器的一部分失去作用,并可能导致崩溃。
EditorInspector get_inspector() const 🔗
返回编辑器的属性检查器 EditorInspector实例。
警告:删除和释放这个节点将使编辑器的一部分失去作用,并可能导致崩溃。
PackedStringArray get_open_scenes() const 🔗
返回包含当前打开的场景的文件路径的数组 Array。
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 实例。
void inspect_object(object: Object, for_property: String = "", inspector_only: bool = false) 🔗
在编辑器的检查器面板中显示给定 object 的属性。如果 inspector_only 为 true ,插件将不会试图编辑 object。
bool is_multi_window_enabled() const 🔗
如果编辑器启用了多窗口支持,则返回 true。以下所有条件都满足时才会启用多窗口支持:
Viewport.gui_embed_subwindows 为
false。如果平台是 Web 等不支持多窗口的平台,或者使用了--single-window命令行参数,则强制为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_inherited 为 true,则会创建新的继承场景。
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 必须接受一个类型为 StringName 的参数,其中包含的是所选对象的类型名称,如果没有选中条目则为空。
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) 🔗
弹出编辑器对话框,用于选择所编辑场景中的 Node。callback 必须接受单个类型为 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) 🔗
重启编辑器。编辑器会关闭,然后再打开相同项目。如果 save 为 true,则重启前会保存项目。
void save_all_scenes() 🔗
保存编辑器中打开的所有场景。
保存当前活动场景。返回 @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 必须与相关选项卡的文本完全匹配(2D、3D、Script、AssetLib)。
void set_plugin_enabled(plugin: String, enabled: bool) 🔗
设置插件的启用状态。插件名称与其目录名称相同。
void stop_playing_scene() 🔗
停止当前正在播放的场景。