5 Building a Memory game using Unity’s 2D functionality

 

This chapter covers

  • Displaying 2D graphics in Unity
  • Making objects clickable
  • Loading new images programmatically
  • Maintaining and displaying state by using UI text
  • Loading levels and restarting the game

Up to now, we’ve been working with 3D graphics, but you can also work with 2D graphics in Unity. So in this chapter, you’ll build a 2D game. You’re going to develop the classic children’s game Memory: you’ll display a grid of card backs, reveal the card front when it’s clicked, and score matches. These mechanics cover the basics you need to know to develop 2D games in Unity.

Although Unity originated as a tool for 3D games, it’s used often for 2D games as well. Unity has had built-in 2D graphics support since version 4.3 in 2013, but even before then 2D games were already being developed in Unity (especially mobile games that took advantage of Unity’s cross-platform nature). In prior versions of Unity, game developers required a third-party framework to emulate 2D graphics within Unity’s 3D scenes. Eventually, the core editor and game engine were modified to incorporate 2D graphics, and this chapter will teach you about that functionality.

The 2D workflow in Unity is more or less the same as the workflow to develop a 3D game: import art assets, drag them into a scene, and write scripts to attach to the objects. The primary kind of art asset in 2D graphics is called a sprite.

5.1 Setting up everything for 2D graphics

 
 
 

5.1.1 Preparing the project

 
 

5.1.2 Displaying 2D images (aka sprites)

 
 
 
 

5.1.3 Switching the camera to 2D mode

 
 
 
 

5.2 Building a card object and making it react to clicks

 
 
 

5.2.1 Building the object out of sprites

 
 

5.2.2 Mouse input code

 
 
 

5.2.3 Revealing the card on a click

 
 
 
 

5.3 Displaying the various card images

 
 
 

5.3.1 Loading images programmatically

 
 
 
 

5.3.2 Setting the image from an invisible SceneController

 
 
 

5.3.3 Instantiating a grid of cards

 
 

5.3.4 Shuffling the cards

 
 
 
 
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest