Using Visual Studio 2013 with Unity3D

If you’ve done any game development or researched game development platforms, you’re probably familiar with Unity3D. Unity 5 was recently announced, and if you haven’t yet downloaded it, get it now. Really, I’ll wait. You owe it to yourself to play with software this awesome. And you can’t beat the price (free).

One of the (many) things that makes Unity3D so incredible is the support for multiple languages, one of which is C#. When you install Unity3D for Windows, MonoDevelop comes as the built-in code editor, and while MonoDevelop is a powerful IDE, if you’re developing for Windows, you’re most likely accustomed to using Visual Studio. Previously, there was a nice little add-in tool you could purchase called UnityVS which enabled development in Visual Studio. The bad news is that UnityVS is no longer available. The good news is that Microsoft purchased the company and now the Visual Studio 2013 Tools for Unity are completely free!

Here’s a quick walkthrough on how to configure Unity to use Visual Studio 2013 as the external editor. As you build projects and develop scripts in Unity, the Console window will provide feedback. In the image shown following, the Console is indicating that I have a script that is throwing an error:

If I double-click the error, Unity sends me over to MonoDevelop to help locate the problem. In the following screenshot, you can see the MonoDevelop IDE, and while it opened the scripts from my project on tabs, it didn’t take me where I needed to be – in my “InstanceAnObject” script. I had to click the tab to get over there, and while the Console window told me the error is on line 44, there isn’t much indication here of what the problem is without jumping back and forth.


Let’s fix it! Fire up Visual Studio 2013 and from the Tools menu, select Extensions and Updates.


When the Extensions and Updates window opens up, select Online on the left side of the window, and then type Unity into the search box at the top right. The search results will fill in and you’ll see the Visual Studio 2013 Tools for Unity item in the list. Click the Download button to download the .msi installer. Once downloaded, you should exit Visual Studio before installing the extension by double-clicking the .msi file.


Now that the tools are installed, hop back over to Unity 5, and select Preferences from the Edit menu.


The Preferences window offers several pages of options. The one we’re concerned with is the External Tools item. Once you’re on the External Tools page, the very first drop-down you see is External Script editor. By opening the drop-down, you may see a few items in there, but likely not Visual Studio 2013, so let’s add it. Select Browse from the drop-down, then navigate to your install location for Visual Studio 2013. If you installed VS with the defaults, it’s likely located at C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE. Select the devenv.exe file and click Open. The drop-down will now add Visual Studio 2013 to the list of available editors.


Now when I go back to the Unity Console window and double-click the error, Visual Studio is launched, I am taken right to the script file containing the error, and the familiar red squiggly underline lets me know right where the problem is.


If I hover my mouse over the underlined code, I see the long description of the error to try and help me figure out the resolution.


In this case, the script was using Unity’s version of Random, which does not contain the NextDouble() method like C#’s. To clarify that I want to use the C# Random, I just added “System.” prior to the Random argument in the method signature.


If I save my script file and head back over to Unity, my Console window updates and no longer shows any errors, so I know I’m good to go.


MonoDevelop is a great tool that is more than adequate for developing Unity3D scripts, but if you’ve been using Visual Studio for a long time, it’s easy to make use of your familiar toolset while you’re developing the next great game.

Open-sourcing Fireside Firmware

One of the questions I was asked about my “Fireside: Cooking with Open Source” session was around the software and availability of the code.

The session covers in pretty good detail what you need to build the Arduino-based remote temperature monitoring device. I also covered the bits necessary to communicate with the device from a Windows (8/8.1) application. One thing you’ll need, however, is the firmware to put on the Fireside to make it work.

And so today, I’m happy to say it’s open source. I’ve had people emailing me asking about building up a back-end, augmenting the functionality, UDP, etc. So now’s your chance to pull the code and make it sing.

Just do the community a favor and branch/push your changes back so the Windows IoT community can benefit.

Remember that you’ll need the Arduino coding environment/compiler or a suitable Visual Studio plug-in to really go anywhere with the code.

The code is up on CodePlex at

Fireside Wiring Diagram and Schematic

Heya – some good news! For those of you that caught my session on building your very own Arduino-based IoT temperature monitoring device for Windows at the MVP Virtual Conference, I have the wiring diagram and schematic to share with you! You can get the parts list out of the session video – I covered each piece in pretty good detail, so you should have a good starting point.

Which one do I want?
As I mentioned, I’m sharing both the wiring diagram and schematic with you. If you’re new to hardware/Arduino and want to go the forgiving, non-permanent route, grab the wiring diagram. This is the one you would build up on a bread board, and involves little to no soldering. You can concentrate on getting things hooked up and not sweat it if you make a mistake.

If you’re more comfortable with soldering and OK with a less forgiving path, then the schematic is for you. Any mistakes are fixable – it just takes a bit more effort. The end result is a more durable, compact device.

With that said, here are the diagrams. Have fun!

Wiring diagram:


Universal Apps and Unity3D v5 Interop – Part I

From a Windows/Windows Phone developer’s perspective, one of the really cool things about Unity3D is how nicely it integrates with the XAML front-end and/or code-behind of Universal Apps. The UI tools in Unity have come along nicely in v5, but if you prefer to create a XAML UI or make use of some system-specific features such as Cortana integration, the connection between Unity and the application is pretty smooth. If you do go the XAML UI route, an important thing to keep in mind is that there is a performance hit. You can decide whether or not it’s a hit your app can afford.

There are a couple of different ways to connect Unity to your Universal App – this blog post will cover one of them, and I’ll follow up with an additional post that demonstrates another.

To get started, fire up Unity, and create a new project. For the sake of this post, I called mine “Interop_01″. Hit Ctrl+S to Save the default scene – I made a folder inside of the Assets folder called “Scenes”, and named my scene “Scene01″ (you have to appreciate creative naming).

For the purposes of the demo app, we’re going to be updating the text property of a Unity TextMesh object from the MainPage of our app. With that said, we need a TextMesh, so select GameObject -> 3D Object -> 3D Text to drop a TextMesh into the scene. You should see the default “Hello World” text show up. I made a couple of tweaks to my text object to make it a little easier to see, but the only important thing is that the text is visible in the Game window. If yours is not, use the translate controls to move it in front of the camera.

To do the interop, we’re going to create two scripts. One script will contain static members accessible to both the Universal App and the Unity app, and the other will run inside Unity on our TextMesh object.

I like to store my scripts in a “Scripts” folder, so in the Project window, select Create -> Folder, and name it (wait for it…) “Scripts”. Now right-click the newly create Scripts folder and select Create -> C# Script. Name the script “Interop”. Open the Interop class for editing in your IDE of choice – MonoDevelop is great, though I prefer using Visual Studio paired with Unity, with the Unity Tools for Visual Studio installed.

When the interop class opens, remove the default Start and Update methods since we won’t be using them. Instead, we’re going to add a new static member of type string that will be updated from the Universal App:

public static string TextToDisplay;

Save the file and head back over to Unity. Create another script, and name it “UpdateTextMesh”. The way the interop will work is that the Universal App will update the TextToDisplay string within the interop class, and the newly created UpdateTextMesh class will update the text property on the TextMesh object from the interop class. In order to update the TextMesh, we need a TextMesh object in our UpdateTextMesh script, so go ahead and declare one:

private TextMesh displayText;

This script is going to be attached to the Unity TextMesh we want to update. For the script to tie into the TextMesh object’s properties, it needs a reference to the TextMesh object. When a script is dropped onto an object in Unity, it has access to other parts of the object via the GetComponent() method. Along those lines, the code shown following was added to the Start() method:

displayText = GetComponent<TextMesh>();

The “displayText” object is now the TextMesh component of the object to which the script is attached. The last bit of code needed inside this script will handle updating the displayed text when the Interop text has been changed. To do this, add the code shown following to the Update() method. This code simply checks to see if Interop.TextToDisplay is null or empty, and if not, assigns the value to the text property of the displayText object.

if (!string.IsNullOrEmpty(Interop.TextToDisplay))
    displayText.text = Interop.TextToDisplay;

Now we just need to wire everything together. In Unity, drag the UpdateTextMesh script from the Project window onto the TextMesh object you created earlier:

Drag the script onto the TextMesh object

After dragging the script onto the object, you will see the script listed in the Inspector window.

The script shows in the Inspector

That’s pretty much all that needs to be done in Unity, but we do need to build the project to get at the rest of the project. From the File menu, select Build Settings. Choose Windows Store under Platform and for SDK, pick Universal 8.1. Click the Build button and choose a folder in which to store the build.

Once Unity has finished building the project, a File Explorer window will open showing your build folder. Double-click the folder, then double-click the solution file to open it. In this case, the solution is called Interop_01.sln.

Open the MainPage.xaml.cs file, and in the MainPage constructor, add a Loaded event listener by typing Loaded +=, then hitting the Tab key twice. Add the code shown following to the Loaded event handler to update Interop.TextToDisplay:

Interop.TextToDisplay = "Hello from code-behind!"

Unity defaults to ARM for both the Windows and Windows Phone builds, so before you can run the app, you’ll probably need to fix that. From the Build menu, pick Configuration Manager. For the Windows project, select “x86″ from the Platform dropdown. Click Close, then press F5 to build and run the app locally. Keep in mind since this is a Universal App, you’ll need to either have the Windows Phone emulator installed, or a device connected to your system.

When the app is done building and runs, you should see the following:


It works, but probably isn’t a very practical example of how you’d use interop. I started out this post touching on XAML UI over the top of a Unity application. This app demonstrates a simple text update, but not really any UI. It is possible to build XAML UI over the top of a Unity app, but you’ll need to tweak the XAML a bit. I added a TextBox and Update button that allows a user to type text into the app and update the underlying Unity app, by editing the code on the MainPage.xaml file. The Unity app runs in a DXSwapChainPanel – wrapping the panel in a Grid allows me to add UI over the top:

    <SwapChainPanel x:Name="DXSwapChainPanel">
        <Grid x:Name="ExtendedSplashGrid">
            <Image x:Name="ExtendedSplashImage" Source="Assets/SplashScreen.png" VerticalAlignment="Center" HorizontalAlignment="Center"/>
    <StackPanel Background="Black" Orientation="Horizontal" VerticalAlignment="Top" Height="70">
        <TextBlock Foreground="White" Text="Text to display:" FontSize="24" Margin="20,20,10,0" />
        <TextBox x:Name="textToDisplay" Width="400" Height="30" VerticalAlignment="Top" Margin="0,20,20,0"/>
        <Button x:Name="btnSend" Content="Update" Width="100" Height="40" FontSize="12" VerticalAlignment="Top" Margin="0,16,0,0"/>

I wired up the click event on the button to update the text in the same manner used earlier, only now the text comes from the XAML TextBox:

Interop.TextToDisplay = textToDisplay.Text;

And now when we run it, the app looks similar to the screen below:

The project file can be found here.

Expand/Collapse all nodes in C1TreeView for Windows 8.x

I have a hobby project I’m working on and part of the application utilizes the excellent ComponentOne XAML TreeView control from the Studio for WinRT XAML control set. One thing I needed that isn’t a part of the control is an ExpandAll() method to open all of the nodes on the tree. I ended up rolling my own based on a Silverlight example I found and wanted to share it here in case anyone needed to do something similar.

The first thing I did is start with a List to hold all of the C1TreeViewItem objects in the TreeView:

List<C1TreeViewItem> itemList;

The ExpandAll() method is shown following. The method starts out with a check of the itemList to see if it’s null or empty. If so, the List is initialized.

After that, I grab the Iitems in my TreeView control, named “MyTreeViewControl”, into _itemList. Once I have those, I can use a foreach loop to pass each item to another method called ExpandNodes(). Note the else clause on the if – the itemList object is populated with the C1TreeViewItem objects the first time through so it’s not necessary to do so each time I want to expand (or collapse) the tree.

public void ExpandAll()
    if (itemList == null || itemList.Count == 0)
        itemList = new List<C1.Xaml.C1TreeViewItem>();
        var _itemList = MyTreeViewControl.Items.ToList();

        foreach (C1TreeViewItem item in _itemList)
        foreach (C1TreeViewItem item in itemList)
            item.IsExpanded = true;

As each C1TreeViewItem is passed into ExpandNodes(), it has the IsExpanded property set to “true” to open the node, and is added to the itemList object in case it needs to be collapsed or expanded again later.

Since the node that is being expanded may have children of its own, I grab the Items from that node and run them through a foreach loop that recursively passes any children into ExpandNodes() until all the nodes are processed.

public void ExpandNodes(C1TreeViewItem item)
    item.IsExpanded = true;

    var _itemList = item.Items.ToList();

    foreach (C1TreeViewItem childItem in _itemList)

Collapsing all of the nodes is as simple as changing the IsExpanded property to “false”. If you needed to be able to both collapse and expand the nodes, the ExpandNodes() method could easily be generalized to use a Boolean in a conditional statement to flip the IsExpanded property without the need for separate methods.

LicenseInformation for in-app purchases

In an earlier blog post, I alluded to a gotcha I came across while developing a Windows Store app that had some in-app purchase options in it.

Part of the process of developing an app that includes in-app purchases is being able to test them locally with a simulated license vs. the actual license.

Declaring either is pretty straightforward:

public LicenseInformation simulatorLicense = CurrentAppSimulator.LicenseInformation;
public LicenseInformation currentAppLicense = CurrentApp.LicenseInformation;

When you’re developing, you use the CurrentAppSimulator license, and before you release, you switch to the CurrentApp license.

The gotcha is that while your app will compile and run fine if you declare both and utilize an “if” statement to switch between them, it only works because it’s local – you won’t make it through the certification process, and there is no specific error provided.

Note that even just having the CurrentAppSimulator declaration will cause problems – which is what I ended up experiencing by inadvertently not commenting out a line of code. So don’t do what I did.

Repeating a sound in Unity3D

I was working on a project in Unity3D recently and needed a way to repeat a sound several times in a row. I did the usual bit of searching, but didn’t really come up with a solution that fit my needs.

I ended up writing a small routine that is passed an AudioClip object and the number of times you’d like it to repeat, and it plays the sound. I code in C# in Unity3D, but I’m sure the code could be adapted to other languages as needed.

IEnumerator RepeatSound(AudioClip whichSound, int numOfTimes) 
    for (int i = 0; i < numOfTimes; i++) 
        AudioSource.PlayClipAtPoint (whichSound, transform.position);
	yield return new WaitForSeconds (.1f);

The code shown should be fairly self-explanatory – it just runs through a quick for loop a number of times, plays the specified AudioClip, then waits before playing it again. Without the wait, the sounds would basically all play at once.

I had a very specific need for this, but one small change I would make to enhance the code would be to pass in a float for the wait time. Then if you have a sound that is 1 second long, passing in a 1 second wait time would line the audio up nicely, one after another.

As-is, calling the RepeatSound method is pretty straightforward:

StartCoroutine (RepeatSound (myAudioClip, 8));

Adding a privacy policy to a Windows 8 application

I recently released a lottery tracking/analysis app called Lady Luck into the Windows 8 Marketplace. I initially failed the certification testing due to a missing privacy policy (amongst other things for future posts). A simple oversight on my part that cost me a bit more time before my app was released.

Any application going in to the Marketplace that uses network capabilities is required to provide a privacy policy on the app’s description page as well as through the Settings Charm. See section 4.1.1 here for more info.

Application capability declarations that make your app network-capable include: internetClient, internetClientServer and privateNetworkClientServer. The Internet(Client) option is enabled by default. If you need to double check your capabilities, open the package.appxmanifest file in your solution and select the capabilities tab.

Capabilities Panel

Fortunately, adding the privacy policy is fairly straightforward and only takes a few minutes.

Start by declaring a Settings Pane object:

SettingsPane settingsPane = SettingsPane.GetForCurrentView();

Inside the constructor, wire up the CommandsRequested event listener:

settingsPane.CommandsRequested += settingsPane_CommandsRequested;

I’m keeping my privacy.txt text file in a folder in my project called privacyPolicy. To grab the contents of that file and display them on the Settings pane, I have the following code as my CommandsRequested event handler:

private void settingsPane_CommandsRequested(SettingsPane sender, SettingsPaneCommandsRequestedEventArgs args)
            SettingsCommand settingsCommand = new SettingsCommand(
            "Privacy policy",
            async command =>
                var settingsFlyout = new SettingsFlyout();
                settingsFlyout.Title = "Privacy policy";

                var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///privacyPolicy/privacy.txt"));
                var properties = await file.GetBasicPropertiesAsync();
                var stream = await file.OpenAsync(FileAccessMode.Read);
                var reader = new DataReader(stream.GetInputStreamAt(0));
                await reader.LoadAsync((uint)properties.Size);
                string policyText = reader.ReadString(reader.UnconsumedBufferLength);

                settingsFlyout.Content = new TextBlock()
                    Text = policyText,
                    TextAlignment = TextAlignment.Left,
                    TextWrapping = TextWrapping.Wrap,
                    FontSize = 14


And now when I run my app, if I swipe from the right edge of the screen and choose Settings, I get the following pane – note the Privacy Policy entry:

Settings Pane

By selecting Privacy Policy, my policy is displayed on the pane:

Privacy Policy

Here’s the sample project if you need a quick implementation.

I’m stuck listening to KC and the Sunshine Band because I don’t have an iPhone

Not that there’s anything wrong with KC and the Sunshine Band, but it’s all about choice, right?

I was in Bellevue, Washington recently for the Microsoft MVP Summit. It was a great opportunity to renew some connections with old friends as well as make some new ones. I spent some time one evening at Lucky Strike – a sort of bowling alley, video arcade, restaurant/bar combination.

Behind the bar there was a large screen advertising ROCKBOT – a “social jukebox app”. The screen said all I had to do was download the application to my smartphone and I could pick songs to play. What a great idea – I’m a big fan of the Internet-connected jukeboxes popular in a lot of places now as the library of music is huge. This is the next best thing – I could pick music and not even have to leave my seat!


No support for Windows Phone (sad trombone!). I was struck by two things – first, somebody with an iPhone or Android (probably iPhone) CHOSE to listen to “That’s the Way I Like It” (uh-huh, uh-huh!), and second, I was basically on Microsoft’s campus and my Microsoft-powered device wasn’t supported. I understand that ROCKBOT is a company out of California, and compared to the aforementioned Internet-connected jukeboxes, they are probably not in a majority of the market, so probably don’t have money to burn as small to mid-sized business aren’t going to divert a ton of resources to develop for a small slice of the market.

This leads me to the point of this post – how to encourage small to medium-sized businesses to support the platform. Windows phone adoption is increasing rapidly in Europe with several markets now in double digits. There are wonderful apps out there like SongArc that have garnered half a million downloads in less than 8 weeks. Think about those numbers for a moment – 500,000 downloads in less than 8 weeks. The right app in a less crowded marketplace gets a whole lot more attention than apps in an over-saturated market.

An argument against the Windows Phone platform I see often is Windows Phone “doesn’t have the apps”. In contrast, Apple has the apps, 2/3rds of which have never even been downloaded.

So what do I (we) as Windows Phone users/developers do? Wait it out? Ask ROCKBOT “pretty please can I have access to your APIs so I can write your software free of charge”?

Neither one of these seems to serve the long term interests of the platform – waiting it out means it may never come around. Writing apps free is a labor of love, and while there are some amazing success stories where the 3rd party app is better/more popular than the official one (see: YouTube, Instagram, etc.), it can become a time-consuming endeavor. Plus, as a developer, the idea is to get paid for your work, right?

So what’s the answer to helping more companies find their way into the Marketplace?