Lucas Meijer

Game Development Consultancy

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.

21 Responses to "VisualStudio integration for Unity2.5 + ItemTemplates"

1 | Lucas Meijer » Blog Archive » One click Unity & Visual Studio integration.

April 8th, 2009 at 12:51 am

Avatar

[...] Update: An improved version of this post, that works for Unity2.5, can be found here. [...]

2 | Troy

April 8th, 2009 at 1:27 am

Avatar

… or try TextMate and the Unity bundles.

3 | chao

April 8th, 2009 at 6:30 am

Avatar

Thanks for the tutorial…However, due to newbie to C# and VS setup, I am not following completely. It be great if a sample unity and VS project package can be offered to get start off.

I lost at:
1. Download this .cs file, and place it in your project, in a folder called “Editor”
>>>which project you referring to? Unity or VS project?

2. Open the .sln file you’ll find in your project root folder
>>>where is the .sln, and what project root folder referring to?

Anyway, nvm if I am asking to much, I just hope to figure out how to setup VS with Unity2.5 for C# scripting…

Thanks
Chao

4 | Keli Hlodversson

April 8th, 2009 at 8:28 am

Avatar

@chao: as far as I understood it’s all in the Unity project folder; The .cs file should go into Assets\Editor\ in your Unity project folder. (Create the Editor folder first if it’s not there.)

After clicking the new menu item in Unity, the VS solution file is located at the root of your Unity project folder (at the same level as the Assets folder, not inside it.)

@lucas: Maybe you should export the editor script as a unitypackage? That way you can simply say “import this package into your Unity project” and not worry about the script being placed in the wrong location. (The package file can then be saved along with the StandardAssets.unitypackage, allowing for easy inclusion in new Unity projects.)

5 | Jerome

April 8th, 2009 at 1:11 pm

Avatar

This works a treat for me – very good, since I love Visual Studio as an editor. But I did note that the solution/project creation script seems to link in Engine-debug.dll by default, for which the zip file doesn’t include a documentation xml. With a little tinkering you can also reduce VS’s clutter by moving some stuff into a custom subdirectory…

6 | Lucas Meijer

April 8th, 2009 at 4:10 pm

Avatar

@Troy: If you suggest to use Textmate, you seriously didn’t get it :)

@chao: Put this script in your unity project folder in Assets/Editor/

@Keli: I guess you’re right. for some reason I really dislike unitypackages. not sure why.

@Jerome: oops. renaming the .xml to UnityEngine-Debug.xml should do the trick. silly I missed this.

@Everybody: Tested & Working on MonoDevelop2 on OSX too.

7 | Ricky Helgesson

April 10th, 2009 at 10:41 am

Avatar

I cannot get the item templates to work for Visual Studio. What am I doing wrong? I am using Visual Studio 2008 Pro. I copy all the unzipped files into my ItemTemplates directory and restarts Visual Studio. I guess this is more of a Visual Studio environment issue but if anyone has any tips, please help me.

8 | Brett Brenneman

April 10th, 2009 at 9:11 pm

Avatar

No templates for me either. I’m using MonoDevelop on the Mac, though.

9 | Unity Technologies Blog » Blog Archive » Resources everyone should see

April 14th, 2009 at 6:08 pm

Avatar

[...] In our roadmap we announced Visual Studio integration for Unity 2.6. However you can already use Unity with Visual Studio very well. Lucas Meijer has a blog post on how to create Visual Studio projects from a Unity Project folder automatically here. [...]

10 | Anonymous

April 14th, 2009 at 11:01 pm

Avatar

where do we put the xml files on osx for monodevelop?

11 | yeobi

April 15th, 2009 at 4:19 pm

Avatar

Thank you for your post. But, I have an error.

“Couldn’t load the script “SyncVisualStudioSolution” because its file name doesn’t match the class name.
Please make sure the file name of the script is the same as the class defined inside it.”

What does this message mean?
What can I do?

12 | lev_wyeth

April 15th, 2009 at 9:26 pm

Avatar

For templates to work, you need to keep the files in the zips, dont unzip the 2 zip files.

If you want them to show up under Code, just make that a subfolder of the “Visual C#/code” and put your zips there.

Nice!

13 | daniel sperry

April 29th, 2009 at 5:10 pm

Avatar

Thanks a lot for your script!

Some of my projects use external DLLs.
I also found useful to include dlls as refereces adding the following lines:

fis = di.GetFiles(”*.dll”, SearchOption.AllDirectories); //make recursive
foreach (FileInfo fi in fis)
{
string relative = fi.FullName.Substring(di.FullName.Length + 1);
relative = relative.Replace(”/”, “\\”);
sw.WriteLine(” “);
sw.WriteLine(” Assets\\” + relative + “”);
sw.WriteLine(” “);
}

14 | daniel sperry

April 29th, 2009 at 5:13 pm

Avatar

Oops html escape problems.

fis = di.GetFiles(”*.dll”, SearchOption.AllDirectories); //make recursive
foreach (FileInfo fi in fis)
{
string relative = fi.FullName.Substring(di.FullName.Length + 1);
relative = relative.Replace(”/”, “\\”);
sw.WriteLine(” <Reference Include=\”" + fi.Name.Substring(0, fi.Name.Length – 4) + “\”>”);
sw.WriteLine(” <HintPath>Assets\\” + relative + “</HintPath>”);
sw.WriteLine(” </Reference>”);
}

15 | Wilks

May 6th, 2009 at 3:10 am

Avatar

Sadly, it doesn’t appear to work for 1.0.2 iPhone version :)

16 | EmFinn

May 18th, 2009 at 7:08 am

Avatar

Love you so much right now. I’ve been copying and pasting code back and forth between unity and VCE for days now.

If you ever need a kidney let me know. I’m sure I have a spare around here somewhere.

17 | diamondTearz

May 19th, 2009 at 4:25 pm

Avatar

Thanks for updating the Visual Studio Item Templates!

18 | Digitalos

June 13th, 2009 at 1:37 pm

Avatar

Really nice, I was using UniScite and this is just awesome in comparison. Thanks very much for doing this and maintaining it :)

19 | CptCrunch

June 17th, 2009 at 5:06 pm

Avatar

Thanks for your script and tips!

Here’s my question: since the project now compile and link with the unity dlls, can we run the executable and have the game start from there?

My intent is to try and debug the C# code from Visual studio. Do you think this is possible?

20 | Aram Azhari

November 8th, 2009 at 9:56 pm

Avatar

Hey, Excellent work.

In order to make the code appear under the “Code” category in Visual Studio, You must place your zip files in a folder with the name of that category.

So the final path for the zip file will be:

“Visual Studio 2008\Templates\ItemTemplates\Visual C#\Code\

21 | Aram Azhari

November 8th, 2009 at 9:57 pm

Avatar

Although I would suggest putting it under a folder called “Unity”.

That might look nicer :D

Comment Form

Game Development Consultancy

Hi. I'm Lucas Meijer. consultant on game development projects I work at Unity. You're looking at my portfolio website.

Please look around, and take a look at my work, my thoughts or more general information about myself.


If you have any questions regarding consulting, licensing of games, or anything else, feel free to email me at lucas@lucasmeijer.com

Contact Info

Lucas Meijer
lucas@lucasmeijer.com
lucasmeijer on twitter
ljmeijer on skype