Arguably, the most important part of any project is knowing where to start and what tools you have in your war chest before setting out to make your game. Here, we will walk through all the new features of the 2D system that were introduced in Version 4.3 of Unity.
Since this is the first chapter, let's cover how this book is structured. The main aim of this book is to build a fully functional Role Playing Game (RPG) style game framework and cover all the main aspects of any good and well-rounded RPG game, including the following features:
Character development and setup
Building your main game view
A wider world view
Events and encounters
Shopping and inventory systems
Skills, experience, and leveling
We will be visiting places such as the following:
Your home town, as shown in the following screenshot:
The local shop, as shown in the following screenshot:
The outside world, as shown in the following screenshot:
Battling goblins in the dark forest, as shown in the following screenshot:
In this chapter, we'll walk through what's available in the new 2D toolkit and also what else Unity added to the toolset in 4.3. Then, kick off the next chapter with building the foundations of our project with some of the best practices in the industry, including guidance from the Unity team themselves (either direct from team members or from responses in the forums).
The following topics will be covered in this chapter:
Overview of the new 2D system
Rundown of the additional improvements in 4.3
Since creating games can become quite expensive these days, we'll use some of the best free assets out there. There are plenty of resources available to the game developers these days either as placement assets for the developer's use, whether they are full assets, or just a framework that you can tweak to get your desired result. There are a multitude of options.
In the code bundle of this book, you'll get all the assets that you need to follow on during the creation of the game, and the site where it is available online will be listed with the instructions.
Some of the best sites to gather assets are described as follows:
Art: Art, especially 2D art, is generally easy to find on a budget; particularly for the placeholder art until you buy or create your own for the finished product (although I've seen many games created with some of these assets). Some good sites to start with are http://opengameart.org/ and http://open.commonly.cc/.
Audio: Right sound is a lot trickier to get. Free sites are okay, but they generally don't have the right sound you will want or you will end up digging through hundreds or more sounds to get a close match. A good website to start is at http://soundbible.com/.
We won't actually be covering audio as part of this title, as not much has changed since the introduction of Unity 4.0. There are also some larger changes being introduced in Unity 5.0 to look out for, so keep your eyes peeled.
Some sites just hold a general collection of assets instead of specializing in specific areas. The best site for this, as everything is almost guaranteed to be free, is http://search.creativecommons.org/.
For an even wider list of resources, refer to the following blog post that is updated frequently with what's out there:
What follows is a brief rundown of the entire primary enhancement and a walkthrough of some of the other important improvements brought in with v4.3.
The main difference is the way assets will be imported into your solution and the default camera is set up. You can easily change the profile that the editor is using at any time by navigating to Edit | Project Settings | Editor and changing the Default Behavior Mode option. This is shown in the following screenshot:
Changing the Mode setting will not affect the running of your game. This setting is only used while importing new assets and adding them to your scene as to whether they are imported as textures or sprites.
In 2D, sprites are simple images that generally depict a single object (for example, a character) or scene (for example, background). Several sprites can also be made for a single object in the individual frames for that, as shown here:
At the core of the new 2D system is the new sprite texture importer, which imports your texture assets and prepares them as sprites in your project folder. When dragged on to your scene, they are automatically given a new Sprite Renderer object (refer to the next section) that is ready to be displayed in your game; no additional lighting or work is required.
Sprite region on texture
All of the previous characteristics can be modified straight from the editor or at runtime through code or by using the new animation dope sheet (Unity's new animation editor control).
By default, each texture is imported as a single sprite; however, by using the Sprite Editor (refer to the Sprite Editor section), you can change this in various ways. These are covered in the next sections.
The main difference with the Sprite Renderer object is that you do not need to set up a separate material manually (it is assigned automatically) and the default shader does not require any additional lighting (single vertex lighting is used). You can enhance beyond this by adding your own materials and lighting if you wish; there are certainly no limits here.
The Sprite Renderer, like the other renderers, also supports dynamic batching with uniform scaling as well.
Changing the Alpha color
Altering the sprite's pivot position
Splicing the texture to identify the sprite region (this is also used for spritesheets; refer to the next section)
Spritesheets are a core part of any 2D system, especially with 2D animation. Unifying all textures into a single larger texture means greater performance when sending the sprites to the graphic cards in a single texture, which is a lot faster than sending lots of smaller files. Refer to the following screenshot:
The traditional way of forming spritesheets is to put sprites into specific regions on a single image, and then identify the box regions where the individual sprites lie. These regions form individual frames in the sprite animation. As you can see in the preceding screenshot, you have six sprites arranged horizontally in two rows to form a single splash animation. The sprites could have also been arranged in a single row or a single column; it doesn't matter. It's just how the artist best packs the spritesheet for the animation. Unity can handle just about any arrangement you wish to throw at it. Just set the width and height of each texture region and the Unity Sprite Editor will do the rest.
Unity has added a very clever texture cutting and edge detection to make this work very well and identify specific regions on the texture for each sprite. You can also change the selection areas if Unity is too optimistic when selecting the texture regions.
If you have the Pro edition of Unity, the editor can also generate these texture atlases for you from many other sprites very efficiently. This saves hours in asset generation.
In Unity Pro, the engine also has the capability to turn your 2D texture asset into a 2D static mesh, which allows you to have greater control over the display and modification of the original texture at runtime. Refer to the following screenshot:
You have several options with the mesh feature, and you can decide whether you want a mesh for the entire texture or a single tight mesh for the sprite in parts only (effectively, creating a flat 2D mesh just for your sprite).
This also would enable the ability to rig your sprites and alter their shape during the course of the game.
For those who have tried (or have created) physics systems in a 3D world of a 2D game, it's always been troublesome to just restrict one of the physics axes; it basically just doesn't work very well and is not as performant as it could be due to all calculations still being in 3D. Refer to the following screenshot:
More often than not, developers create their own much more simple physics systems (which usually accounts for a high percentage of bugs in games if done wrong).
Games such as Angry Birds and Cut the Rope make heavy use of the 2D physics systems to handle a lot of on-screen animation for free using physics.
Box2D has already been used and ported for many other platforms (including XNA), so it makes complete sense for Unity; in most 2D games, you'll see a 4 times increase in the physics performance. Care should be taken, as with every physics system; don't expect it to solve all your problems. Code well and make sure that the standing objects generate interactions.
If you want to see a great comparison between the 2D and 3D physics systems, refer to this excellent post at http://x-team.com/2013/11/unity3d-v4-3-2d-vs-3d-physics/.
Distance Joint 2D
Hinge Joint 2D
Slider Joint 2D
Spring Joint 2D
Similar to the 3D system, you can also apply the physics materials to your 2D objects using Physics Material 2D. This allows greater control over an object's physics interactions, such as friction and bounciness.
The new animation Dope Sheet(animation editor) is effectively a second version of the original animation Dope Sheet. This area has received a complete overhaul and has now been tightly integrated with the Mecanim system. Refer to the following screenshot:
When the recording mode is enabled (it is a small record button at the upper-left of the Dope Sheet tab), simply changing any value in the editor while in animator view will create new properties in the animation. These animations can then be enhanced with curves for tweening/lerping support and can have the length of the animation controlled much more easily.
You can also manually edit the Dope Sheet tab without the recording mode enabled by adding the curves (the Animation properties) and selecting the property on the attached object that you wish to animate.
In the 2D system, animations automatically generated from the spritesheets lean very heavily on this feature, making it very powerful for 2D.
We will cover the animation Dope Sheet system in Chapter 3, Getting Animated, so stay tuned.
The major highlights of Unity 4.3 are covered in the following sections.
Mecanim is a powerful tool for both 2D and 3D animations. In Unity 4.3, there have been many improvements and enhancements, including a new game object optimizer that ensures objects are more tightly bound to their skeletal systems and removes unnecessary transform holders. Thus making Mecanim animations lighter and smoother. Refer to the following screenshot:
In Unity 4.3, Mecanim also adds greater control to blend animations together, allowing the addition of curves to have smooth transitions, and now it also includes events that can be hooked into at every step.
Unity 4.2 introduced Windows Phone and Windows 8 support, since then things have been going wild, especially since Microsoft has thrown its support behind the movement and offered free licensing for the existing Pro owners. Refer to the following screenshot:
The emulator is now fully supported with Windows Phone (new x86 phone build)
It has more orientation support, which allows even the splash screens to rotate properly and enabling pixel perfect display
It has trial application APIs for both Phone and Windows 8
It has improved sensors and location support
On top of this, with the recent release of Windows 8.1, Unity 4.3 now also supports Windows 8.1 fully; additionally, Unity 4.5.3 will introduce support for Windows Phone 8.1 and universal projects.
If you have only been using the free version of Unity till now, you will not be aware of what a Nav Mesh agent is. Nav Meshes are invisible meshes that are created for your 3D environment at the build time to simplify path finding and navigation for movable entities. Refer to the following screenshot:
You can, of course, create the simplified models for your environment and use them in your scenes; however, every time you change your scene, you need to update your navigation model. Nav Meshes simply remove this overhead. Nav Meshes are crucial, especially in larger environments where collision and navigation calculations can make the difference between your game running well or not.
Unity 4.3 has improved this by allowing more runtime changes to the dynamic Nav Mesh, allowing you to destroy parts of your scene that alter the walkable parts of your terrain. Nav Mesh calculations are also now multithreaded to give an even better speed boost to your game. Also, there have been many other under-the-hood fixes and tweaks.
The Unity editor received a host of updates in Unity 4.3 to improve the performance and usability of the editor, as you can see in the following demo screenshot. Granted most of the improvements are behind the scenes.
The editor refactored a lot of the scripting features on the platform, primarily to reduce the code complexity required for a lot of scripting components, such as unifying parts of the API into single components. For example, the LookLikeControls and LookLikeInspector options have been unified into a single
LookLike function, which allows easier creation of the editor GUI components. Further simplification of the programmable editor interface is an ongoing task and a lot of headway is being made in each release.
Besides the editor features, one of the biggest enhancements has to be the upgrade of the MonoDevelop editor (http://monodevelop.com/), which Unity supports and is shipped with. This has been a long running complaint for most developers simply due to the brand new features in the later editions. Refer to the following screenshot:
MonoDevelop isn't made by Unity; it's an open source initiative run by Xamarin hosted on GitHub (https://github.com/mono/monodevelop) for all the willing developers to contribute and submit fixes to. Although the current stable release is 4.2.1, Unity is not fully up to date. Hopefully, this recent upgrade will mean that Unity can keep more in line with the future versions of this free tool.
Sadly, this doesn't mean that Unity has yet been upgraded from the modified V2 version of the Mono compiler (http://www.mono-project.com/) it uses to the current V3 branch, most likely, due to the reduced platform and the later versions of the Mono support.
Movie textures is not exactly a new feature in Unity as it has been available for some time for platforms such as Android and iOS. However, in Unity 4.3, it was made available for both the new Windows 8 and Windows Phone platforms. This adds even more functionality to these platforms that were missing in the initial Unity 4.2 release where this feature was introduced. Refer to the following screenshot:
So, now you can see what all the excitement is about with the new Unity 4.3 release. Unity has gone a long way to address the gaps in their product and added some new bells and whistles to boot. You should also watch the keynotes at http://unity3d.com/unite/archive, especially to see what else is coming up (such as the new GUI system coming in Version 4.6).
We covered the objective of the book, the paths to get the assets needed for the sample project, a detailed analysis of the new 2D features added in Unity 4.3, and a rundown of the additional features in 4.3 for reference.
Are you seated comfortably? Well keep your arms and legs in the ride at all times and prepare yourself for a high-speed ride.