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

Adding thread safety to the generic singleton

In this section, we’re going to focus on making our generic singleton thread-safe by guarding against different threads potentially creating more than one instance of the singleton. For example, imagine you need to process an algorithm that finds all enemies within a set distance, as well as their positions and orientations. Doing this on the main thread might decrease your frame rate and slow down other processes in your game. However, if you separate the algorithm into a separate thread, Unity’s main thread is free to keep running the essential parts of the game while the sub-thread does its work.

Thread safety in multithreaded environments is a huge topic and has additional implications when using Unity APIs. For a deeper dive, check out .NET Multithreading by Alan Dennis at https://www.manning.com/books/net-multithreading.

If you’re new to the concept of threading, it helps to think of your...