08 Apr, 2009
VisualStudio integration for Unity2.5 + ItemTemplates
As I was looking at the web traffic for this blog, I noticed that the post on Visual Studio integration for Unity was getting most traffic by far. Too bad the post was from a 2.1-mac era, and it actually doesn’t work anymore for 2.5-windows, which I suspect most people are looking for these days.
I updated the integratoin to work for Unity 2.5 on Windows, Visual Studio Express, Visual Studio pro, and MonoDevelop. I also created some item templates for Visual Studio, more on that below.
It’s amazing how much convincing it takes to get people to try out Visual Studio for development. I’ve heard every excuse imaginable so far. If you take anything away from this blog, let it be “maybe I should try that visual studio Lucas keeps nagging about”. Or try MonoDevelop. Or SharpDevelop. Anything but Unitron or UniScite or Notepad.
Did I mention Visual Studio Express is free, and does everything you need?
To use this, follow these steps:
- Download this .cs file, and place it in your project, in a folder called “Editor”
- Notice that you now have a Tools menu item, containing a SyncVisualStudioSolution item
- Click it
- Open the .sln file you’ll find in your project root folder
Every time you click it, the .sln and .csproj files get recreated. In daily usage you won’t do this very often, as a nice workflow is just that you create new scripts in visual studio itself, instead of in Unity. As long as you create scripts in visual studio, Unity will automatically pick up those scripts, and your Unity project and your VisualStudio solution will be in sync. If one day you decide to create a script in Unity, just click the SyncVisualStudioSolution button, and your solution will be recreated, with all scripts and shaders that live in your Unity project.
The created project directly references the UnityEngine.dll and UnityEditor.dll in your Unity installation, nothing you need to do there. For the best experience, download these xml documentation files, and place them in your C:\Program Files\Unity\Editor\Data\lib folder. They will get you the Unity documentation in tooltips as you are using methods in VisualStudio. Very neat. (Thanks to Benoit Fouletier for his regex-fu)
Are there no downsides?
Not many, but here’s one. Because Visual Studio requires the folders contained in its projects to be in a directory under the .csproj file in order for create->new code file to create files in those folders, instead of in a “fake” folder, I chose to place the .sln and .csproj files in the root of your Unity project.
Unfortunately this means a bit of clutter: sln, csproj, suo, obj, _Resharper, are some of the files/folders you might see laying around in your Unity project folder now. (Not in Assets/ mind you, in the parent folder of Assets). There is nothing inherently bad about this, except that doesn’t look very tidy.
Item Templates
I also created some Visual Studio Item Templates. These are basically boilerplate files, so you don’t have to write the same stuff over and over again. There’s one for a MonoBehaviour, and one for an EditorScript. If you click Add->Add New Item, you’ll get this dialog:
Which results in this file:

Note how the name I chose in the first dialog is inserted in several places in the actual editorscript.
To get these, download this file, and extract the two zipfiles that it contains into your equivalent of
C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Templates\ItemTemplates\Visual C#
I couldn’t figure out how to put these item templates in the “Code” subsection. Bonus points for whoever figures it out, and double bonus points for who figures out how I can specify a starting point point for the cursor, or an initial range of characters that should be selected.
Prefer MonoDevelop?
That’s cool. MonoDevelop reads .sln and .csproj just fine. So if you’re on a mac, and want to use MonoDevelop (make sure to use the newly released 2.0, it has gotten a lot better), go nuts. Just open the .sln file and you should be good to go. In theory
I hope people find all of these useful. Drop a line in the comments if you feel like it.