简体   繁体   中英

c# Intellisense for unity is not working in vscode

I am trying to get unity Intellisense working in vscode. I am using:

- ubuntu 20.04
- unity 2020.3.23f1
- vscode 1.62.3 
- dotnet 6.0.100 
- mono 6.8.0.105

I installed the c# extension in vscode.

I set: unity > edit > preferences > external tools >external script editor

to /snap/bin/code when I open a script, it opens in vscode and gives me this:

Starting OmniSharp server at 11/29/2021, 10:21:03 PM
    Target: /home/(myusername)/Desktop/test/test.sln

OmniSharp server started.
    Path: /home/(myusername)/.vscode/extensions/ms-dotnettools.csharp-1.23.16/.omnisharp/1.37.16/run
    PID: 263330

[info]: OmniSharp.Stdio.Host
        Starting OmniSharp on ubuntu 20.4 (x64)
[info]: OmniSharp.Services.DotNetCliService
        Checking the 'DOTNET_ROOT' environment variable to find a .NET SDK
[info]: OmniSharp.Services.DotNetCliService
        Using the 'dotnet' on the PATH.
[info]: OmniSharp.Services.DotNetCliService
        DotNetPath set to dotnet
[info]: OmniSharp.MSBuild.Discovery.MSBuildLocator
        Located 1 MSBuild instance(s)
            1: StandAlone 17.0.0 - "/home/(myusername)/.vscode/extensions/ms-dotnettools.csharp-1.23.16/.omnisharp/1.37.16/omnisharp/.msbuild/Current/Bin"
[info]: OmniSharp.MSBuild.Discovery.MSBuildLocator
        MSBUILD_EXE_PATH environment variable set to '/home/(myusername)/.vscode/extensions/ms-dotnettools.csharp-1.23.16/.omnisharp/1.37.16/omnisharp/.msbuild/Current/Bin/MSBuild.exe'
[info]: OmniSharp.MSBuild.Discovery.MSBuildLocator
        Registered MSBuild instance: StandAlone 17.0.0 - "/home/(myusername)/.vscode/extensions/ms-dotnettools.csharp-1.23.16/.omnisharp/1.37.16/omnisharp/.msbuild/Current/Bin"
            CscToolExe = csc.exe
            MSBuildToolsPath = /home/(myusername)/.vscode/extensions/ms-dotnettools.csharp-1.23.16/.omnisharp/1.37.16/omnisharp/.msbuild/Current/Bin
            CscToolPath = /home/(myusername)/.vscode/extensions/ms-dotnettools.csharp-1.23.16/.omnisharp/1.37.16/omnisharp/.msbuild/Current/Bin/Roslyn
            BypassFrameworkInstallChecks = true
            MSBuildExtensionsPath = /home/(myusername)/.vscode/extensions/ms-dotnettools.csharp-1.23.16/.omnisharp/1.37.16/omnisharp/.msbuild
[info]: OmniSharp.WorkspaceInitializer
        Invoking Workspace Options Provider: OmniSharp.Roslyn.CSharp.Services.CSharpFormattingWorkspaceOptionsProvider, Order: 0
[info]: OmniSharp.WorkspaceInitializer
        Invoking Workspace Options Provider: OmniSharp.Roslyn.CSharp.Services.Completion.CompletionOptionsProvider, Order: 0
[info]: OmniSharp.WorkspaceInitializer
        Invoking Workspace Options Provider: OmniSharp.Roslyn.CSharp.Services.RenameWorkspaceOptionsProvider, Order: 100
[info]: OmniSharp.WorkspaceInitializer
        Invoking Workspace Options Provider: OmniSharp.Roslyn.CSharp.Services.ImplementTypeWorkspaceOptionsProvider, Order: 110
[info]: OmniSharp.WorkspaceInitializer
        Invoking Workspace Options Provider: OmniSharp.Roslyn.CSharp.Services.BlockStructureWorkspaceOptionsProvider, Order: 140
[info]: OmniSharp.Cake.CakeProjectSystem
        Detecting Cake files in '/home/(myusername)/Desktop/test'.
[info]: OmniSharp.Cake.CakeProjectSystem
        Could not find any Cake files
[info]: OmniSharp.MSBuild.ProjectSystem
        Detecting projects in '/home/(myusername)/Desktop/test/test.sln'.
[info]: OmniSharp.MSBuild.ProjectManager
        Queue project update for '/home/(myusername)/Desktop/test/Assembly-CSharp.csproj'
[info]: OmniSharp.Script.ScriptProjectSystem
        Detecting CSX files in '/home/(myusername)/Desktop/test'.
[info]: OmniSharp.Script.ScriptProjectSystem
        Could not find any CSX files
[info]: OmniSharp.WorkspaceInitializer
        Configuration finished.
[info]: OmniSharp.Stdio.Host
        Omnisharp server running using Stdio at location '/home/(myusername)/Desktop/test' on host 263077.
[info]: OmniSharp.MSBuild.ProjectManager
        Loading project: /home/(myusername)/Desktop/test/Assembly-CSharp.csproj
[fail]: OmniSharp.MSBuild.ProjectLoader
        This project targets .NET version that requires reference assemblies that do not ship with OmniSharp out of the box (e.g. .NET Framework). The most common solution is to make sure Mono is installed on your machine (https://mono-project.com/download/) and that OmniSharp is started with that Mono installation (e.g. "omnisharp.useGlobalMono":"always" in C# Extension for VS Code).
[warn]: OmniSharp.MSBuild.ProjectManager
        Failed to load project file '/home/(myusername)/Desktop/test/Assembly-CSharp.csproj'.
/home/(myusername)/Desktop/test/Assembly-CSharp.csproj
/home/(myusername)/.vscode/extensions/ms-dotnettools.csharp-1.23.16/.omnisharp/1.37.16/omnisharp/.msbuild/Current/Bin/Microsoft.Common.CurrentVersion.targets(1217,5): Error: This project targets .NET version that requires reference assemblies that do not ship with OmniSharp out of the box (e.g. .NET Framework). The most common solution is to make sure Mono is installed on your machine (https://mono-project.com/download/) and that OmniSharp is started with that Mono installation (e.g. "omnisharp.useGlobalMono":"always" in C# Extension for VS Code).

[fail]: OmniSharp.MSBuild.ProjectManager
        Attempted to update project that is not loaded: /home/(myusername)/Desktop/test/Assembly-CSharp.csproj

when I set omnisharp.useGlobalMono to always like it says to, I get this:

Starting OmniSharp server at 11/29/2021, 10:29:27 PM
    Target: /home/(myusername)/Desktop/test/test.sln

OmniSharp server started with Mono 6.8.0.
    Path: /home/(myusername)/.vscode/extensions/ms-dotnettools.csharp-1.23.16/.omnisharp/1.37.16/omnisharp/OmniSharp.exe
    PID: 263478

System.TypeLoadException: Could not load type of field 'McMaster.Extensions.CommandLineUtils.CommandLineApplication:_validationErrorHandler' (41) due to: Could not load file or assembly 'System.ComponentModel.DataAnnotations, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies.
  at OmniSharp.Stdio.StdioCommandLineApplication..ctor () [0x00000] in <fa42ddee3b094ad29621a886df38c746>:0 
  at OmniSharp.Stdio.Driver.Program+<>c__DisplayClass0_0.<Main>b__0 () [0x00006] in <3fcd524b49434503b0223979f206dfcf>:0 
  at OmniSharp.HostHelpers.Start (System.Func`1[TResult] action) [0x0001c] in <646769315b6548dcb5f6b23c2c2cc918>:0 

I don't know what this means or how to fix it so Intellisense will work.

Try to click regenerate project files in External Tools . ;)

I use vscode and that plugins: https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.csharp https://marketplace.visualstudio.com/items?itemName=Unity.unity-debug

and it work nice.

在此处输入图像描述

Often struggling with this... In my case (Mac OS) this happens when first opening VS Code directly from Unity, eg by double clicking on some C# script in the assets.

Opening the VS Code application from the Finder solves for me (after OmniSharp finishes loading all project files).

Sometimes I also find useful calling OmniSharp: Select Project and/or OmniSharp: Restart OmniSharp from the VS Code Command Palette.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM