Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering Unity 2D game development

You're reading from   Mastering Unity 2D game development Mastering Unity 2D Game Development will give your game development skills a boost and help you begin creating and building an RPG with Unity 2D game framework

Arrow left icon
Product type Paperback
Published in Aug 2014
Publisher
ISBN-13 9781849697347
Length 474 pages
Edition 1st Edition
Tools
Arrow right icon
Author (1):
Arrow left icon
Simon Jackson Simon Jackson
Author Profile Icon Simon Jackson
Simon Jackson
Arrow right icon
View More author details
Toc

Table of Contents (15) Chapters Close

Preface 1. Overview 2. Building a Character FREE CHAPTER 3. Getting Animated 4. The Game World 5. NPCs and Interactions 6. The Big Wild World 7. Encountering Enemies and Running Away 8. Shopping for Weapons 9. Getting Ready to Fight 10. Fight and Gain XP 11. Onward Wary Traveler 12. Deployment and Beyond A. Additional Resources Index

Welcome to 2D

Welcome to 2D, as you might say, Unity v4.3 has brought 2D into its primary toolset a huge range of features to make 2D development a lot simpler and more integrated with its editor environment.

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 new 2D mode

When creating a new Unity project, you now have the option to select whether you want to treat assets as normal 3D assets or use the new 2D import wizards, as shown in the following screenshot:

The new 2D mode

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:

The new 2D mode

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.

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 object to create an animation. Refer to the following screenshot:

Sprites

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.

As you would expect, you can alter most of the characteristics of your sprite, including the following characteristics:

  • Scaling
  • Pivot point
  • 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.

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.

Sprite Renderer

The Sprite Renderer is the new 2D renderer to draw sprites to the screen, much in the same way as other Unity renderers draw currently. Refer to the following screenshot:

Sprite Renderer

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.

Sprite Editor

The new Sprite Editor object is Unity's way of viewing and manipulating the sprite texture assets once they have been imported into Unity.

Sprite Editor

The editor allows some basic manipulations to happen to a sprite. For example:

  • 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

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:

Spritesheets

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.

Texture atlases

Akin to spritesheets, texture atlases are a more efficient way of packing textures into a single texture. Refer to the following screenshot:

Texture atlases

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.

Sprite meshes

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:

Sprite meshes

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.

The Box2D physics system

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 axis; 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:

The Box2D physics system

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).

Unity recognized this limitation while building their 2D system and has pulled in another world class physics system for 2D, called Box2D.

Tip

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.

Tip

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/Unity-v4-3-2d-vs-3d-physics/.

Through Box2D, Unity adds several new physics collision options. They are as follows:

  • Rigidbody 2D
  • CircleCollider 2D
  • BoxCollider 2D
  • PolygonCollider 2D
  • Edgecollider 2D

Unity also adds the following 2D physics joint options to control how two physics bodies bond together:

  • 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 Physicis Material 2D. This allows greater control over an object's physics interactions, such as friction and bounciness.

The new animation Dope Sheet

The new animation Dope Sheet 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:

The new animation Dope Sheet

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.

For extra visibility, there is a Preview window next to the Animator controls. So, you can see a close up of the effects of the changes you make to the animation.

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.

Other Unity 4.3 improvements

Unity 4.3 was not just about the new 2D system; there are also a host of other improvements and features with this release.

The major highlights of Unity 4.3 are covered in the following sections.

Improved Mecanim performance

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:

Improved Mecanim performance

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.

The Windows Phone API improvements and Windows 8.1 support

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 Windows Phone API improvements and Windows 8.1 support

Unity 4.3 builds solidly on the v4 foundations by bringing additional platform support, and it closes some more gaps between the existing platforms. Some of the advantages are as follows:

  • 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 Windows Phone 8.1 and universal projects.

Dynamic Nav Mesh (Pro version only)

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:

Dynamic Nav Mesh (Pro version only)

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 even an even better speed boost to your game. Also, there have been many other under-the-hood fixes and tweaks.

Editor updates

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.

Editor updates

The improved Unity Editor GUI with huge improvements

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.

Additionally, the keyboard controls have been tweaked to ensure that the navigation works in a uniform way and the sliders/fields work more consistently.

MonoDevelop 4.01

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 4.01

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/Main_Page) it uses to the current V3 branch, most likely, due to the reduced platform and the later versions of the Mono support.

Movie textures

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:

Movie textures

With movie textures now added to the platform, other streaming features are also available, for example, webcam (or a built-in camera in this case) and microphone support were also added.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at AU $24.99/month. Cancel anytime