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

Working with MonoBehaviours

Originator classes don’t have to be straight C# classes; with a little tweaking, they can also be Unity components. The thing you have to remember is that the Memento pattern is best suited to originators that want to keep their encapsulation intact, which means you probably won’t have a ton of public variables in an originator Monobehaviour.

Unity does allow you to work with private variables (as you probably know), but it also has the option to serialize fields (which you might not know). But wait, how can we serialize a field if we have a variable with a get and set property, especially one with different access modifiers? Since Unity automatically generates backing fields for all properties, you can use a special attribute to tell Unity to serialize the backing field of any property, giving us the best of both worlds – encapsulation and serialization.

Update Character.cs to match the following code, which adds the [field...