To create a 2D game, set up your Unity project and then familiarize yourself with the relevant concepts in the following order:
GameObjects are fundamental objects in Unity that represent characters, props, scenery, and more. Every object in your game is a GameObject.
GameObjects represent the items in your game; the space in which you place them to build your level is called a scene. Scenes in Unity are always 3D; when you make a 2D game in Unity, you typically choose to ignore the third dimension (the z-axis) but you can also use it in special cases, for example when making 2.5D games.
The behavior of GameObjects is defined by blocks of functionality called components. The following components are fundamental for 2D games:
Transform: the Transform component determines the Position, Rotation, and Scale of each GameObject in the scene. Every GameObject has a Transform component.
Sprite Renderer: the Sprite Renderer component renders the Sprite and controls how it looks in a scene.
Cameras: devices that capture and display the world to the player. Marking a Camera as Orthographic removes all perspective from the Camera’s view. This is mostly useful for making isometric or 2D games.
Components are UI representations of C# classes; you can use scripts to change and interact with components, or create new ones. See the Scripting section for more details.
All 2D games need scripts. Scripts respond to input from the player and arrange for events in the gameplay to happen when they should.
Scripts are attached to GameObjects, and any script you create inherits from the MonoBehaviour class.
Sprites are 2D graphic objects. You use Sprites for all types of 2D games. For example, you can import an image of your main character as a Sprite.
You can also use a collection of Sprites to build a character. This allows you greater control over the movement and animation of your characters.
Import your Sprites with Unity’s recommended settings; see Importing and Setting Up Sprites.
Use the Sprite Renderer component to render your Sprites. For example, you can use the Sprite Renderer to change the color and opacity of a Sprite.
See the Introduction to the Sprite Renderer Learn tutorial. Sorting SpritesBy organizing Sprites in layers, you can create an illusion of depth. You can sort Sprites according to many strategies. See Sorting Sprites for full details. For example, you might sort Sprites along the y-axis, so that Sprites that are higher up are sorted behind Sprites that are lower, to make the Sprites that are higher appear further away than the Sprites that are lower.
To set the overlay order of Sprites, use Sorting Layers.
You can use a Sprite Atlas to consolidate several Textures into a single combined Texture. This optimizes your game and saves memory. For example, you can add all your Sprites associated with a particular character or purpose to a Sprite Atlas.
World-building refers to the process of building your game’s levels and environments. You can combine the world-building tools in this section in whichever way makes the most sense for your game; for example, you can make a top-down game using only 9-slice, or you can make a side-on platformer with Tilemap and SpriteShape.
9-slicing is a 2D technique that allows you to reuse an image at various sizes without needing to prepare multiple assets. Unity can dynamically stretch and tile designated parts of a Sprite to allow one Sprite to serve as the border or background for UI elements of many sizes. See 9-slicing sprites.
For example, you could use 9-slicing to stretch a Sprite to shape when you build a 2D level.
For example, you can use Tilemaps to paint levels using Tiles and brush tools and define rules for how Tiles behave.
To add some extra Tilemap assets to your Project, install the 2D Tilemap Extras package. This package contains reusable 2D and Tilemap Editor scripts that you can use for your own Projects. You can customize the behavior of the scripts to create new Brushes that suit different scenarios.
For games with isometric perspective, you can create Isometric Tilemaps.
In a similar way to a vector drawing tool, SpriteShape provides a more flexible way to create larger Sprites, such as organic-looking landscapes and paths. See the Sprite Shape Profile.
See the Working with SpriteShape tutorial.
There are three different ways you can animate 2D characters:
|2D animation type||Used for|
|Frame-by-frame||Artistic reasons, if you want your game to have a classic animation art style. Frame-by-frame animation is relatively resource-intensive, both to make and to run.|
|Cutout||Smooth skeletal animation, when the characters don’t require realistic articulation.|
|Skeletal||Smooth skeletal animation where Sprites bend according to the bone structure. Use this when the characters need a more organic feel.|
Frame-by-frame animation is based on the traditional cel animation technique of drawing each moment of an animation as individual images, which are played in fast sequence, like flipping pages on a flipbook.
To do frame-by-frame animation, follow the Frame-by-frame Animation workflow.
In cutout animation, multiple Sprites make up the body of a character, and each piece moves to give the visual effect of the whole character moving. This animation style is similar to skeletal animation (see below), except that the Sprites don’t bend.
With skeletal animation, you map a Sprite or a group of Sprites onto an animation skeleton. You can create and define animation bones for characters and objects, that define how they should bend and move. This approach allows the bones to bend and deform the Sprites, for a more natural movement style. To use skeletal animation, you need to use the 2D Animation package (installed by default).
For a 2D Animation workflow, including a guide to working with the Bone Editor, see the 2D Animation documentation.
This section describes your graphics options when using Universal Render Pipeline (URP).
Because you’re using URP with the 2D Renderer, you can use the Light 2D component to apply optimized 2D lighting to Sprites. For details, see Introduction to Lights 2D.
To set up lighting:
Prepare your Sprites for lighting. For details, see Preparing Sprites for Lighting.
Set up normal map and mask Textures. 2D Lights can interact with normal map and mask Textures linked to Sprites to create advanced lighting effects, such as normal mapping. See Setting up normal map and mask Textures.
Create a 2D Light GameObject; see 2D Lights Properties.
Configure the 2D Renderer Data asset; see Configuring the 2D Renderer Asset.
(Optional) if you want to apply 2D Light effects to a pixel art game, see 2D Pixel Perfect.
See the Lighting in URP Learn tutorial.
To define the shape and properties that a Light uses to determine the shadows it casts, use the Shadow Caster 2D component. Increase the Light’s Shadow Intensity above zero.
Particle systems and post-processing are optional tools that you can use to add polish to your game.
You can use particle systems to create dynamic objects like fire, smoke or liquids, as an alternative to using a Sprite. Sprites are more suited to physical objects. See Particle systems.
You can use post-processing effects and full-screen effects to significantly improve the appearance of your game. For example, you can use these effects to simulate physical camera or film properties, or to create stylized visuals.
URP has its own post-processing implementation. See Post-processing in the Universal Render Pipeline.
The Physics 2D settings define limits on the accuracy of the physical simulation in your 2D game. See 2D Physics.
This video provides an overview of 2D physics features in Unity 2020.1.
To learn how to use Unity’s 2D physics engine, see the 2D Physics Learn tutorial.
The following 2D physics tools are useful for 2D games.
A Rigidbody 2D component places a GameObject under the control of the physics engine. See Rigidbody 2D.
Collider 2D components define the shape of a 2D GameObject for the purposes of physical collisions. You can also use Collider 2D components for input detection. For example, in mobile games you can use them to make Sprites selectable.
The Collider 2D types that you can use with Rigidbody 2D are:
When you set a Collider 2D as a Trigger (by enabling its Is Trigger property), it no longer behaves as a physical object, and it can intersect with other Colliders without causing a collision. Instead, when a Collider enters its space, Unity calls the
OnTriggerEnter function on the Trigger GameObject’s scripts.
Joints attach GameObjects together. You can only attach 2D joints to GameObjects that have a Rigidbody 2D component attached, or to a fixed position in world space. See 2D Joints.
You can add background music and sound effects to your game in Unity; see Audio Overview. Use third-party software to create your audio and import it into Unity with the recommended settings.
Profiling allows you to see how resource-intensive the different parts of your game are. You should always profile your game on its target release platform; see Profiling your application.
After profiling, you can use the results to make performance improvements and optimizations. See Understanding optimization in Unity.
Test your game and your code with the Unity Test Framework; see Unity Test Framework.
When you’ve finished your game, you’re ready to publish it. See Publishing Builds.