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...
制作主螢幕外掛程式
本教學涵蓋的內容
主畫面外掛可讓你在編輯器中央區域建立新的介面,會出現在「2D」、「3D」、「Script」、「Game」與「AssetLib」按鈕旁。這類編輯器外掛稱為「主畫面外掛」(Main screen plugins)。
本教學將帶領你建立一個基本的主場景外掛程式. 為了簡單起見, 主場景外掛程式將包含一個列印文字到控制台的單個按鈕.
初始化外掛程式
首先從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 。
在呼叫 ` _ready() ` 之前先呼叫 ` _enter_tree() ` 函式。在此處,我們實例化主面板場景,並將其作為子元素新增到編輯器的特定部分中。我們使用 ` EditorInterface.get_editor_main_screen() ` 取得主編輯器介面,然後將主面板實例作為子元素新增到該介面中。最後,呼叫 ` _make_visible (false) ` 函式隱藏主面板,以避免在首次啟用外掛時因介面空間衝突而產生問題。
當外掛程式停用時, 呼叫 _exit_tree() 函式. 如果主螢幕仍然存在, 我們呼叫 queue_free() 來釋放實例, 並將其從記憶體中移除.
make_visible() 函式被重寫, 以根據需要隱藏或顯示主面板. 當使用者點擊編輯器頂部的主視口按鈕時, 編輯器會自動呼叫該函式.
get_plugin_name() 和 get_plugin_icon() 函式控制外掛程式主視口按鈕的顯示名稱和圖示.
您還可以新增的另一個功能是 _handles() 功能,該功能允許您處理特定節點型別——當選擇該型別時,系統將自動切換至主螢幕。這類似於點擊 3D 節點後會自動切換到 3D 檢視的操作方式。
主螢幕圖示
您可以使用編輯器中內建的圖示之一,或為主螢幕外掛自訂圖示;無論採用哪種方式,均需通過重寫外掛腳本中的 ` _get_plugin_icon() ` 方法來實現。
如需使用內建圖示,請從 Godot editor icons 官方網站複製圖示名稱。請將從網站複製的名稱作為 EditorInterface.get_editor_theme().get_icon() 的 第一個 參數(第二個參數應保持為 "EditorIcons")。
您可以通過回傳諸如 preload(“res://addons/main_screen/icon.svg”). 等內容來使用自訂圖示。在設計自訂圖示時,應遵循與節點圖示相同的使用規範(推薦採用 SVG 格式,尺寸為 16×16 像素)。有關如何為您的外掛建立圖示的資訊,請參閱 編輯器圖示 。
試試這個外掛程式
在專案設定中啟動外掛程式. 你會觀察到主視口上方的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