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...