Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Mastering Unity 2D game development
Mastering Unity 2D game development

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

By Simon Jackson
AU$67.99 AU$46.99
Book Aug 2014 474 pages 1st Edition
eBook
AU$67.99 AU$46.99
Print
AU$83.99
Subscription
$19.99 Monthly
eBook
AU$67.99 AU$46.99
Print
AU$83.99
Subscription
$19.99 Monthly

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Buy Now

Product Details


Publication date : Aug 26, 2014
Length 474 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781849697347
Vendor :
Unity Technologies
Category :
Table of content icon View table of contents Preview book icon Preview Book

Mastering Unity 2D game development

Chapter 1. Overview

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

  • Battles

  • 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

Getting assets


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.

Note

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

Note

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.

Collection of sites

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:

http://darkgenesis.zenithmoon.com/monster-set-of-free-resources-for-game-design/

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

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.

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

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:

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.

The sprite editor window showing a single sprite

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:

Sprite editor window showing multiple sprites in a grid

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:

A selection of separate textures that have been automatically packed, example from Unity's platformer sample

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:

Image sprites that have been converted into 2D meshes in the scene view

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

Example of Box2D 2D colliders used in the Unity platformer example to surround walkable elements

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/unity3d-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 Physics 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(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:

The animation editor Dope Sheet window showing a single animation over time

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:

The Mecanim Animator window showing an example animation tree

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:

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 for 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:

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.

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.

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

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:

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.

Summary


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.

Left arrow icon Right arrow icon

Key benefits

What you will learn

Explore the new features introduced in Unity3D 4.3 by building your own RPG game Delve into the new sprite importing and rendering system Master the best parts of the new Sprite animation system and improved Mecanim features Make the most of your rendering with tags, layers, sprite sorting, and ordering Dissect the proper use of Delegates, Events, and Messaging, including tips and tricks, and build a full RPG conversation system Do battle with Mecanim-based AI and state machines Build for success with tips on marketing, monetization, and deployment Drive home the platform of choice and expand into deployment/editor extensions

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Buy Now

Product Details


Publication date : Aug 26, 2014
Length 474 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781849697347
Vendor :
Unity Technologies
Category :

Table of Contents

21 Chapters
Mastering Unity 2D Game Development Chevron down icon Chevron up icon
Credits Chevron down icon Chevron up icon
About the Author Chevron down icon Chevron up icon
Acknowledgments Chevron down icon Chevron up icon
About the Reviewers Chevron down icon Chevron up icon
www.PacktPub.com Chevron down icon Chevron up icon
Preface Chevron down icon Chevron up icon
1. Overview Chevron down icon Chevron up icon
2. Character Building Chevron down icon Chevron up icon
3. Getting Animated Chevron down icon Chevron up icon
4. The Game World Chevron down icon Chevron up icon
5. NPCs and Interactions Chevron down icon Chevron up icon
6. The Big Wild World Chevron down icon Chevron up icon
7. Encountering Enemies and Running Away Chevron down icon Chevron up icon
8. Shopping for Weapons Chevron down icon Chevron up icon
9. Getting Ready to Fight Chevron down icon Chevron up icon
10. The Battle Begins Chevron down icon Chevron up icon
11. Onward Wary Traveler Chevron down icon Chevron up icon
12. Deployment and Beyond Chevron down icon Chevron up icon
Additional Resources Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Filter icon Filter
Top Reviews
Rating distribution
Empty star icon Empty star icon Empty star icon Empty star icon Empty star icon 0
(0 Ratings)
5 star 0%
4 star 0%
3 star 0%
2 star 0%
1 star 0%

Filter reviews by


No reviews found
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.