Up to date

This page is up to date for Godot 4.2. If you still find outdated information, please open an issue.

通過 Mono 進行編譯

系統需求

  • .NET SDK 6.0+

    您可以使用「dotnet --info」來檢查安裝了哪些.NET SDK版本。

開啟 Mono 模組

備註

C# 對 Godot 的支援歷來使用 Mono 運作時而不是 .NET Runtime 並且在內部,許多東西仍然被命名為“mono”而不是“dotnet”或以其他方式稱為“mono”。

預設情況下,Mono 模組在建置時沒有被啟用。若要開啟 Mono 模組,請在 SCons 命令行加上 module_mono_enabled=yes 選項。

產生 Glue

建置完成後,需要使用 --generate-mono-glue 參數接上輸出路徑來執行編譯好的可執行檔。該路徑必須為 Godot 資料夾中的 modules/mono/glue

<godot_binary> --headless --generate-mono-glue modules/mono/glue

該指令會讓 Godot 產生 modules/mono/glue/mono_glue.gen.cpp 檔案,以及用於 Godot API 的 C# 解決方案,位於 modules/mono/glue/Managed/Generated 。產生好這些檔案後,就可以為所有需要的建置目標建置 Godot,而無需重複此一過程。

<godot_binary> 表示剛才啟用 Mono 模組編譯出來的 Tools 二進位檔。實際的名稱會依據作業系統與設定檔而定,但格式應該是 bin/godot.<平台>.tools.<位元>.mono ,如 bin/godot.x11.tools.64.monobin/godot.windows.tools.64.mono.exe 。但請特別注意最後的 mono !若剛才編譯 Godot 時沒有啟用 Mono 支援,則產生的二進位檔後面不會有 mono,且將無法用於產生 Mono Glue。

備註

每次更改註冊在 ClassDB 上的 API 時都必須重新產生 Glue 原始檔。這種情況如,向腳本 API 註冊新方法或其中一個向 ClassDB 註冊的方法需要更改參數時。若 ClassDB 與 Glue 原始檔註冊的 API 有出入,Godot 會在啟動時顯示錯誤。

繫結外部函式庫

產生 .NET 黏合後,您可以使用「build_assemblies.py」腳本建立託管庫:

./modules/mono/build_scripts/build_assemblies.py --godot-output-dir=./bin

如果沒遇到問題的話,除了一般的輸出之外,SCons 還會在 bin 資料夾中建立下列檔案:

備註

預設情況下,所有開發版本共用一個版本號,這可能會導致 NuGet 套件快取出現一些問題。要解決此問題,可以使用“GODOT_VERSION_STATUS”為每個建置提供唯一的版本,或在每個建置後刪除“GodotNuGetFallbackFolder”以清除套件快取。

與「經典」Godot 建置不同,當建置啟用了 Mono 模組 (並根據不同的目標平台),有可能會為編輯器與匯出樣板產生資料目錄。該資料夾對於能否正常使用 Godot 來說很重要,在發行 Godot 時必須一同發行。有關該資料夾的詳情請參考 資料目錄

支援平台

提供“--godot-platform=<platform>”參數來控制建置特定於哪個平台的函式庫。忽略此參數以針對目前系統進行建置。

目前,這僅控制對 Visual Studio 作為外部編輯器的支援,庫在其他方面是相同的。

在 Godot 中使用 NuGet 套件

API 組件、來源產生器和自訂 MSBuild 專案 SDK 以 NuGet 套件分發。這對使用者來說都是透明的,但它會使開發過程中的事情變得複雜。

為了將 Godot 與這些套件的開發版本一起使用,必須建立本機 NuGet 來源,MSBuild 可以在其中找到它們。這可以使用 .NET CLI 來完成:

dotnet nuget add source ~/MyLocalNugetSource --name MyLocalNugetSource

Godot NuGet 套件必須新增至該本機來源。此外,我們必須確保 NuGet 快取中沒有該套件的其他版本,因為 MSBuild 可能會選擇其中之一。

為了簡化此過程,「build_assemblies.py」腳本提供了以下「--push-nupkgs-local」選項:

./modules/mono/build_scripts/build_assemblies.py --godot-output-dir ./bin --push-nupkgs-local ~/MyLocalNugetSource

此選項可確保將套件新增至指定的本機 NuGet 來源,並從 NuGet 快取中刪除套件的衝突版本。建議在開發過程中建立 C# 解決方案時始終使用此選項以避免錯誤。

雙精度支援 (REAL_T_IS_DOUBLE)

當建立具有雙精度支援的 Godot 時,即 scons 的“ precision=double ”參數,必須透過傳遞“-- precision=double ”參數來調整託管庫以配對:

./modules/mono/build_scripts/build_assemblies.py --godot-output-dir ./bin --push-nupkgs-local ~/MyLocalNugetSource --precision=double

範例

範例 (Windows)

# Build editor binary
scons p=windows target=editor module_mono_enabled=yes
# Build export templates
scons p=windows target=template_debug module_mono_enabled=yes
scons p=windows target=template_release module_mono_enabled=yes

# Generate glue sources
bin/godot.windows.editor.x86_64.mono --headless --generate-mono-glue modules/mono/glue
# Build .NET assemblies
./modules/mono/build_scripts/build_assemblies.py --godot-output-dir=./bin --godot-platform=windows

範例 (Linux, *BSD)

# Build editor binary
scons p=linuxbsd target=editor module_mono_enabled=yes
# Build export templates
scons p=linuxbsd target=template_debug module_mono_enabled=yes
scons p=linuxbsd target=template_release module_mono_enabled=yes

# Generate glue sources
bin/godot.linuxbsd.editor.x86_64.mono --headless --generate-mono-glue modules/mono/glue
# Generate binaries
./modules/mono/build_scripts/build_assemblies.py --godot-output-dir=./bin --godot-platform=linuxbsd

資料目錄

資料目錄在 Godot 二進位檔編譯時啟用 Mono 目錄時的相依性。該資料夾包含了一些重要的檔案,且會影響 Godot 能否正確運作。在發行 Godot 執行檔時必須一同發行。

編輯器

Godot 編輯器的資料目錄名稱始終為「GodotSharp」。該目錄包含一個包含 Godot API 組件的「Api」子目錄和一個包含編輯器所需工具的「Tools」子目錄,例如「GodotTools」組件及其相依性。

在 macOS 上,如果 Godot 編輯器是作為組合包分發,則「GodotSharp」目錄可能會放置在組合包內的「<bundle_name>.app/Contents/Resources/」目錄中。

匯出樣板

該資料夾必須以其原始名稱與 Godot 匯出樣板放在一起。匯出專案時,Godot 也會將該資料夾複製到遊戲可執行檔旁,並將資料夾名稱改為 data_<APPNAME> ,其中, <APPNAME> 為在專案設定 application/config/name 中設定的應用程式名稱。

在多架構匯出的情況下,將產生多個此類資料目錄。

命令行選項

下列為在建置 Mono 模組時可用的命令行選項:

  • module_mono_enabled=yes | no

    • 開啟 Mono 模組來建置 Godot。