This is an excerpt chapter from my Weekend Code Project: Unity’s New 2D Workflow book. I hope you enjoy this free content on how to use Unity’s new 2D features. If you like what you see, please pick up a copy of the book to get access to the artwork, source code and a PDF/ePub version of the book.
Right now, Unity is the de facto game development framework and IDE for a lot of the success stories you read about on multiple platforms. The IDE is very polished and easy to use. Previously, with Unity being a 3D tool meant there was a certain level of knowledge you needed before getting started. Now with the addition of an all-new 2D workflow, things have gotten a lot easier for game developers looking to build simple, non-3d games.
Unity supports three languages: Unity Script (which is similar to JS), C#, and Boo. Unity now has a free version that supports exporting to desktop and mobile but forces you to display the Unity logo when the game starts up. The Pro version gets pricey, but adds lots of must-have features for more advanced game developers. There are also additional licenses you can get that export to console, such as the Xbox, PlayStation, Wii U, and more, but those licenses cost even more. Unity has a Web Player that relies on a plugin, so while you can export your games to the Web, they won’t be playable on mobile browsers.
Over the next section, we will take a look at the IDE itself and how to navigate around it. There are a lot of resources out there on Unity, but for people who have never opened it up before, this will help get you acquainted with the basics in the IDE.
Before We Get Started
In this book we are going to create a game called Super Paper Monster Smasher. The goal of the game is to keep the monster, which you control, alive for as long as possible from the endless onslaught of knights out to kill you. Originally this was a game I created during a 48 hour game jam called Ludum Dare but since then I have turned it into a demo that is perfect for learning the core skills needed to make a game such as:
- Managing game assets
- The basics of animation
- Building a level
- Moving the Player
- Collision detection
- Spawning enemies
- Changing from scene to scene
As you can see, we have a lot to cover, but before we begin we need to learn a little bit about how Unity works and scripting with C#.
Creating a New Project
When you create a new project in Unity, you will see the following wizard:
As you can see, you are given an option to set the location of where you want to create your project, as well as packages you can include when it is created. The final thing to note, which is new in Unity 4.3, is the 2D setup tab at the bottom of the window.
By setting this to 2D, your project will automatically be configured for 2D game development. Let’s create a project called SPMS2D and toggle 2D.
Once you create your project, you will go into the editor and see the “Welcome To Unity” screen. I highly suggest going through some of the videos to learn more about how Unity works and get a sense for the workflow. Most of it is geared toward 3D, but it still applies to the stuff we will go over in this book. I also suggest checking out the forums since there is a lot of really good information on there that can help you out when you get stuck as you are getting started learning Unity.
How to Use the IDE
At first, Unity may be a little intimidating, but I personally find that the new 2D mode actually simplifies things greatly. Here is a high-level overview of the IDE. When you first open up a project in Unity you will see the following window:
Let’s go over each section of the IDE’s window by window. First, we’ll start with the main area:
The main area is where you will lay out objects, preview, and test your game, work on animations and other visual-based activities. As you can see from the tabs, the Scene and Game tabs are already open. Below the tabs, you will see a few quick-access menus. The most important is the 2D toggle, which is already activated. By unchecking it, you will go back into Unity’s native 3D view.
I’m not going to go into the 3D navigation tools since we will be focusing on 2D instead so let’s look at the Hierarchy panel.
As you add stuff to your scene, you will be able to see and access them from here. For now there is a single object, which is the camera. Also, you have access to a Create shortcut menu, which we will be using a little later on. The same options can be accessed in the IDE’s top menus as well.
While you are looking at the Hierarchy tab, select Camera so we can discuss the Inspector panel next.
Here you can see all of the properties that make up the camera for our game. Unity does a great job of visualizing all the parts that can be configured on each object in your Scene. The Inspector panel not only allows you to modify values, even on the fly while the game is running, but it also lets you add additional functionality onto any GameObject via the Add Component button.
We will get into some of these components a little later on, especially scripts, which will make up a huge part of your coding experience. For now, it’s important to note that a GameObject is anything in the game itself and may be the camera, the player, or even more complex objects like levels, UI or even utilities we build to help us visualize elements in the game. Next up is the Project tab.
Think of this as a view into the project folder itself. To help you better understand it, go to where you created your project on your system and open it up.
As you can see, in addition to all of the additional files that make up the Unity game project, you will see there is an Assets folder. This becomes your default location for everything you put in your game. Here we will store artwork, sounds, scripts, and prefabs, which are reusable pre-configured GameObjects.
The final two things I want to cover should be self-explanatory but are always useful to review. In the upper-left corner you have a set of controls to help you navigate the Scene window.
Each of these tools help you navigate or interact with GameObjects in the Scene panel. The one you will use the most is the Hand tool, simply drag the screen around on the x- and y-axis as well as the movement tool, to the right of the hand tool, to move GameObjects directly.
The other set of controls handles playback and allows you to test the game.
Simply hit Play to start the testing in the Game tab.
You will also notice the Pause and Step-Forward buttons. These are incredibly helpful in allowing you to move through the game frame by frame to see what is going on. You can also go back to the Scene tab while playing the game and modify values of GameObjects at runtime. It’s important to note that any changes you make in the Inspector panel while running the game don’t get saved; it simply allows you to try things on the fly without having to stop testing, make a change, and recompile.
Before we get into the coding and specific 2D tools, I wanted to talk about GameObjects, which are the building blocks of a Unity game. As you begin to flesh things out, you will start by creating these GameObjects in the Scene. Some of them will represent your Player, bad guys, and level while others will make up utilities and Gizmos, which we will learn about later on, to help you manage other GameObjects in the game.
To get you used to working with GameObjects, let’s just create a simple cube and position it in our Scene. Go to the GameObject menu at the top, or the Create menu from the Hierarchy panel, and select Cube.
Now, if you check your Hierarchy panel, you will see the cube and the camera. Select the cube to bring it up in the Inspector panel.
If you run your game now, you may not see the cube depending on where it’s Z axis is set to when it was added to the scene. You can always click on the Main Camera itself to preview the Scene without even hitting Run.
We can fix this by adjusting the cube’s z-axis in the Inspector window to 0.
Now, if you run the game, you will see the cube.
Notice that we still have the Inspector panel open with the cube. You can continue to play with the cube’s properties while the game runs. If you roll over any number property, you will see the cursor change into a two-sided arrow, and you can use that to click and drag the value in the field up and down. Give it a try with the x and y properties of the cube. Since we are in 2D mode, modifying the z-axis will not do anything eventful. When you stop running the game, all of the values will reset themselves.