Work in progress
The content of this page was not yet updated for Godot
4.4
and may be outdated. If you know how to improve this page or you can confirm
that it's up to date, feel free to open a pull request.
制作主屏幕插件
本教程涵盖的内容
主屏幕插件允许你在编辑器的中心部分创建新的 UI,这些 UI 显示在“2D”、“3D”、“Script”和“AssetLib”按钮旁边。这类编辑器插件被称为“主屏幕插件”。
本教程将带领你创建一个基本的主场景插件. 为了简单起见, 主场景插件将包含一个打印文本到控制台的单个按钮.
初始化插件
首先从Plugins菜单中创建一个新插件. 在本教程中, 我们将把它放在一个名为 main_screen
的文件夹中, 但你可以使用任何你喜欢的名字.
插件脚本会自带 _enter_tree()
和 _exit_tree()
方法, 但对于主场景插件来说, 我们需要添加一些额外的方法. 增加五个额外的方法, 脚本就像这样:
@tool
extends EditorPlugin
func _enter_tree():
pass
func _exit_tree():
pass
func _has_main_screen():
return true
func _make_visible(visible):
pass
func _get_plugin_name():
return "Main Screen Plugin"
func _get_plugin_icon():
return EditorInterface.get_editor_theme().get_icon("Node", "EditorIcons")
#if TOOLS
using Godot;
[Tool]
public partial class MainScreenPlugin : EditorPlugin
{
public override void _EnterTree()
{
}
public override void _ExitTree()
{
}
public override bool _HasMainScreen()
{
return true;
}
public override void _MakeVisible(bool visible)
{
}
public override string _GetPluginName()
{
return "Main Screen Plugin";
}
public override Texture2D _GetPluginIcon()
{
return EditorInterface.Singleton.GetEditorTheme().GetIcon("Node", "EditorIcons");
}
}
#endif
该脚本中最重要的部分是 _has_main_screen()
函数,该函数是重载的,因此它返回 true
。该函数在插件激活时由编辑器自动调用,以告知它该插件为编辑器添加了一个新的中心视图。现在,我们将原样保留该脚本,稍后再回来查看。
主画面场景
创建一个新的场景,其根节点由 Control
派生而来(在这个示例插件中,我们将使根节点为 CenterContainer
)。选择这个根节点,在视口中,点击 布局
菜单,选择 整个矩形
。你还需要在检查器中启用 Expand
垂直尺寸标志。面板现在使用主视口中的所有可用空间。
接下来, 让我们为我们的主屏幕插件示例添加一个按钮. 添加一个 Button
节点, 并将文本设置为 "Print Hello "或类似的内容. 给按钮添加一个脚本, 像这样:
@tool
extends Button
func _on_print_hello_pressed():
print("Hello from the main screen plugin!")
using Godot;
[Tool]
public partial class PrintHello : Button
{
private void OnPrintHelloPressed()
{
GD.Print("Hello from the main screen plugin!");
}
}
然后将 "按下" 信号连接到自身. 如果你需要信号方面的帮助, 请参考 使用信号 一文.
我们完成了主屏幕面板. 将场景保存为 main_panel.tscn
.
更新插件脚本
我们需要更新 main_screen_plugin.gd
脚本,让插件实例化我们的主面板场景,并将其放置在需要的位置。这是完整的插件脚本:
@tool
extends EditorPlugin
const MainPanel = preload("res://addons/main_screen/main_panel.tscn")
var main_panel_instance
func _enter_tree():
main_panel_instance = MainPanel.instantiate()
# Add the main panel to the editor's main viewport.
EditorInterface.get_editor_main_screen().add_child(main_panel_instance)
# Hide the main panel. Very much required.
_make_visible(false)
func _exit_tree():
if main_panel_instance:
main_panel_instance.queue_free()
func _has_main_screen():
return true
func _make_visible(visible):
if main_panel_instance:
main_panel_instance.visible = visible
func _get_plugin_name():
return "Main Screen Plugin"
func _get_plugin_icon():
# Must return some kind of Texture for the icon.
return EditorInterface.get_editor_theme().get_icon("Node", "EditorIcons")
#if TOOLS
using Godot;
[Tool]
public partial class MainScreenPlugin : EditorPlugin
{
PackedScene MainPanel = ResourceLoader.Load<PackedScene>("res://addons/main_screen/main_panel.tscn");
Control MainPanelInstance;
public override void _EnterTree()
{
MainPanelInstance = (Control)MainPanel.Instantiate();
// Add the main panel to the editor's main viewport.
EditorInterface.Singleton.GetEditorMainScreen().AddChild(MainPanelInstance);
// Hide the main panel. Very much required.
_MakeVisible(false);
}
public override void _ExitTree()
{
if (MainPanelInstance != null)
{
MainPanelInstance.QueueFree();
}
}
public override bool _HasMainScreen()
{
return true;
}
public override void _MakeVisible(bool visible)
{
if (MainPanelInstance != null)
{
MainPanelInstance.Visible = visible;
}
}
public override string _GetPluginName()
{
return "Main Screen Plugin";
}
public override Texture2D _GetPluginIcon()
{
// Must return some kind of Texture for the icon.
return EditorInterface.Singleton.GetEditorTheme().GetIcon("Node", "EditorIcons");
}
}
#endif
添加了几行特定的代码。MainPanel
是一个保存对场景的引用的常量,我们将该场景实例化到 main_panel_instance 中。
_enter_tree()
函数在 _ready()
之前被调用。在这里我们实例化主面板场景,并将它们添加为编辑器特定部分的子项。我们使用 EditorInterface.get_editor_main_screen()
来获取主编辑器屏幕,并将我们的主面板实例添加为其子项。我们调用 _make_visible(false)
函数来隐藏主面板,这样它在首次激活插件时就不会争夺空间。
当插件停用时,将调用 _exit_tree()
函数。如果主屏幕仍然存在,我们将调用 queue_free()
来释放实例,并将其从内存中移除。
可重写 _make_visible()
函数以根据需要隐藏或显示主面板。当用户点击编辑器顶部的主视口按钮时,编辑器会自动调用该函数。
_get_plugin_name()
和 _get_plugin_icon()
函数控制插件主视口按钮的显示名称和图标。
另一个你可以添加的函数是 handles()
函数, 它允许你处理一个节点类型, 当选择该类型时自动聚焦主屏幕. 这类似于点击一个3D节点会自动切换到3D视口.
试试这个插件
在项目设置中激活插件. 你会观察到主视口上方的2D, 3D, 脚本旁边有一个新的按钮. 点击它将带你进入新的主屏幕插件, 中间的按钮将打印文本.
如果你想试试这个插件的完成版, 请在这里查看插件演示:https://github.com/godotengine/godot-demo-projects/tree/master/plugins
如果你想看一个更完整的例子, 了解主屏幕插件的能力, 请看这里的2.5D演示项目:https://github.com/godotengine/godot-demo-projects/tree/master/misc/2.5d