在 VS Code 中使用 C# Dev Kit 和 Unity Tools 调试 Unity 2022
核心思路是:安装微软官方的 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 |
1. 安装 VS Code 扩展
在 VS Code 扩展商店搜索并安装 Unity (安装这个就够了),如果Unity包含的内容已经安装过,最好卸载,以免冲突。(发布者:Microsoft)。
这会一次性自动安装:
-
Unity(调试器、Unity 专属分析器、Shader/UXML 等着色)
-
C# Dev Kit(项目管理、IntelliSense、Solution Explorer)
-
C#(语言服务)
-
.NET Install Tool
2. Unity 端配置
-
打开 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包已被废弃,可以卸载。
二、启动调试
方式 1:直接 F5(推荐)
安装 Unity 扩展后,项目会自动生成调试配置。直接按 F5,VS Code 会附加到当前打开的 Unity Editor 实例。



生成 launch.json,记住一定要在 extension中选中 unity,然后在点 run -> Add Configuration才会正确生成,否则不会生成正确。 launch.json内容如下:
{ // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "Unity Debugger", "type": "vstuc", "request": "attach", "projectPath": "${workspaceFolder}" } ] }

记住一定要先启动 VSCode的调试,然后在运行 unity.


方式 2:Attach Unity Debugger 命令
按
Ctrl+Shift+P → 输入 Attach Unity Debugger → 选择要附加的 Unity Editor 或 Player 实例。
方式 3:手动配置 launch.json
如需调试远程 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" } ] }
三、常见问题排查
1. 断点显示"未验证"(灰色空心)
C# Dev Kit 3.x 对项目格式要求更严格。如果 Unity 生成的是旧格式
.sln,C# Dev Kit 的项目系统可能崩溃。解决:在 Unity 中点击 Regenerate Project Files,重新打开 VS Code。新生成的
.slnx(SDK 风格解决方案)可被 C# Dev Kit 正确解析。
2. 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
3. 缺少 .NET 10 Runtime
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。
4. 调试 Player(真机/独立程序)
构建 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 运行场景,即可命中断点进行单步调试。

浙公网安备 33010602011771号