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.
Checking the stable version of the documentation...
Création de plugins pour l'écran principal
Ce que couvre ce tutoriel
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".
Ce tutoriel vous guide à travers la création d'un plugin de base pour l'écran principal. Par souci de simplicité, notre plugin pour l'écran principal contiendra un seul bouton qui imprimera du texte sur la console.
Initialisation du plugin
Créez d'abord un nouveau plugin à partir du menu Plugins. Pour ce tutoriel, nous le placerons dans un dossier appelé main_screen, mais vous pouvez utiliser le nom que vous souhaitez.
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")
#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
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.
Scène de l'écran principal
Créer une nouvelle scène avec un nœud racine dérivé de Control (pour cet exemple de plugin, nous allons faire du nœud racine un CenterContainer). Sélectionnez ce nœud racine, et dans la fenêtre d'affichage, cliquez sur le menu Layout et sélectionnez Full Rect. Vous devez également activer le drapeau de taille verticale Expand dans l'inspecteur. Le panneau utilise maintenant tout l'espace disponible dans la fenêtre d'affichage principale.
Ensuite, ajoutons un bouton à notre exemple de plugin pour l'écran principal. Ajoutez un nœud Button et définissez le texte comme "Print Hello" ou similaire. Ajoutez un script au bouton comme ceci :
@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!");
}
}
Ensuite, connectez le signal "pressed" à lui-même. Si vous avez besoin d'aide pour les signaux, consultez l'article Utiliser les signaux.
Nous avons terminé pour le panneau de l'écran principal. Enregistrez la scène sous le nom main_panel.tscn.
Mettre à jour le script du plugin
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")
#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
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.
La fonction _exit_tree() est appelée lorsque le plugin est désactivé. Si l'écran principal existe toujours, nous appelons la fonction queue_free() pour libérer l'instance et la supprimer de la mémoire.
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.
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 Icônes de l'éditeur for information on how to create icons
for your plugin.
Essayez le plugin
Activez le plugin dans les Paramètres du projet. Vous observerez un nouveau bouton à côté de 2D, 3D, Script au-dessus de la fenêtre principale. En cliquant dessus, vous accéderez à l'écran principal de votre nouveau plugin, et le bouton du milieu imprimera du texte.
Si vous souhaitez essayer une version finalisée de ce plugin, consultez les démos du plugin ici : https://github.com/godotengine/godot-demo-projects/tree/master/plugins
Si vous souhaitez voir un exemple plus complet de ce dont sont capables les plugins d'écran principal, consultez les projets de démonstration 2.5D ici : https://github.com/godotengine/godot-demo-projects/tree/master/misc/2.5d