Book Image

Learning Design Patterns with Unity

By : Harrison Ferrone
Book Image

Learning Design Patterns with Unity

By: Harrison Ferrone

Overview of this book

Struggling to write maintainable and clean code for your Unity games? Look no further! Learning Design Patterns with Unity empowers you to harness the fullest potential of popular design patterns while building exciting Unity projects. Through hands-on game development, you'll master creational patterns like Prototype to efficiently spawn enemies and delve into behavioral patterns like Observer to create reactive game mechanics. 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 2023 book, the way you develop Unity games will change. You'll emerge not just as a more skilled Unity developer, but as a well-rounded software engineer equipped with industry-leading design patterns.
Table of Contents (23 chapters)
21
Other Books You May Enjoy
22
Index

To get the most out of this book

My north star is always making my content accessible to as many people as possible, which means I naturally try and keep prerequisites and barriers to entry fairly low or non-existent. However, design patterns are an intermediate programming topic, which means you need some foundational competencies to get going and feel confident with the material being presented.

Whatever brought you here, we need to lay out some basic requirements for getting the best possible experience from reading this book, so we’re all on the same page. Here’s what you’ll need before getting started:

  • Know how to write C# and its basic language components like variables, types, access modifiers, and control flow.
  • Understand and use Object-Oriented Programming concepts like classes, objects, interfaces, generics, events, delegates, inheritance, and encapsulation.
  • Be comfortable with Unity and the Unity editor, including navigation, creating scripts, and adding objects to the scene. You should also be comfortable with coroutines and basic Unity events like Awake, Start, and Update.

I don’t want all of this to sound discouraging, so just know that if you’ve been playing around with Unity and C# for a few months, maybe watched some tutorials, or even made a simple playable prototype or two - you can absolutely continue reading. This book is a reference, which means you’ll likely need to revisit chapters more than once to get things cemented in your mind. Don’t feel obligated to read the book in sequence either – the right tool for the job is the mainstay of everything you’ll learn here.

You’ll also need the current version of Unity installed on your computer—2023 or later is recommended. All code examples have been tested with Unity 2023.1 and should work with future versions without issues.

Requirements for the chapter exercises

Version

Unity

2023.1 or later

Visual Studio

2019 or later

Before starting, check that your computer setup meets the Unity system requirements at https://docs.unity3d.com/2023.1/Documentation/Manual/system-requirements.html.

Download the example code files

The code bundle for the book is hosted on GitHub at https://github.com/PacktPublishing/C-Design-Patterns-with-Unity-First-Edition. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Download the color images

We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://packt.link/gbp/9781805120285.

Conventions used

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

Code In Text: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. For example: “Manager.cs stores our score and loads the next scene.”

A block of code is set as follows:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Item: MonoBehaviour
{
    void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.tag == "Player"){
            // 1
            Manager.Instance.score++;
            Destroy(this.gameObject);
            Debug.Log("Item collected!");
        }
    }
}

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

public class Item: MonoBehaviour
{
    void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.tag == "Player")
        {
            // 1
            GenericManager.Instance.score++;
            Destroy(this.gameObject);
            Debug.Log("Item collected!");
        }
    }
}

Bold: Indicates a new term, an important word, or words that you see on the screen. For instance, words in menus or dialog boxes appear in the text like this. For example: “Navigate to Assets | Scenes, and double-click on SplashScreen.”

Warnings or important notes appear like this.

Tips and tricks appear like this.