返回首页

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

Windows

要在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

MacOS

# Pre 安装依赖
brew install python molten-vk scons

# 步骤 1: 首次编译(不带 glue)
scons platform=macos arch=arm64 module_mono_enabled=yes mono_glue=no target=editor -j9

# 步骤 2: 生成 Mono glue 代码
./bin/godot.macos.editor.arm64.mono --headless --generate-mono-glue modules/mono/glue

# 步骤 3: 使用 build_assemblies.py 构建 C# 程序集
python3 modules/mono/build_scripts/build_assemblies.py \
    --godot-output-dir=./bin \           # 输出目录(必需)
    --godot-platform=macos               # 平台(必需)
    --no-deprecated \                    # 不包含已弃用的功能

# 步骤 4: 最终编译(带 glue)
scons platform=macos arch=arm64 module_mono_enabled=yes target=editor -j9

将代码打包为 dmg

#!/bin/bash

set -e

#==============================================
# 配置区域
#==============================================
BINARY_PATH="bin/godot.macos.editor.arm64.mono"
APP_NAME="Godot"
APP_VERSION="4.3.0"
BUNDLE_ID="org.godotengine. godot"
ARCH="arm64"
MIN_MACOS_VERSION="11.0"
COPYRIGHT="Copyright © 2014-2024 Godot Engine contributors"

#==============================================
# 开始打包
#==============================================
APP_BUNDLE="$APP_NAME.app"

echo "=========================================="
echo "打包 Godot 为 macOS . app"
echo "=========================================="

# 清理旧的包
if [ -d "$APP_BUNDLE" ]; then
    echo "清理旧的 .app 包..."
    rm -rf "$APP_BUNDLE"
fi

# 创建目录结构
echo "创建 .app 目录结构..."
mkdir -p "$APP_BUNDLE/Contents/MacOS"
mkdir -p "$APP_BUNDLE/Contents/Resources"
mkdir -p "$APP_BUNDLE/Contents/Frameworks"

# 检查二进制文件是否存在
if [ !  -f "$BINARY_PATH" ]; then
    echo "错误: 找不到二进制文件 $BINARY_PATH"
    exit 1
fi

# 复制二进制文件
echo "复制二进制文件..."
cp "$BINARY_PATH" "$APP_BUNDLE/Contents/MacOS/$APP_NAME"
chmod +x "$APP_BUNDLE/Contents/MacOS/$APP_NAME"

# 复制 GodotSharp(Mono 版本)
if [ -d "bin/GodotSharp" ]; then
    echo "复制 GodotSharp 程序集..."
    cp -r "bin/GodotSharp" "$APP_BUNDLE/Contents/Resources/"
fi

# 复制图标
echo "复制图标..."
ICON_FOUND=false
for icon_path in "icon.icns" "misc/dist/macos_template.app/Contents/Resources/icon.icns" "misc/dist/macos/editor_icon.icns"; do
    if [ -f "$icon_path" ]; then
        cp "$icon_path" "$APP_BUNDLE/Contents/Resources/$APP_NAME.icns"
        ICON_FOUND=true
        break
    fi
done

if [ "$ICON_FOUND" = false ]; then
    echo "警告: 未找到图标文件"
fi

# 创建 PkgInfo
echo "APPL??? ?" > "$APP_BUNDLE/Contents/PkgInfo"

# 创建 Info.plist
echo "创建 Info.plist..."
cat > "$APP_BUNDLE/Contents/Info.plist" << EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CFBundleDevelopmentRegion</key>
    <string>en</string>
    <key>CFBundleExecutable</key>
    <string>$APP_NAME</string>
    <key>CFBundleIconFile</key>
    <string>$APP_NAME</string>
    <key>CFBundleIdentifier</key>
    <string>$BUNDLE_ID</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundleName</key>
    <string>$APP_NAME</string>
    <key>CFBundlePackageType</key>
    <string>APPL</string>
    <key>CFBundleShortVersionString</key>
    <string>$APP_VERSION</string>
    <key>CFBundleVersion</key>
    <string>$APP_VERSION</string>
    <key>CFBundleSignature</key>
    <string>???? </string>
    <key>LSMinimumSystemVersion</key>
    <string>$MIN_MACOS_VERSION</string>
    <key>LSApplicationCategoryType</key>
    <string>public.app-category.developer-tools</string>
    <key>NSHumanReadableCopyright</key>
    <string>$COPYRIGHT</string>
    <key>NSHighResolutionCapable</key>
    <true/>
    <key>NSSupportsAutomaticGraphicsSwitching</key>
    <true/>
    <key>NSPrincipalClass</key>
    <string>NSApplication</string>
    <key>NSCameraUsageDescription</key>
    <string>Godot Engine requires camera access for camera-based features.</string>
    <key>NSMicrophoneUsageDescription</key>
    <string>Godot Engine requires microphone access for audio input features.</string>
    <key>NSDesktopFolderUsageDescription</key>
    <string>Godot Engine requires access to the Desktop folder to save and load project files.</string>
    <key>NSDocumentsFolderUsageDescription</key>
    <string>Godot Engine requires access to the Documents folder to save and load project files. </string>
    <key>NSDownloadsFolderUsageDescription</key>
    <string>Godot Engine requires access to the Downloads folder to save and load project files. </string>
</dict>
</plist>
EOF

# 代码签名
echo "代码签名..."
# 使用 ad-hoc 签名(用于本地开发)
codesign --force --deep --sign - "$APP_BUNDLE"

# 验证签名
echo "验证签名..."
codesign --verify --deep --strict --verbose=2 "$APP_BUNDLE"

# 创建 DMG(可选)
read -p "是否创建 DMG 文件?  (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
    DMG_NAME="$APP_NAME-$APP_VERSION-$ARCH.dmg"
    echo "创建 DMG:  $DMG_NAME"

    # 创建临时目录
    TMP_DIR=$(mktemp -d)
    cp -r "$APP_BUNDLE" "$TMP_DIR/"

    # 创建 DMG
    hdiutil create -volname "$APP_NAME" -srcfolder "$TMP_DIR" -ov -format UDZO "$DMG_NAME"

    # 清理
    rm -rf "$TMP_DIR"

    echo "✓ DMG 创建完成:  $DMG_NAME"
fi

echo ""
echo "=========================================="
echo "✓ 打包完成!"
echo "=========================================="
echo "应用程序:  $APP_BUNDLE"
echo ""
echo "运行应用程序:"
echo "  open $APP_BUNDLE"
echo ""
echo "或者双击 Finder 中的 $APP_BUNDLE"
echo "=========================================="

参考文章

Scoop

Scoop Proxy

Godot Docs:Compiling for windows

Godot Docs:Compiling with dotnet

相关文章