Storing Type/Game Data Within A Unity 3D Game Using An Offline Flatfile Database
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?
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:
This most important Unity-specific thing mentioned there is Application.persistentDataPath
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
- Database Administration Tutorials
- Programming Tutorials & IT News
- Linux & DevOps World
- Entertainment & General News
- Games & eSport