Unity使用VSCode作为编译器(windows/mac)

1,187 阅读3分钟

所有步骤都在VSCode官方文章中提到code.visualstudio.com/docs/other/… 本文仅做参考避坑

版本:
macOS:13.1 (22C65)
windows: Windows 11 专业版 22H2
unity: 2021.3.10f1c1 / 2021.3.23f1
vscode: 1.77.3

MacOS

下载.NET SDK

Install the .NET SDK, which includes the Runtime and the dotnet command.

安装后验证,打开终端,输入dotnet --info,返回信息

➜  ~ dotnet --info
.NET SDK:
 Version:   7.0.203
 Commit:    5b005c19f5

运行时环境:
 OS Name:     Mac OS X
 OS Version:  13.1
 OS Platform: Darwin
 RID:         osx.13-arm64
 Base Path:   /usr/local/share/dotnet/sdk/7.0.203/
  ...
  ...

Note: .NET SDK一般会自动注入环境变量,如果验证不通过,但又确认安装了,请检查环境变量。

我的安装位置(默认):/usr/local/share/dotnet

➜  ~ which dotnet
/usr/local/share/dotnet/dotnet

下载Mono Framework

To avoid seeing "Some projects have trouble loading. Please review the output for more details", make sure to install the latest stable Mono release.

Note: This version of Mono, which is installed into your system, will not interfere with the version of MonoDevelop that is installed by Unity.

安装后验证,在终端中输入mono --version,返回mono相关信息

➜  ~ mono --version
Mono JIT compiler version 6.12.0.182 (2020-02/6051b710727 Tue Jun 14 15:01:21 EDT 2022)
Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
	TLS:
	SIGSEGV:       altstack
	Notification:  kqueue
	Architecture:  amd64
	Disabled:      none
	Misc:          softdebug
	Interpreter:   yes
	LLVM:          yes(610)
	Suspend:       hybrid
	GC:            sgen (concurrent by default)

Note: Mono这个安装他没有把环境变量安装上,不确定是我环境原因还是其他原因导致。

我的安装位置(默认):/Library/Frameworks/Mono.framework

➜  ~ which mono
/Library/Frameworks/Mono.framework/Versions/Current/bin/mono

我用的是iterm2,在用户根目录~下的.zshrc文件添加

# Mono Framework
export PATH=$PATH:/Library/Frameworks/Mono.framework/Versions/Current/bin

重新刷新,终端中输入source .zshrc,之后再次验证.

在unity中配置默认打开VSCode

Open up Unity PreferencesExternal Tools, then browse for the Visual Studio Code executable as External Script Editor.

打开unity项目,点击左上角unity -> Settings,或者command + ,打开设置页面。

image.png

找到External Tools -> External Script Editor 下拉选项选择Visual Studio Code image.png 其他配置看需要勾选

下载VSCode C#插件

打开VSCode,打开扩展,输入c#,安装

image.png

关闭useModernNet设置

安装完成后打开VSCode设置,可以用Command+,打开,搜索设置useModernNet

image.png

关闭useModernNet,并且配置Mono Path

Note: 你可以尝试不配置MonoPath,看是否生效,我的环境不配置并没有生效,并且会弹出一个Dialog说找不到Mono需要去下载一个完整的Mono Framework以及下载地址。

验证

打开Unity项目,双击一个C#脚本,VSCode弹出后。(注意重启VSCode)

image.png

输出的日志会弹出一堆东西关于OmniSharp,只要没报错即可。

image.png

(Windows系统下C#文件中的一些变量方法上面出现数字 references这样的字样)

Windows

下载.NET SDK

Install the .NET SDK, which includes the Runtime and the dotnet command. Logout or restart Windows to allow changes to %PATH% to take effect.

安装后验证,打开终端,输入dotnet --info,返回信息

C:\Users\sou7h>dotnet --info
.NET SDK:
 Version:   7.0.203
 Commit:    5b005c19f5

运行时环境:
 OS Name:     Windows
 OS Version:  10.0.22621
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\7.0.203\
 ...

Note: 特别注意可能需要重启电脑,使环境变量生效(我不记得我怎么生效的了,总之验证通过即可)

下载 .NET Framework 4.7.1

Note:注意一定要安装4.7.1版本,4.8版本不行。不安装的话OmniSharp会报错需要你安装指定版本

在unity中配置默认打开VSCode (同Mac,见上文macOS)

Note: 注意windows系统unity的setting是在edit菜单中最下面几行的Preferences
External Tool配置都一样

下载VSCode C#插件 (同Mac,见上文macOS)

打开VSCode,打开扩展,搜索C#,安装

VSCode官方文章中所谓关闭useModernNet设置

这个配置打开VSCode的设置,可以用快捷键Ctrl + ,打开,搜索useModernNet

Note: 原文文章中并没有标注MacOS Only或者Windows Only,但是这个设置在我这里,只对MacOS有效果。Windows开启后反而OmniSharp.MSBuild.Discovery.MSBuildLocator相关报错。你可以尝试关闭或者开启,反复验证是否有效。

验证 (同Mac,见上文macOS)

image.png (Windows系统下C#文件中的一些变量方法上面出现数字 references这样的字样)