I've read several documents on how to manage game type data:

Would it be better to use XML/JSON/Text or a database to store game content?

How to choose how to store data?

Im developing an offline RPG game in Unity3D. I've made online games before, but with online we had an edge for the database. For the online game we used a MySQL database each time the player loaded the game, they would download the database (JSON setup) and we would parse it on the client side and it would be good to go.

However for an offline RPG I don't have the luxury of storing my game type data online. To be clear when I say type data I mean things like: -Monster tables -Item tables -Ability tables etc

What I liked about the MySQL database was that it was tabluar and I could edit rows and columns easily and not need to create hundreds of classes/subclasses to manage the type data.

My question is how can I mimic the MySQL database in Unity3D?

I considered using an excel file but I feel that would be so easy for the user to hack. Just change the excel data and boom, you've hacked the game.

Is there a way I can create an offline (secure-ish) flatfile database that will let me avoid having to create tons of type data class files to manage all my game data?


2 Answers 2


Depending on your needs, you could just serialize a Dictionary to a binary file. I can post some code after I get home, but this link explains the serialization features of C#.

This is only secure-ish (a binary file is harder to hack than an excel spreadsheet) but that's all you requested. I can't tell from your question if this is data generated during the game or data you created ahead of time, but if the latter then you could just write an editor within Unity itself.

Alternatively, you could simply not care if the user hacks this data. If this is a single-player game, then let the player do what they want. If this is a multiplayer game, then you need more security anyway.

EDIT: Here's a Unity specific resource about saving a serialized object: http://gamedevelopment.tutsplus.com/tutorials/how-to-save-and-load-your-players-progress-in-unity--cms-20934

This most important Unity-specific thing mentioned there is Application.persistentDataPath

  • \$\begingroup\$ Valid points. But I'd at least like to deter someone from just having to open an excel file, if they want to hack it hardcore Im not worried, but I want to stop 'casual hackage'. I'll take a look at that link, thank you. Regarding my data, all type data is pre-made (i.e. I create a monster table, and then each column would be a stat, and then the rows would be specific monsters with values filled in for those stats) \$\endgroup\$
    – Aggressor
    Feb 10, 2015 at 19:11

There's a few variables that aren't mentioned like how big you expect it to be, what platform the games are going to go on, etc so as usual there's a lot of "it depends" on any answer.

One option is to serialize the data out to JSON. If you store some sort of checksum against the data you'll immediately know if the data has been changed. You can also encrypt the data. The JSON can be serialized into classes and collections and you can use LINQ to query them.

This does add overhead of deserializing the data (and decrypting if you go that route) upon startup and making sure both the data and any checksum is versioned. The biggest thing is you'll probably be sucking in the entire dataset into RAM which is something you may not want to do.

Inventory can be serialized to PlayerPrefs.

EDIT: I stand corrected. It was pointed out to me in comments that PlayerPrefs shouldn't be used for inventory and the following video provided. It provides plenty of info that pertains to this question (though it's pretty long and rambles on a bit): http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/persistence-data-saving-loading

  • 3
    \$\begingroup\$ "Inventory can be serialized to PlayerPrefs." Unity advises against storing game progress or inventory in PlayerPrefs. (See: unity3d.com/learn/tutorials/modules/beginner/… ) - This data is plaintext, so it doesn't meet the asker's "secure-ish" criterion. PlayerPrefs is really only meant for preference data, like look inversion or subtitle settings - stuff for which there's no advantage to doctoring the data, and no loss of game progress if it is reset. \$\endgroup\$
    – DMGregory
    Feb 10, 2015 at 19:56
  • \$\begingroup\$ Also there is a filesize limit. Note where your link says "the file size is limited to 1 megabyte." You'd fill that up with all the monsters, items, and abilities in a decently sized RPG. \$\endgroup\$
    – jhocking
    Feb 10, 2015 at 20:10
  • 1
    \$\begingroup\$ Thanks for the heads up. Yea playerprefs is never a good place to hold a database! Figuring out the best binary storage/format has been my biggest sticking point. Im going to look into LINQ, I've never heard of that, thank you! \$\endgroup\$
    – Aggressor
    Feb 10, 2015 at 21:22
  • \$\begingroup\$ Never suggested the actual database be stored in PlayerPrefs only the inventory - and that suggestion has been rescinded. Only warning on Linq is that some of the methods don't play nice on iPhone/iPad due to JIT compilation. \$\endgroup\$
    – McAden
    Feb 10, 2015 at 21:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .