I am making a clicker-like Android game on Unity and I need to store some data everytime something happens in the game, but it will be very simple data, like some ints and strings. Is it viable to do it serializing it with something like json and store in a file?
Asked
Active
Viewed 2,806 times
2
-
1may be you can make use of Preferences (http://developer.android.com/reference/android/content/SharedPreferences.html) – droidev Dec 07 '15 at 04:26
-
2Possible duplicate of [How to save data in an android app](http://stackoverflow.com/questions/10962344/how-to-save-data-in-an-android-app) – Pratik Butani Dec 07 '15 at 04:26
-
For your current question, I'll suggest you to store this game related data in sqlite database file (follow the link given by @VividVervet). I don't recommend shared prefs as they can be cleared. – Vishal Sharma Dec 07 '15 at 04:29
-
@Vishal sh I dint get "as they can be cleared" – droidev Dec 07 '15 at 04:31
-
If you go to app info and clear data, shared preferences are deleted. That's what happens with my app, I store some registration details in shared prefs in my app, and that restricts the registration dialog popping up. However, when I clear data, data in shared prefs is deleted and popup reappears. – Vishal Sharma Dec 07 '15 at 04:37
-
and do you think database will persist if you clear from the app setting ? – droidev Dec 07 '15 at 04:39
-
@Vishal sh you must recheck that information – droidev Dec 07 '15 at 04:47
-
1The option "Clear Data" will remove the sqlite data as well. But I think, if a User is clicking on that option, then that User is well aware of this fact. For your case, you can use SharedPreferences for now. But in case you want to go for complicated/composite data, I would suggest SQLite. – avin Dec 07 '15 at 05:40
-
try [ArrayPrefs2](http://wiki.unity3d.com/index.php/ArrayPrefs2). It may help you – Hamza Hasan Dec 07 '15 at 11:31
2 Answers
1
Why you want to store it in JSON you can simply use PlayerPref. If you get any difficulty in getting data from it then, this will also help you.
Stores and accesses player preferences between game sessions.

Muhammad Faizan Khan
- 10,013
- 18
- 97
- 186
1
As Mohammed Faizan Khan said, you can use PlayerPrefs
or persistentDataPath
to keep and access the data.
A simple example for PlayerPrefs
:
private int score = 0;
private int savedScore;
void Update () {
if (Input.GetKeyDown (KeyCode.S)) {
PlayerPrefs.SetInt("Score", score);
Debug.Log(score);
}
if (Input.GetKeyDown (KeyCode.L)) {
savedScore = PlayerPrefs.GetInt("Score");
Debug.Log(savedScore);
}
A simple example for persistentDataPath
:
private string savedName;
private int savedHealth;
private string loadedName;
private int loadedHealth;
public void Save(){
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/FileName.dat", FileMode.Create);
PlayerClass newData = new PlayerClass();
newData.health = savedHealth;
newData.name = savedName;
bf.Serialize(file, newData);
file.Close();
}
public void Load(){
if (File.Exists(Application.persistentDataPath + "/FileName.dat")){
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/FileName.dat", FileMode.Open);
ObjData newData = (ObjData)bf.Deserialize(file);
file.Close();
loadedHealth = newData.health;
loadedName = newData.name;
}
}
[Serializable]
class PlayerClass{
public string name;
public int health;
}
Remember that, you need
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
namespaces for persistentDataPath
.