Jhonatan da Silva

Jhonatan da Silva

How to setup Unity + Neovim LSP

What you'll need

  • Neovim 0.5
  • Unity
  • vscode (I kid you not)

A part from that, it would also be nice if you know the basics of neovim or vim.

So, here's the scenario, you are a dev or game dev that uses neovim (or want to use, welcome to the club), and you are a bit frustrated that you can't make neovim lsp + completion work for Unity, so was I, but a step at a time, and you'll figure it out.

So, let's go to the good stuff. What I recommend for doing this

  1. Download omnisharp-roslyn - You'll need to know the path of this file later, so place somewhere accessible.
  2. Install a late version o mono (I'm using 6.8) - You can checkout your current version with mono --version

With those two setup, you'll need to configure omnisharp to look at your latest mono version. Go to the run file where you installed omnisharp (in my case, this file is in $HOME/software/omnisharp/run), and change it to:

#!/usr/bin/env bash

base_dir="$(cd "$(dirname "$0")" && pwd -P)"
bin_dir=${base_dir}/bin
etc_dir=${base_dir}/etc
omnisharp_dir=${base_dir}/omnisharp

mono_cmd=`command -v mono`

config_file=/etc/mono/config

omnisharp_cmd=${omnisharp_dir}/OmniSharp.exe
no_omnisharp=false

export MONO_CFG_DIR=${etc_dir}
export MONO_ENV_OPTIONS="--assembly-loader=strict --config ${config_file}"

"${mono_cmd}" "${omnisharp_cmd}" "$@"

Now, add to your init.vim

" LSP
Plug 'neovim/nvim-lspconfig'
Plug 'hrsh7th/nvim-compe'

If you want to have some symbols in your completion, also add

Plug 'onsails/lspkind-nvim'

This is assuming you are using vim-plug, if not, you can find the specific way to install these plugins for you.

Lua modules

Now in your ~/.config/nvim/ folder, add lua/yourname/lsp.lua, if you don't have it already. Your name is optional, but it makes it easier to detect the configs files you wrote. The complete path should look like

~/.config/nvim/lua/yourname/lsp.lua

In this file you can add:

local nvim_lsp = require'lspconfig'

local pid = vim.fn.getpid()
-- On linux/darwin if using a release build, otherwise under scripts/OmniSharp(.Core)(.cmd)
local omnisharp_bin = "/home/yourusername/software/omnisharp/run"

require'lspconfig'.omnisharp.setup{
    cmd = { omnisharp_bin, "--languageserver" , "--hostPID", tostring(pid) };
    root_dir = nvim_lsp.util.root_pattern("*.csproj","*.sln");
    ...
}

Remember to change the line to your path, it's important to pass the global path (not with $HOME/...)

local omnisharp_bin = "/home/yourusername/software/omnisharp/run"

And add this right next to it if you've added the lspkind:

require('lspkind').init({
    -- enables text annotations
    --
    -- default: true
    with_text = true,

    -- default symbol map
    -- can be either 'default' or
    -- 'codicons' for codicon preset (requires vscode-codicons font installed)
    --
    -- default: 'default'
    preset = 'codicons',

    -- override preset symbols
    --
    -- default: {}
    symbol_map = {
      Text = '',
      Method = 'ƒ',
      Function = '',
      Constructor = '',
      Variable = '',
      Class = '',
      Interface = 'ﰮ',
      Module = '',
      Property = '',
      Unit = '',
      Value = '',
      Enum = '了',
      Keyword = '',
      Snippet = '﬌',
      Color = '',
      File = '',
      Folder = '',
      EnumMember = '',
      Constant = '',
      Struct = ''
    },
})

Now, everything on the neovim side of it is done. Let's go to Unity.

Unity config

That's the tricky part of it, and requires vscode (I kid you not). I spent a lot of time banging my head over the wall because I didn't have vscode (or another editor, it may work) installed, only neovim.

With your project open, under Edit > Preferences

 Preferences

Now goes the vscode part of it, if you don't have vscode installed, those aditional options should not appear to you. You need to set vscode as the editor, and then the option "Generate .csproj files for will show up. Be sure to check

✅ Embedded packages
✅ Local packages
✅ Registry packages
✅ Built-in packages

These options will allow Unity completion as well as the libraries, like ML-Agents that are using for my final graduate project in mechatronics engineering.

After clicking those settings, you can now "Regenerate project files". This will add a lot of .csproj files to the root folder of your project, those files is what lsp and omnisharp will use for completion.

.csproj files

Now, go to the root of your project and open neovim

nvim .

When you open a C# script from unity, the LSP should start getting alive. If you use

:LspInfo

In neovim, you'll see the status of the LSP. In my case, I have galaxyline, so I can see the status of the LSP as well.

status of LSP

And that's it, if you now try, you'll see that you have completion and the LSP working

lsp and completion working

If you want more content on Unity, I have a 1 year box set of tutorials.

References

Backlinks: