Work in progress

The content of this page was not yet updated for Godot 4.2 and may be outdated. If you know how to improve this page or you can confirm that it's up to date, feel free to open a pull request.

制作主螢幕外掛程式

本教學涵蓋的內容

正如在 製作外掛程式 頁面中所看到的, 製作一個擴充編輯器的基本外掛程式是相當容易的. 主螢幕外掛程式允許你在編輯器的中心部分建立新的使用者介面, 它們出現在 "2D" , "3D" , "腳本" 和 "AssetLib" 按鈕旁邊. 這種編輯器外掛程式被稱為 "主場景外掛程式".

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

初始化外掛程式

首先從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")

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

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 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.

當外掛程式停用時, 呼叫 _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