Eine einfache Erweiterung erstellen (Hauptbildschirm)¶
Was diese Anleitung abdeckt¶
As seen in the Erweiterungen (Plugins) erstellen page, making a basic plugin that extends the editor is fairly easy. Main screen plugins allow you to create new UIs in the central part of the editor, which appear next to the "2D", "3D", "Script", and "AssetLib" buttons. Such editor plugins are referred as "Main screen plugins".
Diese Anleitung führt Sie durch die Erstellung eines grundlegenden Plugins für den Hauptbildschirm. Der Einfachheit halber enthält unser Hauptbildschirm-Plugin eine einzige Schaltfläche, mit der Text auf die Konsole gedruckt wird.
Erweiterung initialisieren¶
Erstelle zuerst ein neue Erweiterung aus dem Erweiterungs-Menü. In dieser Anleitung speichern wir diese im Verzeichnis main_screen
, aber es kann auch ein anderer Name verwendet werden.
The plugin script will come with _enter_tree()
and _exit_tree()
methods, but for a main screen plugin we need to add a few extra methods.
Add five extra methods such that the script looks like this:
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 get_editor_interface().get_base_control().get_icon("Node", "EditorIcons")
The important part in this script is the has_main_screen()
function,
which is overloaded so it returns true
. This function is automatically
called by the editor on plugin activation, to tell it that this plugin
adds a new center view to the editor. For now, we'll leave this script
as-is and we'll come back to it later.
Hauptbildschirms-Szene¶
Create a new scene with a root node derived from Control
(for this
example plugin, we'll make the root node a CenterContainer
).
Select this root node, and in the viewport, click the Layout
menu
and select Full Rect
. You also need to enable the Expand
vertical size flag in the inspector.
The panel now uses all the space available in the main viewport.
Next, let's add a button to our example main screen plugin.
Add a Button
node, and set the text to "Print Hello" or similar.
Add a script to the button like this:
tool
extends Button
func _on_PrintHello_pressed():
print("Hello from the main screen plugin!")
Then connect the "pressed" signal to itself. If you need help with signals, see the Signale article.
Wir sind mit dem Hauptbildschirm fertig. Speichern Sie die Szene als main_panel.tscn
.
Aktualisieren des Plugin-Skripts¶
We need to update the main_screen_plugin.gd
script so the plugin
instances our main panel scene and places it where it needs to be.
Here is the full plugin script:
tool
extends EditorPlugin
const MainPanel = preload("res://addons/main_screen/main_panel.tscn")
var main_panel_instance
func _enter_tree():
main_panel_instance = MainPanel.instance()
# Add the main panel to the editor's main viewport.
get_editor_interface().get_editor_viewport().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 get_editor_interface().get_base_control().get_icon("Node", "EditorIcons")
A couple of specific lines were added. MainPanel
is a constant that holds
a reference to the scene, and we instance it into main_panel_instance.
The _enter_tree()
function is called before _ready()
. This is where
we instance the main panel scene, and add them as children of specific parts
of the editor. We use get_editor_interface().get_editor_viewport()
to
obtain the viewport and add our main panel instance as a child to it.
We call the make_visible(false)
function to hide the main panel so
it doesn't compete for space when first activating the plugin.
The _exit_tree()
function is called when the plugin is deactivated.
If the main screen still exists, we call queue_free()
to free the
instance and remove it from memory.
The make_visible()
function is overridden to hide or show the main
panel as needed. This function is automatically called by the editor when the
user clicks on the main viewport buttons at the top of the editor.
The get_plugin_name()
and get_plugin_icon()
functions control
the displayed name and icon for the plugin's main viewport button.
Another function you can add is the handles()
function, which
allows you to handle a node type, automatically focusing the main
screen when the type is selected. This is similar to how clicking
on a 3D node will automatically switch to the 3D viewport.
die Erweiterung testen¶
Aktiviere das Plugin in den Projekt Einstellungen. Es wird ein neuer Knopf erscheinen, neben dem 2D, 3D, Skript, über dem Haupt-Ansichtsfenster. Wird dieser gedrückt, wird die neue Hauptbildschirm Erweiterung gestartet und der Knopf in der Mitte gibt den Text aus.
Wenn Sie eine fertige Version dieses Plugins ausprobieren möchten, sehen Sie sich die Plugin-Demos hier an: https://github.com/godotengine/godot-demo-projects/tree/master/plugins
Wenn Sie ein vollständigeres Beispiel für die Funktionen von Hauptbildschirm-Plugins sehen möchten, sehen Sie sich die 2.5D Demo-Projekte hier an: https://github.com/godotengine/godot-demo-projects/tree/master/misc/ 2.5d