What is the best way to save a deck of cards?


I’ve come across quite a few solutions and methods to deal with this but was hoping I could get some input on the best approach for my specific situation.

I’m developing a card game with deck building. I’d like to create a “default deck” with all cards from which a player can unlock cards and then choose what cards he wants in his custom deck and save one or several decks. The cards themselves don’t contain a lot of information; id, name, type, attack, defense, flavor text.

  • I’d like to avoid PlayerPrefs as I’ve heard it’s not the best solution as it’s slow and fairly inflexible.
  • I could use XML but I think for this purpose it seems a bit overkill and I also don’t like the idea of having the card info so easily accessible in the game folder.
  • It doesn’t have to work on web but it would be a big plus

Now, my thought was that I just hard code the cards (and their values) using a class. So when it comes to storing custom decks I can just store an array of ids to be loaded from the default deck. Would it be possible to serialize this somehow and save to disk using BinaryFormatter? What would that look like?

If I’m on the wrong track, please let me know! I’m really keen to get this aspect of the game done :slight_smile:

Thanks for you help!


You pretty much said it yourself. Using the binaryformatter to serialize a Deck class that stores a List and the Card class is just a series of public variables.

Make sure your classes use [Serializable], and MonoBehavior can’t be serialized so don’t inherit from that (I haven’t tested this, but I’ve read it somewhere else so this may have changed in Unity 5, im not sure).


Deck mydeck = new Deck(generic_list_of_cards);

FileStream fs = new FileStream(filename_location, FileMode.Open);
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(fs, mydeck);

then you would do pretty much the same and do formatter.Deserailize() to get the data back out of the file.