I’m still learning Unity3d and I’m using the C# language since I already know the language basics. Problem is, I never learned how to save data during application runtime. Example would be like a save file of game where it saves the heroes progress.
I was learning the C# language last year and pretty much learned all the basics of the language from variables, statements, conditions, loops, classes, delegates, events, but never got to far beyond that such as .Net libraries and APIs.
If it’s too much to explain, then can someone direct me to a link to help me with being able to save gamedata during gameplay using C#?
There are a ton of options for saving player state. This list is by no means exhaustive, but should provide you with a starting point.
PlayerPrefs
You can save any arbitrary data to PlayerPrefs. It’s essentially an abstracted, platform independent key-value store. While it’s very convenient, it can also be difficult to develop a complex, robust save system, because PlayerPrefs is essentially opaque, in that that content of PlayerPrefs is not stored in a human-readable format. I should note that apparently there are packages that let you view the content of PlayerPrefs, but I cannot confirm whether or not they work, or about the quality of these tools.
Local Save File
On my current project, I’m storing player save files locally, in a JSON formatted text file, located inside of Application.PersistentDataPath. The advantage of this approach is that it’s human-readable, which is invaluable during development. It’s also a more modular, and extensible approach since you could create different files for different type of save-data, such as save files, settings and preferences, etc.
iCloud
I should also note that if you’re developing for the iPhone or iPad, you should also consider saving your game state using iCloud. The advantage of iCloud is that your player state would be saved “into the cloud” so that it game be synchronized across all of the player’s devices. If you’re interested, there’s a Prime31 plugin for iCloud, that presents a very similar interface as PlayerPrefs. I haven’t used it personally, but all of their work, in my experience, has been very top-notch.
Google Play Game Services
Recently Google announced a new cloud games services platform, Google Play Game Services. It’s a cross-platform (iOS, Android, and Web) cloud services platform, that among other things, support saving game state. Coinciding with this announcement, Prime31 released a plugin for Google Play Game Services on iOS. There’s no Android version at the moment, but I imagine it won’t be far behind.
If you are building a webplayer you can serialize your data and send it out over http with the WWW class and save the data on a server in something like a mysql database. Then load the data back at the start of the application with another WWW request and deserialize the data string your receive.
WWW request = new WWW("http://myserver.com/load.php?playerID="+id);
yield request;
Data data = Deserialize(request.text);
Debug.Log("Data loaded.");
This solution expects of course that you have access to your own apache+php server. Which you can install locally on Windows and it comes native on Mac, although you may have to do some configuring to enable it.
Along with all the other good solutions… I have a few recommendations. I would use playerprefs for a storing game settings … volume, graphical preferences. However, playerprefs are easily ediable by the user so be warned.
The next option up is XML format. It works well for storing data because there are built in classes that let you read, change, and add xml nodes easilly. This can also be encrypted and hidden so it is harder for the user to change.
This last option is the most complex but vital for some games… Storing data in a sqllite database. If you need to store a lot of data this is the way to go. This is great if you are storing a whole ton of high scores or a lot of complex data in general.
And it’s not like these options can’t coexist either. You might use all in one game.