Serialization of a Dictionary of Monobehaviour objects

Hello everyone,

I have a dictionary that I want to serialize:

public Dictionary<int, Item> invBag

Where Item derives from MonoBehaviour. I’ve been reading a lot on Serialization and it looks like I can’t directly serialize Item. From what I can tell I then have a couple of choices.

  1. Use a Memento Pattern. (http://en.wikipedia.org/wiki/Memento_pattern). Where I would run through each Item in the dictionary, encapsulate that into a new Serializable class, put that into an array and serialize all of that. Then after we deserialize we load values back into dictionary.
  2. Unity Serializer. Looks like a very convenient option. I would like to understand this process more which is why I would like to try and avoid this.
  3. Build a Serializer. Probably beyond my abilities at the moment but I’m willing to look into it.

So my question is - am I missing something simple? Are these my only (reasonable) choices? I’ve already setup serialization for a simple class with basic Types (strings, int, float, etc.) but I’ve hit a roadblock with this.

Thanks all!

I’d wager option 3 is best. I’ve never used a Momento Pattern before, but reading through the first paragraph it sounds like way too much trouble. Option 3 isn’t all that bad.

What you need to understand about serialization is that you can break it up into two core bits. You have the actual serializer, the brains of the operation, and then you have a wrapper that turns your game data into data the serializer can process. This means that you don’t need to write your own serializer, you merely have to write the wrapper for someone else’s serializer. Seeing as you’re not trying to serialize something complex like a gameobject and all of it’s components, this should be a pretty easy task.

For Unity I think Protobuf is your best choice. It’s fast, file sizes are small, and it’s syntax is fairly simple. Google Code Archive - Long-term storage for Google Code Project Hosting.