Reader small image

You're reading from  Game Development Patterns with Unity 2021 - Second Edition

Product typeBook
Published inJul 2021
Reading LevelBeginner
PublisherPackt
ISBN-139781800200814
Edition2nd Edition
Languages
Tools
Right arrow
Author (1)
David Baron
David Baron
author image
David Baron

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.
Read more about David Baron

Right arrow
Preface
First principles, Clarice: simplicity. Read Marcus Aurelius.

"Of each particular thing, ask: What is it in itself? What is its nature?"

– Hannibal Lecter

The preceding quote is from one of my favorite films and sums up my approach to learning. Following over a decade working in the gaming industry, I have found that the only proper way to gain mastery over a complex system is by breaking it down into its most basic components. In other words, I try to understand the core ingredients before mastering the final form. Throughout this book, you will see that I'm taking a simplistic, but contextual, approach in presenting each pattern. 

The goal is not to dumb down the subject matter but learn by isolating the core concepts behind each design pattern so that we can observe them and learn their intricacies. I've learned this approach in the gaming industry while working as a designer and programmer. We will often build components and systems for our game in isolated levels that we call gyms. We would spend weeks iterating, testing, and adjusting each ingredient of our game individually until we understood how to make them work as a whole. I wrote this book in a way that's consistent with how I approach game development so you, as a reader, can immerse yourself in the subject matter while adopting some good habits along the way that will help you in your career.

However, it is also important to state that the content of this book is not the ultimate reference regarding patterns in Unity. It's just an introduction to the subject matter, not the final destination of the learning process. I'm not presenting myself as the foremost expert and do not wish my words to become gospel among developers. I'm just a developer trying to find an elegant way of using standard software design patterns in Unity and want to share what I discovered. Therefore, as the reader, I encourage you to critique, research, customize, and improve upon everything presented throughout this book. 

Who this book is for

While writing this book, I decided on a specific mental model of my target audience, for the main reason that it's almost impossible to write a book about game development and satisfy every potential type of reader, primarily because game development is a diverse industry and there are so many types of platforms and genres, each with their specific characteristics, that I cannot take into account in a single book. So I decided on focusing the content on a particular kind of audience, which I can describe as follows:

The target audience is game programmers who are currently working on a mobile or indie game project in the Unity engine and who are in the process of refactoring their code to make it more maintainable and scalable. The reader should have a basic understanding of Unity and the C# language.

Senior programmers working on large-scale AAA or MMO games might find particular examples in this book limited compared to the architectural challenges they usually face daily. However, on the other hand, the content of this book might offer another perspective on the use of design patterns in Unity. So feel free to skip any chapter if you already know the theory and want to see how I implemented a specific pattern.

What this book covers

Chapter 1, Before We Begin, is a brief introduction to the contents of this book.

Chapter 2, The Game Design Document, presents the design document behind the fully playable prototype of a racing game.

Chapter 3, A Short Primer to Programming in Unity, reviews some basic C# and Unity concepts.

Chapter 4, Implementing a Game Manager with the Singletoncovers the implementation of a globally accessible game manager with the infamous Singleton pattern.

Chapter 5, Managing Character States with the State Pattern, reviews the classic State pattern and how to encapsulate the stateful behaviors of a character.

Chapter 6, Managing Game Events with the Event Bus, covers the basic principles of the Event Bus pattern and how to use it to manage global game events.

Chapter 7, Implementing a Replay System with the Command Pattern, reviews how to use the Command pattern to build a replay system for a racing game.

Chapter 8, Optimizing with the Object Pool Pattern, covers how to use Unity's native implementation of the Object Pool pattern for performance optimization.

Chapter 9, Decoupling Components with the Observer Pattern, reviews how to decouple core components with the observer.

Chapter 10, Implementing Power-Ups with the Visitor Pattern, explains how to use the Visitor pattern to implement a customizable power-up game mechanic.

Chapter 11, Implementing a Drone with the Strategy Pattern, covers how to dynamically assign attack behaviors to enemy drones with the Strategy pattern.

Chapter 12, Using the Decorator to Implement a Weapon System, explains how to use the Decorator pattern as the foundation of a weapon attachment system.

Chapter 13, Implementing a Level Editor with Spatial Partition, reviews how to use the general concepts of Spatial Partition to build a level editor for a racing game.

Chapter 14, Adapting Systems with an Adapter, covers the Adapter pattern basics and how to use it to adapt a third-party library for reuse with a new system.

Chapter 15, Concealing Complexity with a Façade Pattern, uses the Façade pattern to hide complexity and establish a clean front-facing interface for a complex arrangement of interacting components.

Chapter 16, Managing Dependencies with the Service Locator Pattern, reviews the basics of the Service Locator pattern and how to use it to implement a system that allows the registration and location of specific services at runtime.

To get the most out of this book

To get the most out of this book, you need a basic understanding of the Unity engine. You will also need to be familiar with C# and have general knowledge of object-oriented programming. If you wish to reproduce the code presented in the upcoming chapters, you will need to download the latest version of Unity to your computer.

You can get the most recent build of Unity at the following link: https://unity3d.com/get-unity/download

The system requirements to run Unity can be found here: https://docs.unity3d.com/Manual/system-requirements.html

If you wish to download the source code made available on our GitHub repository, you will need a Git client; we recommend GitHub Desktop as it's the easiest one to use. You can download it at the following link: https://desktop.github.com/

Besides these tools, there are no other libraries or dependencies to download to run the code examples presented in this book.

Download the example code files

Code in Action

Download the color images

Conventions used

There are a number of text conventions used throughout this book.

CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "This class initializes the Context object and the states, and it also triggers state changes."

A block of code is set as follows:

namespace Chapter.State
{
public interface IBikeState
{
void Handle(BikeController controller);
}
}

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

namespace Chapter.State
{
public enum Direction
{
Left = -1,
Right = 1
}
}

Bold: Indicates a new term, an important word, or words that you see on screen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "These objects can publish specific types of events declared by the Event Bus to Subscribers."

Warnings or important notes appear like this.
Tips and tricks appear like this.

Get in touch

Feedback from our readers is always welcome.

General feedback: If you have questions about any aspect of this book, mention the book title in the subject of your message and email us at customercare@packtpub.com.

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/support/errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.

Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at copyright@packt.com with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Share Your Thoughts

Once you’ve read Game Development Patterns with Unity 2021, we’d love to hear your thoughts! Scan the QR code below to go straight to the Amazon review page for this book and share your feedback.

https://packt.link/r/<1800200811>

Your review is important to us and the tech community and will help us make sure we’re delivering excellent quality content.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Game Development Patterns with Unity 2021 - Second Edition
Published in: Jul 2021Publisher: PacktISBN-13: 9781800200814
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
David Baron

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.
Read more about David Baron