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.

Erstellen von Plugins für den Hauptbildschirm

Was dieses Tutorial abdeckt

Main screen plugins allow you to create new UIs in the central part of the editor, which appear next to the "2D", "3D", "Script", "Game", and "AssetLib" buttons. Such editor plugins are referred as "Main screen plugins".

Dieses Tutorial führt Sie durch die Erstellung eines grundlegenden Plugins für den Hauptbildschirm. Der Einfachheit halber enthält unser Hauptbildschirm-Plugin einen einzigen Button, mit dem Text auf die Konsole gedruckt wird.

Initialisierung des Plugins

Erstellen Sie zunächst ein neues Plugin aus dem Menü Plugins. Für dieses Tutorial legen wir es in einem Ordner mit dem Namen main_screen ab, aber Sie können jeden beliebigen Namen verwenden, den Sie möchten.

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 four 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 EditorInterface.get_editor_theme().get_icon("Node", "EditorIcons")

The important part in this script is the _has_main_screen() function, which is overridden to return 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.

Hauptbildschirm-Szene

Erstellen Sie eine neue Szene mit einem Root Node, das von Control abgeleitet ist (für dieses Beispiel-Plugin werden wir den Root-Node zu einem CenterContainer machen). Wählen Sie diesen Root-Node aus, klicken Sie im Viewport auf das Menü Layout und wählen Sie Full Rect. Man muss auch das Expand-Flag für die vertikale Größe im Inspektor aktivieren. Das Panel nutzt nun den gesamten verfügbaren Platz im Hauptfenster.

Als nächstes fügen wir einen Button zu unserem Beispiel-Hauptbildschirm-Plugin hinzu. Fügen Sie einen Button-Node hinzu, und setzen Sie den Text auf "Print Hello" oder ähnlich. Fügen Sie dem Button ein Skript wie dieses hinzu:

@tool
extends Button


func _on_print_hello_pressed():
    print("Hello from the main screen plugin!")

Verbinden Sie dann das "pressed"-Signal mit sich selbst. Wenn Sie Hilfe zu Signalen benötigen, lesen Sie den Artikel Signale nutzen.

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 instantiates 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.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")

A couple of specific lines were added. MainPanel is a constant that holds a reference to the scene, and we instantiate it into main_panel_instance.

The _enter_tree() function is called before _ready(). This is where we instantiate the main panel scene, and add them as children of specific parts of the editor. We use EditorInterface.get_editor_main_screen() to obtain the main editor screen 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.

Die Funktion _exit_tree() wird aufgerufen, wenn das Plugin deaktiviert wird. Wenn der Hauptbildschirm noch existiert, rufen wir queue_free() auf, um die Instanz freizugeben und aus dem Speicher zu entfernen.

Die Funktion _make_visible() wird überschrieben, um das Hauptfenster je nach Bedarf ein- oder auszublenden. Diese Funktion wird automatisch vom Editor aufgerufen, wenn der Benutzer auf den Button des Hauptfensters am oberen Rand des Editors klickt.

Die Funktionen _get_plugin_name() und _get_plugin_icon() steuern den angezeigten Namen und das Icon für den Haupt-Viewport-Button des Plugins.

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.

Main screen icons

You can either use one of the built-in icons from the editor, or provide your own icon for the main screen plugin. In both cases, this is done by overriding the _get_plugin_icon() method in the plugin script.

To use a built-in icon, copy an icon name from the Godot editor icons website. Use the name copied from the website as the first parameter of EditorInterface.get_editor_theme().get_icon() (the second parameter should remain "EditorIcons").

You can use a custom icon by returning something such as preload("res://addons/main_screen/icon.svg"). When designing your own icon, you should follow the same guidelines as for node icons (SVG format recommended, 16×16 size). See Editor-Icons for information on how to create icons for your plugin.

Testen des Plugins

Aktivieren Sie das Plugin in den Projekteinstellungen. Sie werden einen neuen Button neben 2D, 3D, Skript oberhalb des Hauptfensters sehen. Wenn Sie darauf klicken, gelangen Sie zu Ihrem neuen Hauptbildschirm-Plugin, und mit dem Button in der Mitte können Sie Text ausgeben.

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