Setting up Unity with Vim or VS Code on Linux

The following has been tested on Ubuntu and Arch, although it should work for any distro – just change package names accordingly.

screenshot

If you want to use Vim, feel free to skip the VS Code section – although I’d still recommend setting that up to confirm Mono and OmniSharp are working properly.

# Mono

Arch: mono via pacman. Additionally, having mono-msbuild and mono-msbuild-sdkresolver installed has helped in some cases where OmniSharp wouldn’t start properly.

Ubuntu: Install Mono as described here. I’m pretty sure msbuild is shipped along with the above download.

# .NET SDK

Ubuntu: Install via snap:

sudo snap install --classic dotnet-sdk

VS Code might be unable to locate the dotnet-sdk if installed via snap. You can simply fix it by runnig sudo snap alias dotnet-sdk.dotnet dotnet.

Arch: dotnet-sdk via pacman.

(Skip to Vim)

# VS Code

Ubuntu: Install non-Snap VS Code via the .deb package from here.

sudo apt install ./code*.deb

Arch: Install visual-studio-code-bin from AUR (to get access to all extensions). (More info in the Arch Wiki)

In VS Code: Install the following extensions (File -> Preferences -> Extensions):

  • C# (Microsoft)
  • Unity Code Snippets (Kleber Silva)
  • Unity Snippets (Ycleptic Studios)
  • Unity Tools (Tobiah Zarlez)

Either search for each extension, or install them by running:

code --install-extension ms-dotnettools.csharp
code --install-extension kleber-swf.unity-code-snippets
code --install-extension twxs.cmake
code --install-extension Tobiah.unity-tools

In VSCode’s settings (File -> Preferences -> Settings):

  1. Search omnisharp.path, hit Edit in settings.json, and set it to "latest".
  2. Search omnisharp.useGlobalMono and set to always.
  3. Search omnisharp.useModernNet and uncheck the box.

The extensions will automatically download omnisharp. You should be able to see progress in the Output window of VS Code, if not, it might be a good idea to restart VS Code.

# Unity

On Ubuntu, use the Unity Hub AppImage from here. On Arch, install unityhub. You can find more detailed setup instructions here.

From the Unity Editor, in Edit -> Preferences -> External Tools select VSCode (likely /usr/bin/code). Below, tick the following:

  • Embedded packages
  • Local packages
  • Git packages
  • Built-in packages

and hit Regenerate Project Files.

# Issues opening files

You should be able to double click on the .cs files from within Unity and have VS Code open the file. On some systems there’s an issue where VS Code opens some weird paths with quotation marks in them.

To fix this, open up VS Code by yourself, click File -> Open Folder and select your project directory (the one with the Assets directory in it). VS Code should now ask you which project to open. Choose the .sln file and you should be good to go.

# That’s it!

Now, your VS Code should be smart! Hovering over Unity stuff should display documentation, as seen below.

screenshot

# Bonus Round: Vim

Note: I use neovim. The following has not been tested with regular vim.

To set up neovim integration, first install CoC, version v0.0.81.

If you use plugged, add the following line to your init.nvim:

Plug 'neoclide/coc.nvim', {'branch': 'v0.0.81', 'do': 'yarn install --frozen-lockfile'}

After installing CoC, add the OmniSharp plugin to CoC, by adding the following to your init.vim:

let g:coc_global_extensions = [
\ 'coc-omnisharp',
\ ]
let g:OmniSharp_server_use_mono = 1

In your coc-settings.json, specify the latest non-borked OmniSharp version:

{
    "omnisharp.version" : "v1.38.0"
}

Start neovim and let it install OmniSharp. You’re almost done, you just need to make sure coc-omnisharp uses your system’s Mono installation instead of its own:

ln -sf $(which mono) ~/.config/coc/extensions/coc-omnisharp-data/server/bin/mono

# Minor Annoyances and TODOs

I now exclusively use vim for Unity development, but there’s still some stuff to figure out:

  • As you can see in the screenshot, suggestions appear twice.
  • After creating a new C# script, the project files must be regenerated in Unity.
  • I’m sticking older versions of coc and coc-omnisharp, which might break in the future. Things work perfectly fine at least up to Unity 2022, so it’s fine for now.

# Comments

This post is a copy of a GitHub Gist. Feel free to post any comments or additions there. Also, thanks to @jreeee for the Snap Alias tip :)