Using Microsoft Visual Studio Code as the external code editor in Unity

Yesterday during the Build 2015 developer conference, Microsoft announced and released their new editor called Visual Studio Code (or just “Code” for short).  Code is free, runs on Windows, Linux and Mac OSX.  I immediately wondered if it would be a good alternative to MonoDevelop as a code editor for use in Unity.  After a bit of fiddling, I managed to get it all working quite nicely, with proper code completion etc.  There are some caveats though, it does not work perfectly, but the caveats are easily worked around.

Here are the steps I followed on my Mac:

  1. Install Code

    This is a fairly simple step, merely download the zip file, unzip and drag the .app file to your Applications folder.

  2. Choose Visual Studio as Unity’s editor

    Simply open Unity, open preferences with CTRL-`, choose “External Tools”, then choose “Browse” under the “External Script Editor”, and find your newly dragged Visual Studio Code icon in your Applications folder.

  3. Prepare your project in Code

    Start up Code, and click File -> Open, then choose the top-level folder of the Unity project you want to work in.

    This will open the folder, and show you the list of folders and files within your Unity project.  But it has not opened the folder *as a project* yet…  So it’s important to do the next step.

  4. Make Code see the folder as a project

    At the bottom left of the Code window, you will see this:

    pick a project

    Click the “pick a project” part, which will popup a dropdown box at the top of the window.  Choose the option that has just “Your Project.sln”.  Do not choose the one that has “-csharp.sln” at the end.

 

Your Visual Studio Code is now setup for this project, and double-clicking on any C# script within Unity should now open in Code.  You’ll see syntax highlighting, auto-completion, and a bunch of other niceties that comes with Code.

For more information on Visual Studio Code, check their Docs page.

 

Caveats

Some problems I’ve come across with the above solution:

  1. I have to have Code open first, and my project folder open, before trying to edit a script from within Unity.  Otherwise it simply opens Code with no file being edited.
  2. Code also does not know about a Unity project automatically, so if I change from one Unity project to the next, I have to open the new project’s folder in Code first (and follow Step 4 above if I haven’t done so yet).  If I don’t do this, I end up with files from one project in the “Working files” list of another project, which obviously gets confusing.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *