一、准备依赖和源码 #
本机使用 Ubuntu 26.10、GNOME Wayland 和 Radeon 880M,已有 Git、Python 3、build-essential、Clang 22、.NET SDK 11 和 Rider。构建 Godot 4.7.2 还需安装:
sudo apt install --no-install-recommends scons pkgconf libwayland-bin lld
mkdir -p ~/src
git clone --filter=blob:none --branch 4.7.2-stable \
https://github.com/godotengine/godot.git ~/src/godot
cd ~/src/godot
保留默认的 Wayland 和 X11 后端。Godot 4.7.2 默认使用自带头文件,运行时加载系统库,这套配置可以省去 X11、Mesa 和音频库的开发包。libwayland-bin 提供 wayland-scanner,缺少它时构建脚本会关闭 Wayland 支持。Linux 构建检测源码
二、用 Clang、lld 和 ThinLTO 编译 .NET 编辑器 #
引擎主体按 x86-64-v3 编译。Godot 内置的 Embree 只编入部分求交实现,全局启用 AVX2 会导致链接时缺少 BVHN<4> 等符号。在 modules/raycast/SCsub 的 env_thirdparty = env_raycast.Clone() 后加两行,单独限制 Embree 的指令集:
env_thirdparty = env_raycast.Clone()
if env["platform"] == "linuxbsd" and env["arch"] == "x86_64":
env_thirdparty.Append(CXXFLAGS=["-march=x86-64-v2"])
在源码根目录用 Bash 执行。module_mono_enabled=yes 是 Godot 4 启用 C# 的构建开关。target=editor 默认用 -O2,-j20 按本机的 20 个线程设置。
build_args=(
platform=linuxbsd target=editor module_mono_enabled=yes
use_llvm=yes linker=lld lto=thin
cflags=-march=x86-64-v3 cxxflags=-march=x86-64-v3
)
exe=godot.linuxbsd.editor.x86_64.llvm.mono
PATH="/usr/lib/llvm-22/bin:$PATH" scons "${build_args[@]}" -j20 &&
"./bin/$exe" --headless --generate-mono-glue modules/mono/glue &&
python3 modules/mono/build_scripts/build_assemblies.py --godot-output-dir=./bin
三步分别生成原生编辑器、匹配该引擎的 C# API 绑定,以及 bin/GodotSharp/ 下的托管程序集。首次构建程序集时需要访问 NuGet。Godot .NET 构建文档
三、安装到用户目录 #
把可执行文件和 GodotSharp/ 放在同一版本目录,终端和 Rider 共用 ~/.local/bin/godot-dotnet:
root="$HOME/.local/opt/godot-dotnet"
build=4.7.2-stable-clang-v3
mkdir -p "$root/$build" "$HOME/.local/bin" &&
cp -a "bin/$exe" bin/GodotSharp "$root/$build/" &&
ln -sfnT "$build" "$root/current" &&
ln -sfnT "$root/current/$exe" "$HOME/.local/bin/godot-dotnet"
完成安装并创建示例项目后,用户目录里与 Godot 有关的路径如下:
~/
├── src/godot/ # 引擎源码
│ ├── godot.sln # Rider 的引擎解决方案
│ └── bin/ # 编译产物
│ ├── godot.linuxbsd.editor.x86_64.llvm.mono
│ ├── GodotSharp/
│ └── obj/ # 增量编译用的目标文件
├── .local/
│ ├── bin/godot-dotnet -> ~/.local/opt/godot-dotnet/current/godot.linuxbsd.editor.x86_64.llvm.mono
│ └── opt/godot-dotnet/
│ ├── current -> 4.7.2-stable-clang-v3
│ └── 4.7.2-stable-clang-v3/ # 已安装的版本
│ ├── godot.linuxbsd.editor.x86_64.llvm.mono
│ └── GodotSharp/
└── RiderProjects/ForwardLab/ # 游戏项目
├── project.godot
└── ForwardLab.sln # Rider 的游戏解决方案
以后更新时,先保存 Embree 的本地补丁,切到新标签后检查是否仍需应用。重新编译并复制到新的 build 目录,切换 current 即可。回退到本文版本用 ln -sfnT 4.7.2-stable-clang-v3 "$root/current"。
四、创建 Forward+ 项目,验证 Vulkan 和 C# #
在新目录写入 project.godot,然后通过 Wayland 打开项目:
mkdir -p ~/RiderProjects
project="$HOME/RiderProjects/ForwardLab"
mkdir "$project" &&
cat > "$project/project.godot" <<'EOF'
config_version=5
[application]
config/name="ForwardLab"
config/features=PackedStringArray("4.7", "Forward Plus")
[rendering]
renderer/rendering_method="forward_plus"
EOF
~/.local/bin/godot-dotnet --editor --display-server wayland --path "$project"
在左侧点 3D 场景,把根节点改名为 Main,按 Ctrl+S 保存为 Main.tscn。右键根节点,选 附加脚本 → C#,保存为 Main.cs。Godot 会同时生成 ForwardLab.csproj 和 ForwardLab.sln。
using Godot;
namespace ForwardLab;
public partial class Main : Node3D
{
public override void _Ready()
{
GD.Print($"渲染器:{RenderingServer.GetCurrentRenderingMethod()}");
GD.Print($"显卡:{RenderingServer.GetVideoAdapterName()}");
}
}
按 F6 运行当前场景,本机输出如下。首次按 F5 时选 当前场景,即可将 Main.tscn 设为项目入口。
Vulkan 1.4.354 - Forward+ - Using Device #0: AMD - AMD Radeon 880M Graphics (RADV STRIX1)
渲染器:forward_plus
显卡:AMD Radeon 880M Graphics (RADV STRIX1)
单独启动 --project-manager 时看到 OpenGL 属正常情况。4.7.2 项目管理器默认使用 Compatibility,具体游戏使用的后端看游戏启动日志。项目管理器的渲染器选择
五、在 Rider 里打开游戏和引擎源码 #
- 写游戏。 在 Godot 的 编辑器 → 编辑器设置 → Dotnet → Editor → External Editor 选择 JetBrains Rider。本机自动识别到
/snap/rider/current/bin/rider.sh。在 Rider 打开~/RiderProjects/ForwardLab/ForwardLab.sln,用Player配置运行或调试。要让运行配置跟随引擎更新,可以复制一份Player,将可执行文件设为~/.local/bin/godot-dotnet展开后的绝对路径。 - 顺手查引擎。 在 Rider 的 Explorer 右键游戏解决方案,选 Add → Existing Folder,附加
~/src/godot。这样可以在游戏窗口里搜索 C++ 实现。 - 开发引擎。 在源码根目录用与实际构建相同的参数生成引擎自己的
godot.sln,在 Rider 里选editor | x64。以下命令复用第二节的build_args,只生成工程文件。
cd ~/src/godot
PATH="/usr/lib/llvm-22/bin:$PATH" scons "${build_args[@]}" \
vsproj=yes vsproj_gen_only=yes
rider ~/src/godot/godot.sln
如果已按 Rider 提示生成过工程,可以在上述 SCons 命令追加 vsproj_props_only=yes。检查 godot.linuxbsd.editor.x86_64.generated.props 里的 NMakeOutput,本次构建应指向 bin/godot.linuxbsd.editor.x86_64.llvm.mono。Godot 的 Rider 配置文档
本次验证到编辑器内运行 C# 场景。后续打包时,在 4.7.2 下载页 选择同版本的 .NET 导出模板。使用官方模板时,本机编辑器的 -march=x86-64-v3 不会传给玩家的可执行文件。