Chapter 5. 2D Graphics

Unity is best known as a 3D game engine, but it’s just as capable for working with 2D graphics. In this chapter, we’ll discuss how to put together a game designed for a flat perspective.

Note

None of the concepts covered in this chapter are restricted to 2D games!

Recipes in this chapter revolve around concepts related to 2D graphics, such as sprites, and collisions between sprites, particle effects, and sprite sorting.

5.1 Importing Sprites

Solution

  1. Drag and drop the images into your project.

  2. Select the images and set the Texture Type to “Sprite (2D and GUI)” (Figure 5-1).

ugdc 0501
Figure 5-1. Setting the texture type for a sprite
  1. Click Apply.

  2. You can now use this image as a sprite.

Discussion

If your project was created with the 2D template, incoming sprites will automatically be set up this way. Otherwise, images are imported as textures, for use in materials for 3D models.

You can change your project to behave this way by going Edit → Project Settings → Editor and setting Default Behavior Mode to 2D (Figure 5-2). This will also make any new scenes you create have a different set of initial objects—they won’t have a light or skybox.

ugdc 0502
Figure 5-2. Setting the default mode for a project to 2D. This, among other things, makes all new textures default to sprites.

5.5 Customizing Sprite Collision Shapes

Solution

  1. Select the sprite you want to configure, and ensure that Generate Physics Shape is turned on (Figure 5-3).

ugdc 0503
Figure 5-3. Enabling the Generate Physics Shape option in the import settings for a sprite texture
  1. Click the Sprite Editor button. The Sprite Editor window will appear.

  2. In the drop-down list at the top-left corner, select Custom Physics Shape.

You can now begin defining the shape of the sprite (Figure 5-4).

ugdc 0504
Figure 5-4. Creating the shape for a sprite
  1. Click and drag to add a new path to the shape.

  2. Click the points of a path to move them.

  3. Click and drag between two points to insert a new point between them. (You can also hover over the line, and click to add new points!)

  4. Click a point and press Command-Delete (Control-Delete on a PC) to delete it.

  5. When you’re done, click Apply.

Discussion

The physics outline is used as the default shape when you add a polygon collider to an object that has a sprite renderer (Figure 5-5). If a sprite doesn’t have a physics outline set up in it, Unity generates one for you using the transparent regions of the sprite. Unity will generally try to create a very close outline, which means that the default shape is often more complex than it needs to be. When you customize the physical outline of a sprite, you can provide a simpler shape for the physics system to use.

ugdc 0505
Figure 5-5. The physics shape in use

5.6 Using a Composite Collider

5.8 Applying Forces to 2D Objects

5.9 Creating a Conveyor Belt

5.10 Using a Custom Material for Sprites

Solution

  1. Create a new material asset by opening the Assets menu and choosing Create → Material. Name the new material “Sprite Diffuse.”

  2. Select the new sprite, and at the top of the Inspector, change the shader to Sprites → Diffuse (Figure 5-11).

ugdc 0511
Figure 5-11. Selecting the Sprite → Diffuse shader on a material
  1. Select any game object in your scene that has a sprite renderer, and drag your Sprite Diffuse material into its Material slot. It will now respond to lighting (Figure 5-12).

ugdc 0512
Figure 5-12. A sprite using the Diffuse shader. It’s now shaded and responds to light.

Discussion

The default shader ignores lights—the color of the texture is what’s used onscreen (as opposed to being shaded by the environment).

Note

If you plan on lighting your sprites, you should design your sprites to be very bright. Unless the sprite is hit straight-on by bright light, it will be darker than it appears in the texture.

5.11 Managing Sprite Sorting

Discussion

The Order in Layer setting controls the drawing order of a sprite within its sorting layer. And there’s always at least one sorting layer, called Default, which can’t be removed.

To create sorting layers open the Edit menu, and choose Project Settings → Tags & Layers. Create your sorting layer, and rearrange their order (Figure 5-13).

ugdc 0513
Figure 5-13. Creating and configuring sorting layers

Use Sorting layers to ensure that large sprite collections are drawn above or below other collections: for example, you might create one sorting layer called Background and another called Characters; objects on the Background sorting layer are always drawn behind objects on the Characters layer, regardless of their Order in Layer property.

You can select a sorting layer for a sprite renderer by changing its Sorting Layer property in the Inspector (Figure 5-14).

ugdc 0514
Figure 5-14. Selecting a sorting layer for a sprite renderer

Sorting layers have a similar name to Layers, but they’re a different thing. Two objects can be on different sorting layers while being on the same layer.

5.12 Using Sorting Groups

Discussion

Sorting groups ensure that all members of that group are drawn together, and that no other objects can be drawn between them.

For example, Figure 5-15 and Figure 5-16 both show two characters, each of which is composed of a number of other sprites. These sprites have their sort order configured so that, for example, the head is drawn on top of the body, and one of the arms is drawn behind the body. In Figure 5-15, the two characters have a sorting group on their parent object, while in Figure 5-16, they don’t. This results in the various body parts incorrectly overlapping the other character’s.

ugdc 0515
Figure 5-15. Two groups of sprites, each with a sorting group attached to its parent. The two characters are sorted separately from each other.
ugdc 0516
Figure 5-16. The same two groups of sprites, with the sorting group disabled. Because the sprites are on the same layer and have the same sorting orders, parts of the two characters overlap each other.

5.13 Creating a 2.5D Scene