- Home /
Best way to store\retrieve simple textual data?
For example, in an RPG you might want to store a list of weapons, with their various attributes, names of their models and textures, etc. What is the preferred way of accomplishing this in unity?
Answer by Ray-Pendergraph · May 26, 2011 at 09:10 PM
We do a lot of this in XML (System.Xml). Most of the missions on our games have models (domain models not 3D models) of at least one kind or another and the System.Xml stuff is very easy to use and bind to your model classes to feed your game. To store the data you can also put the XML files in asset bundles (as text assets) and slurp them off the disk or across the wire using the WWW class and parse the text bytes in the game. You can build them into the game by putting them into a folder in the project name "Resources" and use the Resources.Load to pull them in. This makes the data baked into the game at build time though as opposed to having the ability to get new configurations via the asset bundle functionality.
I am not a huge XML advocate but it is the most supported common interchange format. I would love to attempt YAML or JSON to write the configuration files as XML is so verbose. Most of those libraries are open source and we have not had a chance to test them out since some of them look to be unmaintained. We have not noticed any significant overhead but we keep the parsing to specific parts of the game... you don't want to parse it 'during' the game.
The X$$anonymous$$L file in Resources definitely seems the best option for hard-coded data storage. $$anonymous$$y problem now is storing player state data that is more complex than PlayerPrefs seems suited for. I think serializing\deserializing some kind of PlayerState class will be the way to go there, but I've yet to figure out how that works.
On various platforms (particulary the Windows and $$anonymous$$ac standalone) you can use System.IO to persist the same type of models with just regular IO. I have done this to persist various game states. Depending on what platform you are on standard IO packages may or may be accommodated. Check out the Application object to see what paths might be available. http://unity3d.com/support/documentation/ScriptReference/Application.html
Could you define the definition of "model" you're using? Not knowing is confusing me. :)
$$anonymous$$odel as in domain model (http://en.wikipedia.org/wiki/Domain_model) which represents the data like user data, game state, inventory, score, damage, etc. You can use attributes from the X$$anonymous$$L namespace (System.Xml) to easily annotate the model classes that represent these things in the game and save them out to disk and even persist them remotely on a server to a database or service. On some Unity platforms particularly the desktop platforms (Winblows and $$anonymous$$ac) you can use standard C# file IO to pretty much do anything you want with persisting data to the disk (encrypt them, whatever). The mobile platforms probably differ at least a little.
Your answer
Follow this Question
Related Questions
How to store profile data in unity with firebase 0 Answers
Where to store data about location 0 Answers
Store item images in database or on device? 0 Answers
Custom Editor Window to Create and Store Item Data 1 Answer
Data Storage 5 Answers