How to build a Unity game for Windows 10

There are various ways to develop apps and games for the Windows Platform. One of them is by using a great tool called Unity . Unity is a development environment and platform to build 3D and 2D games and interactive experiences. There’s a vast community of people creating, sharing and selling assets. The most awesome thing about Unity is that is has a free version you can use for your personal projects. It only shows a splash screen stating that the game is built with the personal version of Unity.

There are beautiful examples of games built with Unity (you definitely should look at the showcase ), but for now I’ll focus on getting started.

The game I’m going to build is a going to be a simple puzzle game, it will be a clone of Alien Tiles . I’m going to use Unity 5 and Visual Studio 2015, both running on Windows 10. The game will show a grid of tiles with a particular color. When the player clicks a tile the entire row and the entire column of the tile will cycle to the next color. The goal of the game is to get the entire grid to a specific color.

Setting up Unity

Let’s start by opening Unity and create a new project. I’m going to name my game “ColorfulTiles” for now. To have Unity preconfigured for a 2D game, I select 2D from the options.

Unity Welcome Screen

I also want to include the Visual Studio 2015 Tools Asset Packages, by clicking the Asset Packages button and checking the package “Visual Studio 2015 Tools”. And hit “Done” to close the package select window.

Asset Packages

 

Now hit “Create Project” to create the game and get going.

I won’t be going over the various panels of Unity in detail in this tutorial, so I can focus on building the game. But, let me know if you would like me to write a tutorial about the details of the UI of Unity.

When starting a new project in Unity I always start by creating a few folders: Scenes, Prefabs, Materials and Scripts. Depending on the game I’ll be building I might add other folders (for sounds, textures, etc.), but for now this would be sufficient. You can add folders by right-clicking the project panel and going to Create -> Folder in the context menu.

Folders

 

Graphics

Before I start on the level generator I would like to add the tile to the game. I do this by adding an “empty game object” to the scene, by right-clicking on the hierarchy and selecting “Create Empty” from the context menu. I rename this GameObject to “Tile”. Now the graphic itself could be anything from a 3D object to a sprite. In this first version of the game I use a simple quad. I add one to the “Tile” object I just created by right-clicking the “Tile” and selecting “3D” -> “Quad”. I renamed the quad “TileGraphic”. I added the quad to an empty GameObject to be able to change the graphic in a later stage without needing to change too much in the game. Scripts, translation and scaling will be done on the root object of the tile and not on the graphic. Actually, let’s change the scale of the “Tile” object to 0.9 on all axis. This way I can create a grid of 1 by 1 tiles and have a little space between the tiles.

You might be wondering why I add 3D object to a 2D game. The 2D game is actually still 3D under the cover. Just by configuration, like setting the Camera to orthographic for example, the 3rd dimension somewhat hidden and object don’t appear smaller when further away from the camera.

To make the tile reusable I convert it to a Prefab. A prefab is an object that is stored separate and can be reused easily. To convert the GameObject to a Prefab just drag it from the hierarchy to the Prefabs folder. The GameObject in the hierarchy should turn blue indicating it is a prefab.

Create Prefab

 

I delete the “Tile” GameObject from the hierarchy now. It should still be available as a prefab.

Generating a level

Time to write some code. To generate a level we need to run some code. I added a new C# script to the scripts folder, by right-clicking the scripts folder and selecting “Create” -> “C# script”. I named the script “LevelController”. Double-click the script to edit it in Visual Studio, which may take a few seconds.

Just a Moment

 

After Visual Studio opens you’ll end up with a class, inherited from MonoBehavior. This class contains two methods. “Start”, which is called when the script is activated and “Update”, which is called every frame.

The first version of the LevelController just generates a level when the script is activated. It does this by going through two for loops. The two integer fields in the beginning of the class define how many rows and columns the grid will contain. By making these public Unity will show them in the Inspector so these values can be changed design-time. The third field, “Tile”, will be linked to the prefab we created earlier.

Inside both for-loops the Instantiate method is called. This method instantiates a new version of the GameObject passed as its first parameter. The second parameter of the Instantiate method is the position of where the newly instantiated GameObject should be placed. It seems like a whole lot of math for such a simple thing. All it does is centering the grid to the world. The +.5f is added because the tiles are positioned based on their center and not the top-left corner as you might expect. The last parameter, the Quaternion.identity, is the rotation. In this case it basically means to use no rotation.

Make sure the script is saved before heading back to Unity.

In Unity the script has to be attached to something. Because there’s not much in the scene at the moment, I just attach it to the camera by dragging the script onto it. When you look at the Inspector you should see the script, with its properties in there. Last thing to do is to drag the prefab created earlier to the “Tile” property of the script. By now, the inspector should look something like this:

Inspector

 

If you were following along you should be able to test-run the game now. You can do this by hitting the play button at the top of the screen in Unity.

Running in Unity

 

If you haven’t already, this would be a great time to save the scene. Just hit ctrl+s and save the scene under the scenes folder by the name of “Main”.

Running as a Windows app

To run the game as a Windows app we’ll have to change the build settings. You can find the build settings under “File”->”Build Settings…” in the menu.

Build Settings

 

First, make sure the scene is in the build. You’ll probably have to add it by clicking the “Add Open Scenes” button. Next, set the Platform to “Windows Store” and click “Switch Platform”. Change the SDK to Universal 10. To be able to Run and debug from Visual Studio, check the “Unity C# Projects” and “Development Build” checkboxes. Keep in mind that this will create a C# solution with everything you need to run the game. If you are looking to buy hacks on online games, visit iwantcheats.net for more information. The scripts we’ve edited before are not C# files contained in this project, because they’re Unity scripts that happen to be C# (although they’re linked in there as well).

Hit Build to build the project. You’ll have to specify a folder to build to. I usually create a new folder called “WindowsStoreBuild” and choose that. You can open the solution in Visual Studio and run it from there.

Running as UWP App

 

And that’s it for now. In the second part I’m going to add the various colors to the tiles and probably make them clickable.

Enable onscreen keyboard in VS Android Emulator

By default, the Visual Studio Android emulator sets its keyboard entry to the hardware keyboard attached to you PC. But it might be very useful when developing apps to work with the software keyboard on Android. In the Windows Phone emulator, you can use a page-up and page-down to enable and disable the keyboard. In the Android emulator you’ll have to change a setting in the OS itself.

To change the setting, got to the Android Settings and to Language & Input. Than select “Change Keyboard”.

Hit the switch to change the input method.

You’re good to go:

 

   

Convert json to TypeScript

While working on a Windows Store app I noticed I was writing TypeScript interfaces to get some syntax checking and code completion on JSON data received from an external service. In C# projects I’m used to use http://json2csharp.com to get the C# classes for my JSON and than just deserialize it to those types. I quickly searched the web and realized there wasn’t a similar solution for TypeScript.

Solution

http://json2ts.com was born. You just paste a block of JSON code into the text area and hit the generate TypeScript button to convert the piece of JSON to a TypeScript definition with interfaces.

Using the site

To use the result in you application you’ll have to add a “.d.ts”  file (declaration files) and just copy/paste the generated TypeScript from json2ts in there. You may have seen these files before, like for jQuery or KnockoutJS. These files only contain a description of types and are very useful when used next to a Javascript Library (for a lot of these have a look at DefinitelyTyped), but there are useful on the JSON result of a external service too.

Let’s assume there’s some 3rd party service you want to use in you application that return the following JSON:

This result is stored in the “result” variable. Now you can type “result.date” in your code to use the date value. But, TypeScript can give use code completion and syntax checking in Visual Studio. When http://json2ts.com generates the interfaces for this piece of JSON it looks something like this:

You can see the relations between the various types. When you place this in a “.d.ts” file and reference this file in the TypeScript file you want to use the definition you get all the benefit of TypeScript.

Wrap up

The site isn’t tested very much and I’m pretty sure there are some bug in it. If you have a piece of JSON that isn’t converting properly, please send me an email ( ts2json@timmykokke.com ). If you have any questions or feature suggestion drop me a line too. I probably going to add some “advanced” features to set the module name and root object names.