Power of wrappers in Unity (ig)

Have you ever tried to serialize a matrix in the Unity Inspector or other complex data structures? Initially, Unity will not serialize fields like List<List<T>> unless we use a wrapper!

That’s the key idea: we don’t serialize List<List<T>> directly. Instead, we create a brand new serializable class with a property like List<T> values, and then serialize List<WrapperClass> instead!

Example:
Our wrapper class: csharp [Serializable] public class ListOfStrWrapper { [TextArea(3, 7)] public List<string> values = new List<string>(); }

and the fields in another object:

```csharp public List listOfStrWrappers = new List();

public List listOfListOfStrNoWrapper = new List(); ```

listwrapper.png

It's might be not very clear, but our second structure hasn't serialized!

But what is the actual use of it?

I came up with this approach to create relatively complex dialogues conveniently and quickly (very useful for jams! (ig, since I don’t have much experience, haha)).

In my project, each single dialogue is represented as a List<DialogueNode> (which is basically a container for the actual text of each node, speaker portrait, their name, etc.).

However, I need my entities to contain several dialogues, which would mean using List<List<DialogueNode>> — and that is not serializable!

So, this is exactly where a wrapper becomes useful. csharp [Serializable] public class DialogueContainerWrapper { public List<DialogueNode> values = new List<DialogueNode>(); } and create List<DialogueContainerWrapper> instead! Result:
dialogueWrapper.png
And dialogue example:
wrapper dialogue.gif

Pros and Cons

Pros: Fast, easy, and convenient for small data structures!

Cons: It may become messy when working with grotesque objects and containers. It is fine for around 10 objects with a small number of attributes, but as that number grows, things can get very inconviniet.

For bigger containers, it would be better to create a ScriptableObject container and assign a list of them instead, or load them from resources.

Hope you found this small article useful! I’m still learning the engine, and advice from more experienced people would be greatly appreciated, haha!

My very first jam entry