Special connect() issue 2014

Volume 29 Number 12A

Cross-Platform : Cross-Platform Game Development with Visual Studio Tools for Unity

Adam Tuliper; issue 2014

I’ve noticed some interesting things in my foray into game development. A friend created an Android game that recently hit No. 1 on the Google Play Store. I have some other friends at another indie game company who have reached a top spot on iOS. I know yet another who has more than 500,000 players on a Windows Store game. They all share something in common regardless of the platform they build for: They all use Unity on Windows to develop their games. (That’s Unity the game engine, not the dependency injection package from Microsoft.)

Unity on Windows accounts for a whopping 88.6 percent of Unity projects, far exceeding OS X (stats.unity3d.com). I find that stat quite interesting because Unity is the most popular cross-platform game development middleware. On the mobile side, iOS takes up about 26 percent of the mobile platform usage category, with Android at 71 percent, yet Unity on Windows attracts the largest number of developers, demonstrating Windows is still the No. 1 platform on which developers want to code.

Visual Studio Tools for Unity (VSTU) is a software package (formerly known as UnityVS) that Microsoft obtained through the acquisition of SyntaxTree in 2014. This product used to cost about $100 and allowed Unity developers to use Visual Studio to not only edit code, but also to debug code. Why is this a big deal? First, because Microsoft made this great tool 100 percent free. Second, it’s being actively developed by the SyntaxTree team that joined Microsoft. Third, it’s great because I can use the familiar code editor/debugger to develop my games. As soon as Visual Studio 2015 Preview bits were released, the update to VSTU was released to support it, as well.

In Unity, for any GameObject you want code assigned to, simply add a script component to it. To edit that code, you double-click it. Unity then opens MonoDevelop by default (which installs with Unity) to edit and debug code. For me, there’s a bit of friction here when developing this way. I think the MonoDevelop project has done some great work, but Visual Studio is a more full-featured tool. I want to use Visual Studio to write and debug code, just like I have for the past 18 years. Previously in Unity, you could use any editor (such as Visual Studio, Sublime and so on) to edit your Unity code, but you couldn’t debug your code from Visual Studio. That means you would edit code in Visual Studio and switch back to MonoDevelop to debug, or drop Debug.Log statements everywhere, which isn’t the most productive way to debug.

Many readers of MSDN Magazine are enterprise developers. Many I speak to have wondered for a long time about writing games, especially after XNA went the way of the dodo. For those folks, I recently did a four-part series for the magazine on game development with Unity (msdn.microsoft.com/magazine/dn759441) to help new audiences get introduced to the experience of game development on Windows with Unity. Most enterprise developers I know use Visual Studio at work, but can’t use it at home for game development because their organization is the license holder. MonoDevelop works in this case, but it doesn’t provide the Visual Studio experience. Microsoft recently released Visual Studio Community Edition, a free version virtually the same as Visual Studio Professional. It’s awesome for small teams or the individual developer because it’s free and can run VSTU. Coupled with the free version of Unity, you have a completely free multiplatform game development system using Unity and Visual Studio. For those wondering about Visual Studio Express, that product doesn’t support plug-ins, so you can’t run VSTU in it. There are no future versions of Visual Studio Express planned because Visual Studio Community Edition is the future—and a great future it is.

Why Use VSTU?

If you’ve developed great games with Unity and MonoDevelop, why would you use something different? What does VSTU give in addition to MonoDevelop? Well, the IntelliSense is better. The code formatting works better. You can use plug-ins such as ReSharper to refactor and enhance your code. If you’re used to Visual Studio, you’re surely used to the Solution Explorer, of which Mono­Develop has a limited version, but which doesn’t provide nearly the same experience. There’s a better debugging experience through the Output window in Visual Studio, as well, so you can see the result from commands such as Debug.Log without having to switch back and forth to Unity to view the output as you normally would. VSTU also enhances the debugging experience by providing better visualizations for collections, such as lists, dictionaries and the often-used (in Unity) Hashtable. Unity contains functionality for its own SerializedProperty, which allows your custom editor extensions to read values in the inspector. This type displays as expected with VSTU, but when being visualized in MonoDevelop, it spews lines and lines of errors in the Unity console.

Also, with the release of Visual Studio 2015 Preview, there’s a great new feature added to VSTU: syntax highlighting for shaders (see Figure 1). While it might not be considered a huge productivity booster, it’s significantly easier on the eyes when writing shaders, and it was a top-requested feature on the UserVoice site used to gather feature suggestions and feedback. Anyone who has used modern tools with syntax highlighting and then had to develop without it quickly misses the functionality, so a lot of you will cheer this release.

New Syntax Highlighting for Shaders in Visual Studio 2015 Preview
Figure 1 New Syntax Highlighting for Shaders in Visual Studio 2015 Preview

How Does It All Work?

Before further exploring new features, I’ll discuss how the “magic” behind the tool works. Unity contains a virtual environment inside of its editor that’s controlled by a soft debugger. When you debug a game, you aren’t debugging Unity.exe, as you might expect, because you can attach a debugger to a process from Visual Studio. Instead, you’re connecting over a port to Unity and then sending commands to the Unity soft debugger running inside of the editor. MonoDevelop ships with a MonoDevelop.Debugger.Soft library that understands the binary format used to talk back to the Unity soft debugger. Unity adds a thin wrapper around this library to support custom actions and distributes that with MonoDevelop, as shown in Figure 2. The plug-in can launch Unity or just connect over a port to talk to the soft debugger (to check out the code, see the GitHub project at bit.ly/1zY6K2a). The good folks working on VSTU have in turn written an extension for Visual Studio that uses this library, as well (see Figure 3). It allows you to not only use Visual Studio in all its code-editing, refactoring, syntax-highlighting and IntelliSense glory, but to also debug Unity code using the debugging experience you’re used to in Visual Studio—and it works better than what’s built in to the default Unity  distribution. 

The Unity Plug-in for Debugging from MonoDeveloper
Figure 2 The Unity Plug-in for Debugging from MonoDeveloper

The Microsoft Visual Studio Tools for Unity Extension in Visual Studio 2015 Preview
Figure 3 The Microsoft Visual Studio Tools for Unity Extension in Visual Studio 2015 Preview

Unity creates its own .csproj files to manage its Mono compilation and some other .csproj files for adding to Visual Studio if you want to use Visual Studio as an editor only. I covered a bit of this Unity architecture in the first article of my Unity series earlier this year. VSTU creates its own versions of the .csproj files, as shown in Figure 4. It might seem like there can be many .csproj files to manage, but the details are handled behind the scenes so you don’t have to bother with them. The usual F5 debugging in MonoDevelop doesn’t always work. Unity will always do its own compilation on your code, but when doing an F5 to debug in MonoDevelop, it will do an additional compilation on the binaries and might error out on some syntax that Unity actually didn’t mind and, thus, fail to debug. Every time you start to debug in MonoDevelop, you have to attach to the Unity.exe process (which in turn connects to the port for debugging). This differs from the “press F5 to debug” you’ve grown used to in Visual Studio. I’ve also locked up Unity quite a few times while using MonoDevelop to debug, something that hasn’t been an issue with VSTU on Windows.

Visual Studio Tools for Unity Creates Its Own Versions of the .csproj Files
Figure 4 Visual Studio Tools for Unity Creates Its Own Versions of the .csproj Files

Using VSTU

VSTU is simple to use, but it does warrant a brief explanation of its Unity Editor because it might differ from what you expect compared with something such as a Visual Studio extension. A Visual Studio extension can just be installed once and then be active for each project, as with VSTU. The Unity environment is extremely scriptable with C#, which makes it powerful, but for each project you want the functionality in, the architecture requires you to import the required VSTU package. This is a one-time operation for each project. Simply choose the VSTU package from Assets/Import Package, as shown in Figure 5. This decompresses the package and tells you the files about to be imported. That’s it.

Importing the Visual Studio Tools for Unity Package into Unity
Figure 5 Importing the Visual Studio Tools for Unity Package into Unity

You can also install any .unitypackage file into Unity by double-clicking on it, so for VSTU, you could also just double-click on the “Visual Studio 2015 Tools.unitypackage” file in your file system. Once installed, to edit code you simply double-click on a script file in Unity and it will open in Visual Studio with VSTU active. You must remember to install the VSTU package into your Unity project, or Visual Studio will display an ambiguous error that might leave you a little confused. It’s also worth mentioning that if your code doesn’t compile when you attempt to open it for the first time, the Visual Studio projects won’t generate and, therefore, no project loads in Visual Studio, so you’ll receive the same ambiguous error. This also happens in Unity with your MonoDevelop projects—they just won’t generate if your code initially has an error prior to first-time project generation (which, again, happens behind the scenes). These are just a few things I’ve experienced of which you should be aware.

Mac Usage

What about you Mac users out there? Because Visual Studio runs on Windows and not OS X, how do the other roughly 11 percent of developers not using Windows enhance their development experience with Unity? You can run Windows in a virtual machine (VM) and share folders so you can switch back and forth for debugging. There’s a little friction with that method, too, though. There’s a better way. Because VSTU connects to Unity over a port, technically you can do cross-machine development. With virtualization software such as Parallels and VMware on a Mac, you can run software from your guest VM and have it show up in your host OS desktop just like it was running natively there in a window. This means Visual Studio can run on your OS X desktop just like any other OS X application and connect to Unity running on your Mac to debug it from Windows. Yeah, that’s pretty cool.

The Cross-Platform Approach

We certainly live in an amazing time of multiplatform app development. Sometimes, though, this app dev landscape feels like we’re living in a single land with many different currencies, and some vendors take some and not another, and everyone speaks a different language. There are so many different tools, platforms and devices, you could keep busy for life just learning them all. At Microsoft, we’ve worked closely with many partners to bring cross-platform development to our tooling and platform for an integrated experience. I can spin up a Linux VM on Microsoft Azure. I can use Visual Studio to debug Python scripts (even running remotely in the cloud), and I can read great documentation on microsoft.com about setting up push notifications on an iPhone device. On top of all that, .NET is now open source, ASP.NET runs on Linux and OS X, and Visual Studio has an Android emulator. I certainly didn’t foresee this day, and I believe I just saw a pig fly by my window outside.

Unity, of course, is my favorite cross-platform game development tool, although I have seen impressive implementations of business apps with advanced 3D capabilities that have been done in Unity. Unity supports 16 different platforms—yes, you read that correctly. That’s quite impressive and you’re likely curious what makes up that number, so here you go: iOS, Android (including devices such as OUYA), Windows, Windows Store, BlackBerry, Windows Phone, OS X, Linux, Web (via plug-ins previously and now HTML5/JavaScript/WebGL), PS3, PS4, PS Vita, PS Mobile, Xbox One, Xbox 360 and WiiU.

You also have several great choices for developing cross-platform applications solely with Visual Studio. The flagship Microsoft IDE now has integration with Apache Cordova, allowing cross-platform HTML-based applications (not Web sites—these are apps) to be developed from within its familiar environment. You can debug these applications from within Visual Studio for Windows, Android and even iOS. For an example of this, check out the MSDN Library article, “Run Your Apache Cordova App on iOS,” at bit.ly/1ycNUVD.

In the cross-platform app space, the leader is Xamarin (pronounced zam-a-rin for those who still are unsure). Xamarin isn’t gaming-focused, although you definitely can create games with it. The tooling can integrate with Visual Studio, as well, for excellent cross-platform app development, which even allows you to design a UI for iOS and Android all from within Visual Studio. Xamarin has had since its inception part of the original Mono team, so that expertise has served the company well. Mono is also what powers your custom game code and extensibility in Unity—it has come an incredibly long way since its creation in 2001.

For those who love C++ (you know who you are), I touched briefly on the Android emulator earlier, and it will support C++ apps. That’s right: Visual Studio now lets you create a native Android app via the Cross Platform | Native-Activity Application template. This allows development and debugging of Android code from Visual Studio starting with Visual Studio 2015 Preview. It was all I could do to not italicize that entire last line!

Wrapping Up

To review, Microsoft has been adding to its cross-platform story for some time, but now it’s in overdrive. VSTU is, of course, free and has been going through some great updates; you can find a history of them at bit.ly/15pC1RN. Visual Studio Community Edition is free. Microsoft is known for having great development tools and is committed to bringing the best development and debugging experience to game developers using Visual Studio. So I say to all: Use Unity, develop on Windows and use Visual Studio for an awesome development and debugging experience.

Additional Learning

Adam Tuliper is a senior technical evangelist with Microsoft living in sunny Southern California. He’s an indie game dev, co-admin of the Orange County Unity Meetup and a Pluralsight author. He and his wife are about to have their third child, so reach out to him while he still has a spare moment at adamt@microsoft.com or on Twitter at twitter.com/AdamTuliper.

Thanks to the following Microsoft technical expert for reviewing this article: Jb Evain