【Unity】使用VSCode调试

发布时间 2023-12-18 13:45:45作者: 我爱我家喵喵

安装 2022.3.14f1c1 版本的 unity 编辑器并使用 VSCode 调试 c# 脚本。

  1. VSCode请使用最新版本 (1.85.1),并安装 Unity 插件最新版本 v0.9.3

  2. Unity 中配置外部编辑器,选择 Visual Studio Code(v1.85.1)。

  3. Unity 中双击一个 .cs 文件,可以在 vscode 中打开此文件。找到 vscode 中 .vscode 下的 settings.json 文件,在最低部添加以下内容:

...
    "dotnetAcquisitionExtension.existingDotnetPath": [
        {
            "extensionId": "ms-dotnettools.csharp",
            "path": "C:\\Program Files\\dotnet\\dotnet.exe"
        },
        {
            "extensionId": "visualstudiotoolsforunity.vstuc",
            "path": "C:\\Program Files\\dotnet\\dotnet.exe"
        },
        {
            "extensionId": "ms-dotnettools.csdevkit",
            "path": "C:\\Program Files\\dotnet\\dotnet.exe"
        }
    ]
...
  1. 此时已经能够在 vscode 中运行、启动调试了。

  2. 在 Unity 中指定场景切换到游戏(Game)页开始运行,可以正常调试了。如果修改了脚本,需要重新运行游戏。

  3. 示例脚本:(点击按钮改变 Text 内容)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class NewBehaviourScript : MonoBehaviour
{
    private int value = 10;

    public void clickTest()
    {
        value = value + 10;
        Debug.Log("点击了按钮:" + value);
        GameObject.Find("TestText").GetComponent<TMPro.TextMeshProUGUI>().text = value.ToString();
    }
}