27,59 €
This book is written for every game developer ready to tackle the bigger picture and start working with advanced programming techniques and design patterns in Unity.
Game Development Patterns with Unity 2021 is an introduction to the core principles of reusable software patterns and how to employ them to build components efficiently.
In this second edition, you'll tackle design patterns with the help of a practical example; a playable racing game prototype where you’ll get to apply all your newfound knowledge. Notable updates also include a game design document (GDD), a Unity programming primer, and the downloadable source code of a complete prototype.
Your journey will start by learning about overall design of the core game mechanics and systems. You’ll discover tried-and-tested software patterns to code essential components of a game in a structured manner, and start using classic design patterns to utilize Unity's unique API features.
As you progress, you'll also identify the negative impacts of bad architectural decisions and understand how to overcome them with simple but effective practices.
By the end of this Unity book, the way you develop Unity games will change – you’ll adapt a more structured, scalable, and optimized process that will help you take the next step in your career.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 249
Veröffentlichungsjahr: 2021
Copyright © 2021 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
Group Product Manager: Ashwin NairPublishing Product Manager: Pavan RamchandaniSenior Editor:Keagan CarneiroContent Development Editor:Divya VijayanTechnical Editor: Saurabh KadaveCopy Editor: Safis EditingProject Coordinator:Manthan PatelProofreader: Safis EditingIndexer: Subalakshmi GovindhanProduction Designer:Aparna Bhagat
First published: March 2019 Second edition: July 2021
Production reference: 1290721
Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK.
ISBN 978-1-80020-081-4
www.packt.com
Contributors
David Baron is a game developer with over 15 years of experience in the industry. He has worked for some well-known AAA, mobile, and indie game studios in Montreal, Canada. His skill set includes programming, design, and 3D art. As a programmer, he has worked on various games for various platforms, including virtual reality, mobile, and consoles.
Lucas Bertolini has 10+ years' experience as a video game software developer. He has worked on three major projects: for Pollux Ltd. (Hong Kong) as a game developer and designer; for Schell Games (Pittsburgh, US) where he moved and worked as a developer until the project was completed; and for Globant as a developer.
He has worked in technical education for 5+ years and has taught a variety of programming courses. He is the cofounder of NGA and Bytenarchy Studios, both digital services development companies that use Unity as their main technology.
Lucas has written Hands-On Game Development without Coding, available from Packt.
Mark Bonasoro grew up in the late 80s in a family with computers from a young age. In the mid-90s, Mark started building his own PCs. His interest in game development started when he used Softimage on a Silicon Graphics machine in high school. Mark's programming skills ramped up in the 2000s when he started college, using C++ to make business applications, and worked in a small indie game company using a 3D multiplatform game engine, still in circulation, called Intrinsic Alchemy. In 2006, Mark started a game studio, as an excuse to learn with his classmates to make mobile games for Java 2 Micro Edition-featured phones for Sony Ericsson and Nokia. He graduated in software engineering in 2010 from Concordia University and worked as a Unity developer for Affordance Studios producing educational games.
Title Page
Copyright and Credits
Game Development Patterns with Unity 2021 Second Edition
Dedication
Contributors
About the author
About the reviewers
Preface
Who this book is for
What this book covers
To get the most out of this book
Download the example code files
Code in Action
Download the color images
Conventions used
Get in touch
Share Your Thoughts
Sections 1: Fundamentals
Before We Begin
Notes about the new edition
The philosophy of the book
What are design patterns?
Which subjects aren't covered in this book?
The game project
Summary
The Game Design Document
The design document
Game overview
Unique selling points
Minimum requirements
Game synopsis
Game objectives
Game rules
Game loop
Game environment
Camera, control, character (3Cs)
Camera
Character
Character description
Character metrics
Character states
Controller
Game ingredients
Superbikes
Pickups
Obstacles
Weaponry
Game systems
Game menu
Game HUD
Summary
Further reading
A Short Primer to Programming in Unity
What you should already know
C# language features
Unity engine features
Summary
Further reading
Section 2: Core Patterns
Implementing a Game Manager with the Singleton
Technical requirements
Understanding the Singleton pattern
Benefits and drawbacks
Designing a Game Manager
Implementing the Game Manager
Testing the Game Manager
Summary
Managing Character States with the State Pattern
Technical requirements
An overview of the State pattern
Defining character states
Implementing the State pattern
Implementing the State pattern
Testing the State pattern implementation
Benefits and drawbacks of the State pattern
Reviewing alternative solutions
Summary
Managing Game Events with the Event Bus
Technical requirements
Understanding the Event Bus pattern
Benefits and drawbacks of the Event Bus pattern
When to use the Event Bus
Managing global race events
Implementing a Race Event Bus
Testing the Race Event Bus
Reviewing the Event Bus implementation
Reviewing some alternative solutions
Summary
Implement a Replay System with the Command Pattern
Technical requirements
Understanding the Command pattern
Benefits and drawbacks of the Command pattern
When to use the Command pattern
Designing a replay system
Implementing a replay system
Implementing the replay system
Testing the replay system
Reviewing the implementation
Reviewing alternative solutions
Summary
Optimizing with the Object Pool Pattern
Technical requirements
Understanding the Object Pool pattern
Benefits and drawbacks of the Object Pool pattern
When to use the Object Pool pattern
Implementing the Object Pool pattern
Steps for implementing the Object Pool pattern
Testing the Object Pool implementation
Reviewing the Object Pool implementation
Reviewing alternative solutions
Summary
Decoupling Components with the Observer Pattern
Technical requirements
Understanding the Observer pattern
Benefits and drawbacks of the Observer pattern
When to use the Observer pattern
Decoupling core components with the Observer pattern
Implementing the Observer pattern
Testing the Observer pattern implementation
Reviewing alternative solutions
Summary
Implementing Power-Ups with the Visitor Pattern
Technical requirements
Understanding the Visitor pattern
Benefits and drawbacks of the Visitor pattern
Designing a power-up mechanic
Implementing a power-up mechanic
Implementing the power-up system
Testing the power-up system implementation
Reviewing the power-up system implementation
Summary
Implementing a Drone with the Strategy Pattern
Technical requirements
Understanding the Strategy pattern
Benefits and drawbacks of the Strategy pattern
When to use the Strategy pattern
Designing an enemy drone
Implementing an enemy drone
Steps to implementing an enemy drone
Testing the enemy drone implementation
Reviewing the enemy drone implementation
Reviewing alternative solutions
Summary
Using the Decorator to Implement a Weapon System
Technical requirements
Understanding the Decorator pattern
Benefits and drawbacks of the Decorator pattern
When to use the Decorator pattern
Designing a weapon system
Implementing a weapon system
Implementing the weapon system
Testing the weapon system
Reviewing the weapon system
Reviewing alternative solutions
Summary
Implementing a Level Editor with Spatial Partition
Technical requirements
Understanding the Spatial Partition pattern
When to use the Spatial Partition pattern
Designing a level editor
Implementing a level editor
Steps for implementing a level editor
Using the level editor
Reviewing the level-editor implementation
Reviewing alternative solutions
Summary
Section 3: Alternative Patterns
Adapting Systems with an Adapter
Technical requirements
Understanding the Adapter pattern
Benefits and drawbacks of the Adapter pattern
When to use the Adapter pattern
Implementing the Adapter pattern
Implementing the Adapter pattern
Testing the Adapter pattern implementation
Summary
Concealing Complexity with a Facade Pattern
Technical requirements
Understanding the Facade pattern
Benefits and drawbacks
Designing a bike engine
Implementing a bike engine
Testing the engine facade
Reviewing alternative solutions
Summary
Managing Dependencies with the Service Locator Pattern
Technical requirements
Understanding the Service Locator pattern
Benefits and drawbacks of the Service Locator pattern
When to use the Service Locator pattern
Implementing a Service Locator pattern
Testing the Service Locator pattern
Reviewing alternative solutions
Summary
About Packt
Why subscribe?
Other Books You May Enjoy
Packt is searching for authors like you
Share Your Thoughts
Sections 1: Fundamentals
In this section of the book, we will review some design and programming fundamentals before moving on to the book's hands-on sections.
This section comprises the following chapters:
Chapter 1
,
Before We Begin
Chapter 2
,
The Game Design Document
Chapter 3
,
A Short Primer to Programming in Unity
Before We Begin
Welcome to the second edition of Hands-On Game Development Patterns with Unity; this edition is not merely a revision of the previous version, but a complete upgrade of the original book. After the first edition came out, I was fortunate to get a lot of constructive feedback, which inspired me to improve the structure of this new edition. As we will review in the following sections, this book concentrates on the "hands-on" aspect of the title; in other words, we are going to get our hands dirty and work on implementing systems and features for a fully playable prototype of a game with design patterns. This new approach to the book's structure will be more tangible and also more enjoyable. It's more fun to work on a playable game than with random code examples. So, before we begin, in the following sections, I will establish specific parameters to the book's content and overall approach.
Let's quickly review the topics we are going to see in this chapter, as follows:
Notes about the new edition
The philosophy of the book
What are design patterns?
Which subjects aren't covered in this book?
The game project
As mentioned, I redesigned this edition entirely based on reader feedback from the previous version, and so, in consequence, I decided to cut some content from the last edition that readers considered to be trivial, such as the following:
Game loop
and
u
pdate
pattern
chapters: These chapters focused too much on theory and didn't align themselves with the book's "hands-on" approach.
Anti-patterns
chapter: Anti-patterns is a complex and deep subject that deserves a book in itself to do it justice.
The end goal of this edition is not to cut content but to instead redesign the book to focus on practical game development uses of software design patterns in order to build a complete project. In other words, unlike the first edition, in which I took the approach of presenting each design pattern in isolation and with a self-contained code example, we will use them unitedly this time.
I added some chapters in this edition that were lacking in the previous version, such as the following:
The Game Design Document
: The beginning of a new game project often starts with writing a GDD. A GDD is a document that will help us understand the design intention behind the game systems and mechanics that we will build throughout the book.
A Short Primer to Programming in Unity
: We will use several advanced Unity engine concepts, C# features, and
o
bject-oriented programming
(
OOP
) techniques throughout the book. In this chapter, we will take the time to review them to establish a shared knowledge base.
This book is not a technical bible or the ultimate authority on how to use design patterns with Unity, and it's best described as a guide filled with design propositions to resolve some game programming challenges in Unity. The code examples included in each chapter are not flawless implementations because the art of design and programming is a continuous refinement process, and so the core goal of this book is to introduce you to potential solutions and inspire you to find better ones.
For those that are very new to programming, design patterns might be a novel concept. The simplest way of explaining design patterns is that they are reusable solutions to common software development problems. An architect named Christopher Alexander originated the notion of design patterns to describe reusable design ideas. In the late 1980s, inspired by the concept, software engineers started experimenting with applying concepts of reusable design patterns to software development, and over the years several books were written on the subject, such as the classic Design Patterns: Elements of Reusable Object-Oriented Software by the so-called "Gang of Four".But in this book, I will be avoiding covering the academic side of software design patterns, focusing instead on their practical use for programming game mechanics and systems in Unity. I will present a recurrent game programming problem in each chapter and propose resolving it using a specific design pattern adapted for the Unity application programming interface (API).
There are many facets to game programming, and a single book cannot cover them all with the depth they deserve. This book has a specific focus: design patterns and the Unity engine. So, if you are starting your journey into becoming a professional game programmer, this book will not be enough to complete your education. But luckily, some very talented individuals in our industry have taken the time to write very specialized books on core topics of game development. I recommend that anyone interested in joining the game industry reads each of the following reference books:
Physics programming:
Real-Time Collision Detection,
Christer Ericson
Engine programming:
Game Engine Architecture,
Jason Gregory
Three-dimensional
(
3D
) programming:
Mathematics for 3D Game Programming and Computer Graphics,
Eric Lengyel
Artificial intelligence
(
AI
) programming:
Programming Game AI By Example,
Mat Buckland
I have focused the content of this book on a specific aspect of game programming, but I will mention concepts from other domains of game development throughout the book. So, if you feel unfamiliar with some of the topics mentioned, take the time to explore them in depth; the time invested in researching will make you a better game programmer.
Throughout this book, we will be working continuously on a single game project example. The working title of the game is Edge Racer. As the title may indicate, it's a racing game; to be more specific, it's a futuristic racing game in which the player drives high-speed motorcycles. We will review the core concepts of the game in more detail in Chapter 2, The Game Design Document. But before continuing, I wish to list the reasons I decided on a racing game instead of another type of game—for example, a role-playing game (RPG)—as follows:
Simplicity
: Racing games have a simple premise—get to the finish line as fast as possible without crashing. Because this book is not about game design but game programming, I wanted a simple type of game that will permit us to focus on learning about software design patterns and not get bogged down with the implementation details of complex game mechanics.
Fun
: I've worked on various games of many different genres, and I always found that racing games are the most fun to develop because they are enjoyable to test. In racing games, you can speed-run to specific parts of the game and quickly reproduce bugs or test new features. Unlike other games with deep game mechanics and large maps, such as RPGs, racing games are usually quicker to debug.
Performance
: The main challenge of programming a racing game is maintaining a consistent frame rate as you add more features and content. So, I find working on racing games forces you to maintain good game-programmer habits by always keeping an eye on how fast your code is running and not just making it more readable.
Personal
: There's also a personal reason for me choosing a racing game—it's because it's my favorite genre. I love playing racing games and I love making them.
In conclusion, the game industry produces various products in many genres and sub-genres, but a racing game is a good reference point for us to start learning about design patterns in Unity because it's a simple context and forces us to keep an eye on keeping code clean and fast.
In this chapter, we reviewed the book's structure so that we can start with a clear understanding of its content and purpose. The key takeaway is that we will be using design patterns to build the mechanics and systems of a new racing game named Edge Racer.
In the next chapter, we will review the GDD to have a solid understanding of the game project that we will be working on in the upcoming chapters. I would not recommend skipping it because it's always a good practice to get to know as much as possible about a game project before starting writing code, as this helps in understanding how the parts fit the whole.
The Game Design Document
Before we start typing a single line of code, we need to complete a crucial step in a game's development cycle, which is creating a Game Design Document (GDD). A GDD is a blueprint of our entire project; its primary purpose is to put on paper the overall vision of our game's core concepts and act as a guide to a multidisciplinary team of developers during the arduous journey of a lengthy production cycle.
A GDD can include detailed descriptions of the following elements:
Lists of core visual, animation, and audio ingredients
Synopsis, character biographies, and narrative structures
Marketing research material and monetization strategies
Descriptions and diagrams that illustrate systems and mechanics