如何编译Godot(Godot & Godot with C#)

要在Windows下编译Godot, 需要以下环境:

  • Visual Studio Community:使用最新版本。

  • MinGW-w64:可以替代 Visual Studio。请务必将其安装/配置为使用 posix 线程模型。使用 MinGW 编译主分支时,需要 GCC 9 或更高版本。

  • Python 3.6+:确保在安装程序中启用将 Python 添加到环境变量中。

  • SCons 3.0+:构建系统。建议使用最新版本,特别是为了正确支持最近发布的 Visual Studio。

为了方便起见,建议使用 scoop 安装所需的软件,因为它会默认将软件的路径配置到环境变量,相较于手动安装更省事一些。

安装scoop

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
Invoke-RestMethod -Uri https://get.scoop.sh | Invoke-Expression

为 Scoop 设置代理

可选:如果你的scoop无法正常下载应用

# scoop config proxy ip:port
scoop config proxy 127.0.0.1:7890

安装环境

scoop install gcc python scons make mingw

安装 scons

python -m pip install scons

编译 Godot

下载源代码

git clone https://github.com/godotengine/godot.git

使用 scons 编译源代码

scons platform=windows

等待编译完成后,可执行文件可在 godot/bin/ 目录下看到。

godot
└─bin
└─godot.windows.editor.x86_64.console.exe
└─godot.windows.editor.x86_64.exe
└─godot.windows.editor.x86_64.exp
└─godot.windows.editor.x86_64.lib

编译 Godot with C#

使用源代码构建出 mono 版本的godot需要安装 .NET SDK

下载源代码

git clone https://github.com/godotengine/godot.git

使用 scons 编译源代码

若要启用 godotc# 支持,需要在构建时添加 module_mono_enabled=yes 命令。

scons platform=windows module_mono_enabled=yes

命令执行后,godot的文件目录结构应如下所示。

godot
└─bin
└─godot.windows.editor.x86_64.mono.console.exe
└─godot.windows.editor.x86_64.mono.exe
└─godot.windows.editor.x86_64.mono.exp
└─godot.windows.editor.x86_64.mono.lib

但此时的 godot mono 还无法运行,我们需要为其添加绑定。

使用 godot mono 生成 .Net glue

在 godot 目录下输入以下命令。

该命令将指示 Godot 在 godot/modules/mono/glue/GodotSharp/GodotSharp/Generated 目录下生成 Godot API 的 C# 绑定文件,并在 godot/modules/mono/glue/GodotSharp/GodotSharpEditor/Generated 目录下生成编辑器工具的 C# 绑定文件。

bin/godot.windows.editor.x86_64.mono --headless --generate-mono-glue modules/mono/glue

使用python脚本依照glue生成托管库

生成 .NET glue 后,可以使用脚本生成托管库。

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

命令执行后,bin/ 目录下应该会生成出一个 GodotSharp 目录。

有了它,godot mono 就可以使用 c# 开发项目了。

godot
└─bin
└─GodotSharp/
└─godot.windows.editor.x86_64.mono.console.exe
└─godot.windows.editor.x86_64.mono.exe
└─godot.windows.editor.x86_64.mono.exp
└─godot.windows.editor.x86_64.mono.lib

参考文章

Scoop

Scoop Proxy

Godot Docs:Compiling for windows

Godot Docs:Compiling with dotnet