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.

制作主螢幕外掛程式

本教學涵蓋的內容

主畫面外掛可讓你在編輯器中央區域建立新的介面,會出現在「2D」、「3D」、「Script」、「Game」與「AssetLib」按鈕旁。這類編輯器外掛稱為「主畫面外掛」(Main screen plugins)。

本教學將帶領你建立一個基本的主場景外掛程式. 為了簡單起見, 主場景外掛程式將包含一個列印文字到控制台的單個按鈕.

初始化外掛程式

首先從Plugins功能表中建立一個新外掛程式. 在本教學中, 我們將把它放在一個名為 main_screen 的資料夾中, 但你可以使用任何你喜歡的名字.

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

這個腳本中重要的部分是 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!")

然後將 "按下" 訊號連接到自身. 如果你需要訊號方面的説明, 請參考 繫結訊號 一文.

我們完成了主螢幕面板. 將場景保存為 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")

增加了幾行具體的內容. 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