安装Unity编辑器

通过Unity hub安装最新的2022.2.9f1c1版本,安装时勾选开发工具 Visual Studio,

新建个项目,选择2D(URP)模板,(不要勾选启动版本管理

打开编辑器后,在菜单栏-edit--prederences-External Tools 右侧找到 External Script Editor 选择 Microsoft Vtudio 2022

新建个c#脚本并打开后,查看编辑器中是否提示Unity消息。如果没有连接,关闭代码编辑器,在Project里空白部分按右键-选择Open C# Project,这回帮助我们重新编译并打开整个项目,重新调用引用和命名空间,帮助我们解决不能成功连接的问题。

VS Code编辑器

VS Code推荐安装Unity相关插件

  • C#(在设置里搜索Use Modern Net并取消勾选)
  • Unity Code Snippets
  • Debugger for Unity
  • Unity Tools

其他常用插件

  • Chinese (Simplified) - 简体中文
  • Code Translate - 划词翻译
  • Doxygen Documentation Generator - 注释
  • Error Lens - 错误提示
  • indent-rainbow - 彩色缩进(可在设置中调整进使用线条)
  • Material Icon Theme - 图标主题
  • Remote - SSH
  • Draw.io Integration - Drawio
  • Image preview - 图片预览
  • Path Intellisense - 路径提示
  • Prettier - Code formatter - 格式化代码工具

VS Code没有代码提示 解决方法

检测是否打开工程目录

在VSCode资源管理器中查看VSCode是否已经打开整个项目文件。你可以查看VSCode左侧的资源管理器,查看列表当中是否有sln文件。 如果没有,打开Unity菜单Edit->Preferences->Extermal Tools中 点击Regenerate project files

验证工程是否指定正确

按下键盘的Ctrl + Shift+ P快捷键,选定工程目录下的sln文件。 至此,如果你以前是有代码提示的,稍等几秒等待自动加载完成以后你可以重试一下是否有代码提示功能。

检查和安装对应的.NET开发者版本

  1. 用VSCode打开你工程目录下的.csproj文件。
  2. 找到Project->PropertyGroup->TargetFrameworkVersion这条数据,查看版本
  3. 到这下载对应版本号的开发者版 .NET Framework

查看Unity菜单Edit->Preferences->Extermal ToolsExternal Script Editor是否为Visual Studio Code,如果不是,点击一下,找找有没有Visual Studio Code[...]这个选项

  • 情况1:没有 且你的版本低于2019,请选择 Browse...手动指定你的VSCode应用程序 – 随后在下一行会出现Extermal Script Editor Args这个选项,用于配置VSCode启动命令行 –它的默认参数应该是"$(ProjectPath)" -g "$(File)":$(Line):$(Column) – 如果不对点击下面的[Reset argument][Regenerate project files]按钮重置命令行和工程依赖文件,随后重开Unity
  • 情况2:如果没有 且你的版本高于2019,请在Package Manager中,搜索vis 找到Visual Studio Editor这个插件,看看是不是最新的 不是就升级,

随后在VSCode中安装新版Unity插件,同时它会自动安装C#C# Dev Kit这两个拓展依赖。C#C# Dev Kit依赖于.NET Install Tool拓展,它负责自动下载部署.NET依赖环境,VSCode打开以后,你可能会看到控制台输出弹出了正在下载.Net7.0.14的信息。

这个地址来自于Github,下载速度过慢可以手动下载

安装后关闭VSCode 然后在Unity中重新双击你的脚本文件 理论上这时候应该就不会弹出下载了,参阅解决VSC中使用Unity插件时“download the .NET Runtime.”的问题 摘要:根据开发者的回复,目前只能由用户自己配置VSC的setting.json。所以需要在工程目录下的.vscode/setting.json配置文件里加上这样的配置: 

"dotnetAcquisitionExtension.existingDotnetPath": [
  {
      "extensionId": "ms-dotnettools.csharp",
      "path": "C:\\Program Files\\dotnet\\dotnet.exe"
  },
  {
      "extensionId": "visualstudiotoolsforunity.vstuc",
      "path": "C:\\Program Files\\dotnet\\dotnet.exe"
  }
 ]

删除文件 sln 文件

  1. 工程下的**.csproj和.sln文件中记录着当前项目的脚本文件、程序集引用以及一些平台宏、版本信息(如果你用的IDE是VS的话那么还有一个.vs的隐藏文件夹),删除前先备份
  2. 删除工程中的**.csproj和.sln文件并不会对项目造成影响(除非你在这些文件中加入了你需要的更改)
  3. 可以通过重启项目来重新生成丢失的**.csproj和.sln文件,或者也可以通过以下方式刷新这些文件:①编辑器中"Editor->Preferences...->External Tools->Regenerate project files"刷新.csproj和.sln文件

The .NET Core SDK cannot be located 解决方法

检查是否安装 .NET SDK,终端输入: dotnet --list-sdks

解决方法:

  • 安装.NET最新版
  • 检查环境变量中是否有C:\Program Files\dotnet\
  • 删除C:\Program Files (x86)\dotnet\

下载.NET

https://dotnet.microsoft.com/zh-cn/download

代码格式化 大括号不换行 解决方法

  1. 在 C:\Users\xxx.omnisharp\ 目录下创建 omnisharp.json
  2. 粘贴下面内容到omnisharp.json
 {
    "//": "https://github.com/OmniSharp/omnisharp-roslyn/wiki/Configuration-Options",
    "FormattingOptions": {
        "EnableEditorConfigSupport": false,
        "OrganizeImports": true,
        "NewLine": "\n",
        "UseTabs": false,
        "TabSize": 4,
        "IndentationSize": 4,
        "SpacingAfterMethodDeclarationName": false,
        "SpaceWithinMethodDeclarationParenthesis": false,
        "SpaceBetweenEmptyMethodDeclarationParentheses": false,
        "SpaceAfterMethodCallName": false,
        "SpaceWithinMethodCallParentheses": false,
        "SpaceBetweenEmptyMethodCallParentheses": false,
        "SpaceAfterControlFlowStatementKeyword": true,
        "SpaceWithinExpressionParentheses": false,
        "SpaceWithinCastParentheses": false,
        "SpaceWithinOtherParentheses": false,
        "SpaceAfterCast": false,
        "SpacesIgnoreAroundVariableDeclaration": false,
        "SpaceBeforeOpenSquareBracket": false,
        "SpaceBetweenEmptySquareBrackets": false,
        "SpaceWithinSquareBrackets": false,
        "SpaceAfterColonInBaseTypeDeclaration": true,
        "SpaceAfterComma": true,
        "SpaceAfterDot": false,
        "SpaceAfterSemicolonsInForStatement": true,
        "SpaceBeforeColonInBaseTypeDeclaration": true,
        "SpaceBeforeComma": false,
        "SpaceBeforeDot": false,
        "SpaceBeforeSemicolonsInForStatement": false,
        "SpacingAroundBinaryOperator": "single",
        "IndentBraces": false,
        "IndentBlock": true,
        "IndentSwitchSection": true,
        "IndentSwitchCaseSection": true,
        "IndentSwitchCaseSectionWhenBlock": true,
        "LabelPositioning": "oneLess",
        "WrappingPreserveSingleLine": true,
        "WrappingKeepStatementsOnSingleLine": true,
        "NewLinesForBracesInTypes": false,
        "NewLinesForBracesInMethods": false,
        "NewLinesForBracesInProperties": false,
        "NewLinesForBracesInAccessors": false,
        "NewLinesForBracesInAnonymousMethods": false,
        "NewLinesForBracesInControlBlocks": false,
        "NewLinesForBracesInAnonymousTypes": false,
        "NewLinesForBracesInObjectCollectionArrayInitializers": false,
        "NewLinesForBracesInLambdaExpressionBody": false,
        "NewLineForElse": false,
        "NewLineForCatch": false,
        "NewLineForFinally": false,
        "NewLineForMembersInObjectInit": false,
        "NewLineForMembersInAnonymousTypes": false,
        "NewLineForClausesInQuery": false
    }
 }

VS Code settings.json

 {
  "files.autoSave": "afterDelay",
  "workbench.list.smoothScrolling": true,
  "editor.smoothScrolling": true,
  "editor.cursorBlinking": "smooth",
  "editor.mouseWheelZoom": true,
  "editor.formatOnType": true,
  "editor.formatOnSave": true,
  "editor.wordWrap": "on",
  "editor.guides.bracketPairs": true,
  "editor.acceptSuggestionOnEnter": "smart",
  "editor.suggestSelection": "recentlyUsed",
  "window.dialogStyle": "custom",
  "debug.showBreakpointsInOverviewRuler": true,
  "indentRainbow.indicatorStyle": "light",
  "workbench.iconTheme": "material-icon-theme",
  "files.autoGuessEncoding": true,
  "editor.cursorSmoothCaretAnimation": "on",
  "terminal.integrated.smoothScrolling": true,
  "editor.suggest.snippetsPreventQuickSuggestions": false,
  "editor.formatOnPaste": true,
  "editor.defaultFormatter": "esbenp.prettier-vscode",
  "[csharp]": {
    "editor.defaultFormatter": "ms-dotnettools.csharp"
  }
 }
 ​

微软官方的代码编辑器配置指南:

Visual Studio for Unity:

https://learn.microsoft.com/zh-cn/visualstudio/gamedev/unity/get-started/getting-started-with-visual-studio-tools-for-unity?pivots=windows

Visual Studio Code for Unity:

https://code.visualstudio.com/docs/other/unity

(VSCode建议安装C#(在设置里搜索Use Modern Net并取消勾选)、Debugger for Unity、Unity Code Snippets等插件)


笔记源:M_Studio : https://www.bilibili.com/video/BV1mL411o77x/

7_erQ : Unity使用VS Code编辑时,代码补全提示不全、误纠错的解决记录

解决VSCode无法显示Unity代码提示的真正解决办法

THE END