VSCode——愉快的写C#

2018-12-03  本文已影响0人  战斗力五只鹅

写Unity项目

VSCode安装插件C#,并在Unity中选择OpenC#Project,此时C#代码智能提示功能生效,即可使用VSCode写Unity项目了。

ps:项目更新到Unity2018后,出现C#插件智能提示失效的错误, 日志信息:

Failed to load project file 'f:*******\Assembly-CSharp.csproj'.
f:\Projects*******\Assembly-CSharp.csproj
C:\Users#######.vscode\extensions\ms-vscode.csharp-1.15.2.omnisharp\1.30.1\msbuild\15.0\Bin\Microsoft.Common.CurrentVersion.targets(1126,5): Error: The reference assemblies for framework ".NETFramework,Version=v4.7.1" were not found. To resolve this, install the SDK or Targeting Pack for this framework version or retarget your application to a version of the framework for which you have the SDK or Targeting Pack installed. Note that assemblies will be resolved from the Global Assembly Cache (GAC) and will be used in place of reference assemblies. Therefore your assembly may not be correctly targeted for the framework you intend.

根据error信息,到.NETFramework4.7.1官网下载这里下载安装好,然后重新打开项目就可以了。


如果需要断点调试,安装插件Debugger for Unity

当然,为了更好的使用,还可以安装以下插件:

直接运行一段C#代码

有时我们只想反复运行测试一小段算法代码,这时频繁启动unity就很烦。通过CodeRunner插件,可以不用启动项目,直接运行一段C#代码。

  1. VSCode安装CodeRunner。
  2. 新建Test.cs,输入以下代码。
Console.WriteLine("Hello C#");
  1. 选择代码右键RunCode,出现乱码。
[Running] scriptcs "f:\tempCodeRunnerFile.cs"
'scriptcs' �����ڲ����ⲿ���Ҳ���ǿ����еij���
���������ļ���

[Done] exited with code=1 in 0.018 seconds

解决方案

打开控制台属性:

打开控制台属性.png

修改下方的默认编码页

控制台属性.png

但是发现新版控制台是无法修改的:


新版控制台.png

所以只能通过注册表修改:
比如这个方案提到的注册表位置:

HKEY_CURRENT_USER\Console\%SystemRoot%_system32_cmd.exe

然而图样图森破,我的注册表是这样的:


我的注册表.png

这是由于我的win10版本,默认采用的控制台是powershell而不是cmd。所以可以换回cmd,也可以修改powershell的输出编码。

powershell注册表这边没有CodePage这一项不要紧,我们自己新建一个就可以了。例如这个方案PowerShell中文乱码

通过cmd的chcp命令可以查看当前cmd的编码,chcp [xxx]可以切换编码,但是只对当前窗口有效。

  1. cmd乱码解决后,重新RunCode,仍然无法运行: image.png
    查看CodeRunner主页: image.png
    所以接下来需要安装scriptcsscriptcs介绍
    通过The package manager for Windows安装scriptcs。官方的安装教程很详细,装好后可以尝试下从命令行写C#。 cmd写C#.png

Good,安装成功,可以直接从CodeRunner运行C#了。

上一篇 下一篇

猜你喜欢

热点阅读