Setting up Visual Studio for Unity

From Unify Community Wiki
Jump to: navigation, search

Author(s):
Jashan Chittesh
This includes contributions from the Unity forums by benblo and Lka (collected from various threads in the Unity forums)

Contents

Old Instructions / Mac Instructions

Unity can now easily create a Visual Studio project for you. This is all you need if you're using Windows for development - if, however, you like to develop on the Mac and still use Visual Studio (because it's the best IDE), also have a look at the old instructions. This also has information on how to get Intellisense documentation:

Creating a Visual Studio Solution

To create a Visual Studio Solution:

  1. Click Assets->"Sync VisualStudio Project"
  2. Open Visual Studio
  3. Click File->Open->"Project/Solution..."
  4. Locate the .sln which is likely at the root of your project.

Important note about file renaming

If you need to rename a behaviour (non-behaviour scripts don't matter), make sure to do it in Unity! If you rename it in VS, Unity will consider it a new file, and all references to it (components) will be lost.

Then in VS, exclude the script, display all files in the solution explorer, include the new renamed script, and finally rename the class in the editor (and enjoy some sweet refactoring :). It's a tad tedious, but once you get the habit it's not that bad.

Note that if you forget and rename in VS, all is not lost. Just find the components in Unity, you'll see all their properties are still there but the script reference (it will say "Missing Mono Behaviour"). Just drop the renamed script in the script box, and yay, you didn't lose any property!

Version Control

UT's Asset Server

The best solution to version control for Unity is probably UT's Asset Server. I'm using this now and thus I have dropped Subversion, but for reference for people who would prefer to use Subversion:

Subversion

If you want version control, you may want to try Subversion: http://subversion.tigris.org/

It’s very convenient to use on Windows with TortoiseSVN: http://tortoisesvn.tigris.org/

One important note: In the setup I’m using, I don’t version control the actual VS.NET project. There’s not really much worthwhile version controlling in there, anyways. Instead, I directly put the script files from my assets folder under version control. I have those on a share mapped to a drive. Don’t get confused when TortoiseSVN doesn’t automatically, create the relevant icon overlays, you need to change the settings for that to work correctly: Right click on some item in the explorer to get the context menu, go to TortoiseSVN/Settings. In there, under Look and Feel, you find Icon Overlays, and there, under Drive Types, you’ll find Network Drives is not checked. Check it, and the world is wonderful again ;-)

There is also a Subversion-plugin for Visual Studio.NET 2005, but personally, I didn’t really like it. If you still want to give it a try: http://ankhsvn.tigris.org/.

If you want a great SVN plugin for VS.NET you should use VisualSVN[1]. It is not free, but pretty cheap and it works like a charm.

I would not recommend using Subversion for version controlling the binary assets - I'd only use it for scripts and textfiles. Some people (see this posting for an example) have reported trouble someone along the way when version controlling Unity projects (including the binary assets) with Subversion, so you have been warned ;-)

Subversion, another take

This is drawn mostly from experiences with using Unity and SVN at the Global Game Jam 2009.

As said above, scripts textfiles and also assetfiles work well with Unity. You can also version scenes and prefabs, but since they are binary and cannot be merged - you will have to take care when a conflict arises. Typically this will mean that you have to update, make the changes you want again and then commit. Makes a good case for commiting often!

The worst part to use SVN for is the Library folder. The cache should definately be ignored, and most of the other stuff - including metadata - changes so often that you always get conflicts. What we did was to ignore the library folder completely. This has the unfortunate effect that references to scrips/prefabs/assets for things in the scenes and prefabs does not get saved. Simple properties do - just not references. This lead to tedious work of recreating what others have set up in your local copy. We remedied that by having one machine that was the master machine. On that machine we integrated everything, build the game and made sure it ran. When this was over we shut down Unity. Zipped the library folder and put that in SVN. Others could then update to get the zip and all changes, shut down Unity, remove the library folder and unpack the new one. Then they would be up and running. Somewhat tedious, but it worked for us.

Some nice Add-Ons to Consider

OBSOLETE: One click Unity & Visual Studio integration (Mac)

Note: This is obsolete since Unity has integrated creating visual studio solutions. For issues specific to Mac when using Visual Studio (in a virtual machine environment), see:


Check out Lucas Meijer's blog post which also has a very nice Unity package which makes integrating Unity with Visual Studio even easier: http://lucasmeijer.com/posts/one-click-unity-visual-studio-integration/

OBSOLETE: One click Unity & Visual Studio integration (Windows)

NOTE: This is obsolete now that Unity has full Visual Studio solution support integrated (in particular, no reason to worry about symbolic links anymore as Unity does it all right for you).

Go to Lucas Meijer's blog post and download his Unity package: http://lucasmeijer.com/posts/one-click-unity-visual-studio-integration/

Windows is not able to create symbolic links in the same way as OSX so Meijer's script needs to be modified a little:

ProcessStartInfo t = new System.Diagnostics.ProcessStartInfo("c:\\WINDOWS\\junction.exe", "VisualStudio/AssetsLink Assets");

Now you can re-export the package for future use in other Unity projects. (Verified on Vista, should work the same for Windows XP)

Consolas Fontpack

Consider getting the Consolas Fontpack which looks much better than Courier.

Don’t forget to switch on ClearType – it’s not on by default. See instructions here: English German

Microsoft Visual Studio 2005 IDE Enhancements

You might also want to install:

Microsoft Visual Studio 2005 IDE Enhancements

InternetExplorer Developer Toolbar

If you’re also developing ASP.NET content, you may consider downloading and installing:

Internet Explorer Developer Toolbar

UML Modelling with UModel

If you’re developing complex applications, you may consider using UML to not model the various levels of the application in a more abstract manner than just code. I found Altova Umodel a very nice tool because it supports round-trip engineering and can be smoothly integrated into Visual Studio .NET 2005 and 2008.

Altova UModel

VERY IMPORTANT: Use UModel only with version control (or make frequent backup copies of the relevant UModel project files). I almost lost a few hours of work because I rearranged the folder structure inside of Unity on time, and another time because somehow I opened VS.NET 2008 while my network connection from the VMWare to my Mac somehow was broken. The result was that UModel did not find any classes anymore and automatically removed all the classes from all the diagrams. Fortunately, I made a copy of the UModel project file before saving this disaster. So make sure you check in the UModel project file after you’ve worked on it, so when UModel does weird things, you don’t lose your precious time...

Note: This never happened to me again, so I'm not saying UModel is buggy or something, but it's just wise to be a bit cautious with software ;-)

Using Visual C# Express As the code editor

If you are using Windows and are editing C# scripts in Visual Studio or C# Express, the new "Assets->Sync VisualStudio Project" option is nice. However, using Visual C# Express edition, you are limited in that you cannot double-click on a script or warning/error in Unity and have it open in Visual C# Express. Here is a workaround for that limitation:
1) Download ClassExec and extract to a folder of your choosing.
2) Create a batch file and put the following line :

"<path>\classexec.exe" %1 --class .cs

3) In Unity, under Edit->Preferences->External Script Editor, select the batch file you created above.
4) In Unity, after using 'Assets->Sync VisualStudio Project' and opening the solution in Visual C# Express, double clicking a script or a warning/error in the console will cause it to open in the same instance of Visual C# Express, allowing you to enjoy easy integration between the two IDEs.

As an alternative, instead of downloading the ClassExec program you can:

1. Skip downloading ClassExec
2. Create the batch file, as above, but with the following line :

Start "" %1

3-4. Same as Steps 3-4 in the instructions using the ClassExec

This method works for both Windows 7 and Vista. Haven't tested other systems.

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox