Skip to main content

2013 | Buch

Learn Unity for 2D Game Development

insite
SUCHEN

Über dieses Buch

2D games are everywhere, from mobile devices and websites to game consoles and PCs. Timeless and popular, 2D games represent a substantial segment of the games market.

In Learn Unity for 2D Game Development, targeted at both game development newcomers and established developers, experienced game developer Alan Thorn shows you how to use the powerful Unity engine to create fun and imaginative 2D games.

Written in clear and accessible language, Learn Unity for 2D Game Development will show you how to set up a step-by-step 2D workflow in Unity, how to build and import textures, how to configure and work with cameras, how to establish pixel-perfect ratios, and all of this so you can put that infrastructure to work in a real, playable game. Then the final chapters show you how to put what you've already made to work in creating a card-matching game, plus you'll learn how to optimize your game for mobile devices.

Inhaltsverzeichnis

Frontmatter
Chapter 1. Unity Basics for 2D Games
Abstract
This book is about making 2D games using the Unity engine. It will focus not only on the central concepts critical to 2D games, such as 2D space and projections, but also on the practical workflow that can be followed in Unity for creating 2D games. I use the term 2D game (two-dimensional game) to mean any game that principally relies on only 2D coordinates. Famous 2D games include Tetris, Super Mario Brothers, Angry Birds, and many more.
Alan Thorn
Chapter 2. Materials and Textures
Abstract
Materials and textures are the raw materials for 2D games. They are to 2D games what 3D meshes are for 3D games. Practically, all your game graphics, from characters and backgrounds to special effects and in-game menus, will be implemented in the form of materials and textures. If you can see it on-screen, then it’s due to materials and textures. There’s simply no getting around them, so it’s important to have a solid understanding of them—and not just for 2D games either but for games more generally. This chapter aims to provide that understanding. It’ll explain the crucial difference and relationship between materials and textures. It’ll also discuss how you can create both of them, as well as many other relevant considerations to keep in mind when working with Unity. So fire up your PC and let’s get started.
Alan Thorn
Chapter 3. Quick 2D Workflow
Abstract
In this chapter we’ll put together a simple 2D game in Unity using only the standard tools and editor features. This game is a space-invaders style clone featuring a player-controlled spaceship at the bottom of the screen (see Figure 3-1). In this game, the player can move horizontally, left and right, and must shoot invading aliens at the top of the window. The purpose of this exercise is not to show you how to make space-invader style games (though it will do that!) but to show you the more abstract principles and workflows for making “simple” 2D games quickly without having to code any additional features or editor plug-ins. This workflow shows that it’s entirely possible to make 2D games straight away in Unity by using only the 3D-oriented tools. But it also shows the limitations of this approach, as well as its far-reaching performance implications for making games that’ll perform well across all types of devices, especially mobiles. Once we concretely identify these challenges and issues, we’ll proceed to tackle them one by one throughout subsequent chapters. So now, follow along with me as we create our first 2D game.
Alan Thorn
Chapter 4. Customizing the Editor with Editor Classes
Abstract
In the previous chapter we created a 2D space-invaders style game with Unity. Although this game proved functional and “fit for purpose”, we nonetheless foresaw a range of problems or limitations that could arise when creating more complex 2D games- such as a top-down RPG game. These problems arise largely because we’re using a 3D engine and toolset to make 2D games. We were left with the following questions: How can I position game objects in the scene to align on the screen at specific pixel positions? How can I make textures pixel-perfect? How can I make them render as sharply and cleanly as they appear in Photoshop or GIMP? How can I create hundreds of sprites and tiles without increasing the draw count for each unique texture? Throughout this book we’ll answer all these questions. To get started at doing this however, we’ll need to explore Editor Classes. So let’s look at those here and see how they work.
Alan Thorn
Chapter 5. Procedural Geometry and Textured Quads
Abstract
Nearly every 2D game uses sprites and tilesets, in one form or another. Games such as Phantasy Star, Zelda, Final Fantasy, Dizzy, Contra, Donkey Kong, Mario, Sonic, and others, have all used sprites and tiles. Some games use them in a conventional and obvious way while some less so. But the very fact that jargon terms exist for these two concepts is testament to their prevalence in games. The sprite is to 2D games what a mesh is to 3D games; and tilesets find their closest 3D equivalent in the static and modular geometry of the scene.
Alan Thorn
Chapter 6. Generating Atlas Textures
Abstract
This chapter ranks as one of the most important in the book. Here, we’ll create a new ScriptableWizard Editor extension that’ll generate Atlas Textures. What is an Atlas Texture? And what does creating one achieve? To answer the first question, imagine this: you’ve created lots of different sprite images for your 2D game (all at different pixel dimensions)—one file for the player, one for each enemy, and one for each prop and power-up. That’s probably a lot of files! Then, you copy and paste them all into one, larger image file, consolidating them neatly into rows and columns. In doing this, you’ve just created an Atlas Texture the manual way. Now why is an Atlas Texture useful? Essentially, they offer us several benefits when we use them in Unity for our objects, as opposed to using many different textures. These benefits are listed here:
Alan Thorn
Chapter 7. UVs and Animation
Abstract
The previous chapter explained how to develop a GUI editor add-in that allows users to add selected textures in the project panel to a larger atlas texture. Atlas Textures help us improve the performance of our 2D games. In generating the atlas, we also saved meta-data. This included all filenames of textures inside the atlas, and their UV positions within the atlas. In closing that chapter, we tested the add-on functionality by assigning an Atlas Texture to a procedural Quad Mesh in the scene, to see how it looked in the Viewport. The problem we faced was that the quad mesh rendered the complete atlas texture, rather than just a region of it (see Figure 7-1). The problem was not with the Atlas Texture, but with the mesh itself; with its UV mapping. By default, the UV mapping for Quad Meshes is configured to show a complete texture. To show only a region, the UV mapping must be adjusted at the vertex level. That is, we must dig deeper into the mesh construction and edit its vertices. We’ll do that in this chapter, as we create a new editor add-in to control mesh UV mapping. This add-on lets us select a textured quad in the scene and entirely customize its UV mapping to show any region within the Atlas Texture (or within any texture!). In addition, we’ll also create another class to change UV mapping over time, creating a flipbook animation effect or an animated sprite effect.
Alan Thorn
Chapter 8. Cameras and Pixel Perfection
Abstract
Now you've seen how to create the infrastructure for a truly 2D game in Unity, at least in terms of textures and geometry. In the past few chapters we've created editor plug-ins to generate quads and atlas textures. The quads are formed from four corner vertices and two polygons. By combining the quads with atlas textures, and UV mapping, we can display specific regions of an atlas on the quad surface. Further, we can animate the UV mapping to show flip-book animations, like traditional sketchbook animations where its pages are flipped through one after the other. But there's a crucial ingredient missing in our 2D formula. Sure, we can create quads and atlas textures, but all the quads we create are shown using a standard Unity camera in a perspective view. This means all our 2D elements are subject to the standard “laws of perspective.” See Figure 8-1. The implications of this are considered more fully in the list that follows.
Alan Thorn
Chapter 9. Input for 2D Games
Abstract
In this chapter we'll talk about user input for 2D games, specifically input from the keyboard, mouse, and mobile devices. Input is critical for any game, whether 2D or not. Without input, a game cannot be interactive. For most 2D games, we need to read input that maps directly to 2D space, in terms of pixel positions. However, because Unity is a 3D engine and our objects exist in three-dimensional space, we'll also need to convert screen space positions to world space positions. For this chapter, I'll be working from a premade, basic 2D project, which is included in the book companion files (in Project_Files/Chapter09/); however, you can work along with your own projects instead, if you want to. Some points covered in this chapter include:
Alan Thorn
Chapter 10. Getting Started with a 2D Game
Abstract
By this stage you should have a firm grasp on the most fundamental issues underpinning 2D games in Unity, including procedural geometry, Atlas Textures, UV mapping, orthographic cameras, and 2D input. The preceding chapters focused on each of these issues in isolation, one at a time, and examined their implementation details and specifics. This chapter, and the one that follows, puts these ideas into practice in the form of a 2D game project. Specifically, we'll make a simple 2D card matching game—the kind that tests your memory on position and placement, as we'll see. Take a look at Figure 10-1 to see the project in action. In creating this, we'll put to use all the skills and ideas we've seen so far. Additionally, at the end of this chapter, our project will take an interesting and arguably more complicated twist—one which you may or may not expect. I'll leave that to be a surprise! So let's get started now with our card matching game.
Alan Thorn
Chapter 11. Completing the 2D Card Game
Abstract
This chapter resumes from where the previous ended. It assumes you've been working through the card game project and that you now have a working version. This version is currently a single player game where the player must find three card matches within a grid of cards. If you don't have a project to work with, you can start from the project files I've created, found in the book companion files at Project_Files/Chapter11/Project. Figure 11-1 shows the project created so far. In this chapter we'll transform that single-player project into a networked multi-player version. In short, we'll adapt the project so two players across either a local network or the Internet can play the same game together, with each player participating in turns and being able to see the actions of the other player on their turn. To achieve this, we'll make use of the Unity API and its range of networking classes. Specifically, we'll examine a trio of classes: the Network class, the MasterServer class and the NetworkView class.
Alan Thorn
Chapter 12. Optimization
Abstract
This chapter looks at optimization; specifically performance optimization. That is, we’ll explore practical techniques you can use when making games in Unity to help them run well (or better) across different devices and hardware, including mobile devices. These techniques are presented in a list throughout this chapter, with sub-headings to organize them. They are presented in no particular order (least of all in order of importance), and some of them are related and some not. But regardless, all of them are typically important considerations for optimization—and they're likely to be things you'll consider at some point during development, regardless of whether they ultimately turn out to be the right solutions or approaches for you and your circumstances. Before getting started, however, it's worthwhile for me to say a little about optimization generally, as it applies to Unity games and also beyond. So let's start there.
Alan Thorn
Chapter 13. Wrapping Things Up
Abstract
Congratulations on reaching this chapter—you should now be in a strong position to create 2D games in Unity (even if you're using the free version). The techniques I've demonstrated here work both with Unity Free and Unity Pro. Of course, what's been presented in this book, however, should not be seen as the end of the line for 2D game development. It should be seen less as an ending point and more as a starting point. No book, no matter how large, could say everything there ever was to know about 2D game development. The subject of 2D games is simply too large to encompass and discuss in its entirety. For this reason, there's inevitably a lot this book leaves unsaid or untouched. The purpose of this book has not been to cover all aspects of 2D development, but rather to examine a foundation or core that underpins all 2D games. By learning and understanding this core, and a general 2D work-flow, you're now in a position of greater power and independence. This is because, whatever kind of 2D game you want to make and whatever kind of challenges it throws at you, you now know the fundamentals of how to draw, process and handle 2D graphics with Unity in a way that's optimal.
Alan Thorn
Backmatter
Metadaten
Titel
Learn Unity for 2D Game Development
verfasst von
Alan Thorn
Copyright-Jahr
2013
Verlag
Apress
Electronic ISBN
978-1-4302-6230-5
Print ISBN
978-1-4302-6229-9
DOI
https://doi.org/10.1007/978-1-4302-6230-5