Setting up VS Code for Unity Development

Posted on

When it comes to Unity development, the go-to IDE for most developers is Visual Studio by Microsoft. This IDE comes installed by default through Unity Hub and is loaded with features to aid debugging and development.

Don’t get me wrong; Visual Studio is great. However, I have over 10,000 hours of experience with Visual Studio Code. It’s difficult getting used to an entirely different set of key commands and switching contexts every time I work on a game (I use VS Code for work). My big issue, though, is the performance differences between the two. VS Code is significantly more snappy and lightweight, I felt throttled by Visual Studio.

I was a little nervous venturing off into VS Code land, but for my needs it works better than Visual Studio with some minor configuration.

Configuring Unity

Before we get to the VS Code side of things we need to do some set up on the Unity side. You’ll have to install a couple packages to get VS Code working good. You’ll need both the Visual Studio Code Editor plugin and the Visual Studio Editor plugin. It might seem odd to require the Visual Studio Code Editor plugin, but it’s a dependency for the Unity VS Code plugin that we’ll install later.

The two required Unity packages
The two required Unity packages

Once we have those we’re ready to tell Unity to use VS Code as our external editor. This can be done by going to Edit > Preferences > External Tools and selecting Visual Studio Code as your external editor. You’ll also want to click the Regenerate project files button. Once complete remember to do a save and save your project, this will ensure these settings persist next time you restart Unity.

Setting VS Code as our external editor
Setting VS Code as our external editor

Install .NET SDK 7

Another dependency of the Unity VS Code plugin is the .NET SDK 7.0. Download it from here and install before proceeding to the next section.

VS Code Plugins

Now that we have all the prerequisites out of the way, we’re ready to start configuring VS Code. There are several plugins you’ll need to ensure optimal functionality.

C#

The first plugin you’ll want is C#, this adds C# language support to VS Code and is the official plugin by Microsoft.

Unity

The next plugin you will need is the official Unity plugin by Microsoft. This plugin enables you to use the Debugger with unity inside of VS Code.

C# Dev Kit and C# IntelliCode for C# Dev Kit

These plugins will automatically install with the Unity plugin. The dev kit plugin is essential, while the IntelliCode plugin is optional. I opted to leave it installed. These plugins greatly assist in understanding Unity projects in VS Code.

Unity Code Snippets

I consider this one an absolute must, as the name suggests Unity Code Snippets will add a ton of code snippets for Unity development. Say you start typing onTriggerEn you’ll see a little suggestion in the popup, and when you click it it will generate the full method for you, just like Visual Studio does.

Unity auto-complete inside of VS Code
Unity auto-complete inside of VS Code

XML

If you’re working directly with .UXML (ui toolkit) you will want to install the XML plugin. This adds syntax highlighting and autocomplete for XML files. In the next section we’ll configure VS Code to open .uxml files as xml.

Configuring VS Code

There’s a few settings you’ll want to change to make Unity development better on VS Code.

Opening UXML and USS files

These extensions are essentially just xml and css, you’ll want to instruct VS Code to open them as such. This can be done by adding associations to your vs code settings json.

{
    "files.associations": {
      "*.uxml": "xml",
      "*.uss": "css",
    }
}

Formatting settings

If you’re not a fan of the default C# formatting settings, you can customize them by adding an .editorconfig file to your project’s root. I like to keep my braces on the same line, so I added the following content to mine:

#  CSharp formatting rules:
[*.cs]
csharp_new_line_before_open_brace = none

Using VS Code for Unity development

At this point you should have a pretty good development environment. Everything should be highlighting correctly and auto suggest should be popping up as you type things. You should also be able to see how many references a particular variable or function has. Overall it should feel very Visual Studio like without the additional overhead.

Using the debugger is easy too. Simply head to the debugger tab and click the play button on the top left next to “Attach to unity”. The first time you do this Unity will prompt you to enable debugging, simply click enable and you’re good to go!

Using VS Code debugger with Unity
Using VS Code debugger with Unity