













核心思路是:安装微软官方的 Unity 扩展(会自动拉取 C# Dev Kit 等依赖)→ 在 Unity 中关联 VS Code → 直接 F5 附加调试。以下是详细步骤和常见问题排查。
| 组件 | 要求 |
|---|---|
| Unity | 2021 LTS 或更高(Unity 2022 满足) |
| VS Code | 最新稳定版 |
| .NET Runtime | .NET 10 SDK / Runtime(C# Dev Kit 3.x 必需) |
| Unity 包 | Visual Studio Editor ≥ 2.0.20 |
在 VS Code 扩展商店搜索并安装 Unity(发布者:Microsoft)。
这会一次性自动安装:
Unity(调试器、Unity 专属分析器、Shader/UXML 等着色)
C# Dev Kit(项目管理、IntelliSense、Solution Explorer)
C#(语言服务)
.NET Install Tool
打开 Unity → Edit → Preferences → External Tools
External Script Editor 选择 Visual Studio Code
点击 Regenerate Project Files(重新生成项目文件)
注意:确保 Package Manager 中的
Visual Studio Editor包版本 ≥ 2.0.20。旧版的Visual Studio Code Editor包已被废弃,可以卸载。
安装 Unity 扩展后,项目会自动生成调试配置。直接按 F5,VS Code 会附加到当前打开的 Unity Editor 实例。
按 Ctrl+Shift+P → 输入 Attach Unity Debugger → 选择要附加的 Unity Editor 或 Player 实例。
如需调试远程 Player(如手机、主机),可在 .vscode/launch.json 中添加:
{ "version": "0.2.0", "configurations": [ { "name": "Attach to Unity", "type": "vstuc", "request": "attach" }, { "name": "Attach to Player", "type": "vstuc", "request": "attach", "endPoint": "127.0.0.1:56321" } ] }
C# Dev Kit 3.x 对项目格式要求更严格。如果 Unity 生成的是旧格式 .sln,C# Dev Kit 的项目系统可能崩溃。
解决:在 Unity 中点击 Regenerate Project Files,重新打开 VS Code。新生成的 .slnx(SDK 风格解决方案)可被 C# Dev Kit 正确解析。
检查 VS Code 输出面板(Ctrl+Shift+U → 右上角选 C# 开发工具包),若看到:
C# Dev Kit extension is disabled because setting 'dotnet.preferCSharpExtension' is set to 'true'
解决:在 .vscode/settings.json 中设置:
"dotnet.preferCSharpExtension": false
C# Dev Kit 3.x 后台服务基于 .NET 10。如果报错 You must install or update .NET to run this application,请下载安装:
.NET 10 Runtime
ASP.NET Core 10 Runtime(C# Dev Kit 的项目解析服务依赖它)
安装后执行 dotnet --list-runtimes 确认存在 Microsoft.AspNetCore.App 10.0.x。
构建 Player 前,在 Unity 中勾选:
File → Build Settings → ✅ Development Build
✅ Script Debugging
(可选)✅ Wait For Managed Debugger(让 Player 启动后等待调试器连接)
□ Unity 2022 已安装 □ VS Code 已安装 Unity 扩展(含 C# Dev Kit) □ Unity → Edit → Preferences → External Tools → 选择 VS Code □ Visual Studio Editor 包 ≥ 2.0.20 □ 已点击 Regenerate Project Files □ .NET 10 Runtime + ASP.NET Core 10 Runtime 已安装 □ dotnet.preferCSharpExtension 未设为 true □ 按 F5 启动调试
完成以上步骤后,在 VS Code 中打开 C# 脚本设置断点,回到 Unity 运行场景,即可命中断点进行单步调试。
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。