Up to date
This page is up to date for Godot 4.2
.
If you still find outdated information, please open an issue.
使用 .NET 编译¶
需求¶
-
You can use
dotnet --info
to check which .NET SDK versions are installed.
启用 .NET 模块¶
备注
C# support for Godot has historically used the
Mono runtime instead of the
.NET Runtime and internally
many things are still named mono
instead of dotnet
or
otherwise referred to as mono
.
By default, the .NET module is disabled when building. To enable it, add the
option module_mono_enabled=yes
to the SCons command line, while otherwise
following the instructions for building the desired Godot binaries.
生成胶水代码¶
Parts of the sources of the managed libraries are generated from the ClassDB.
These source files must be generated before building the managed libraries.
They can be generated by any .NET-enabled Godot editor binary by running it with
the parameters --headless --generate-mono-glue
followed by the path to an
output directory.
This path must be modules/mono/glue
in the Godot directory:
<godot_binary> --headless --generate-mono-glue modules/mono/glue
This command will tell Godot to generate the C# bindings for the Godot API at
modules/mono/glue/GodotSharp/GodotSharp/Generated
, and the C# bindings for
the editor tools at modules/mono/glue/GodotSharp/GodotSharpEditor/Generated
.
Once these files are generated, you can build Godot's managed libraries for all
the desired targets without having to repeat this process.
<godot_binary>
refers to the editor binary you compiled with the .NET module
enabled. Its exact name will differ based on your system and configuration, but
should be of the form bin/godot.<platform>.editor.<arch>.mono
, e.g.
bin/godot.linuxbsd.editor.x86_64.mono
or
bin/godot.windows.editor.x86_32.mono.exe
. Be especially aware of the
.mono suffix! If you've previously compiled Godot without .NET support, you
might have similarly named binaries without this suffix. These binaries can't be
used to generate the .NET glue.
备注
每次ClassDB注册的API更改时, 都必须重新生成胶水源码. 即, 例如, 当将新方法注册到脚本API时, 或该方法的参数之一发生更改时. 如果ClassDB和胶水源码之间的API不匹配,Godot将在启动时打印一条错误.
构建托管库¶
Once you have generated the .NET glue, you can build the managed libraries with
the build_assemblies.py
script:
./modules/mono/build_scripts/build_assemblies.py --godot-output-dir=./bin
如果一切正常, 除了正常的输出,SCons应该在 bin
目录中创建了以下文件。
备注
By default, all development builds share