Home > OS >  store data persistent for learn app with unity
store data persistent for learn app with unity

Time:05-10

I'm currently working on a language learn app with unity. I want to implement that when you guessed a work (e.g. a number) incorrect, you need to guess the word again in the next iteration. I thought of a way that you store for each word in every play mode a value between 10 to -10 and when an item has a big negative number the word occurrence more often than if it has a big positiv number.

My Problem is that I don't know how to store the data properly. PlayerPrefs are too inconvenient for this problem, and I don't know how to modify a JSON file properly.

Currently, I store the data for the items in a class. enter image description here

Maybe you could have a structure like:

Numbers
   write
     zero: -5
     one:  3
   match
     zero:  5
     one:  4
Alphabet
   write:
     A: -10
   match: 
     A:  5

Does anyone have an idea how to solve this problem?

CodePudding user response:

One of the best JSON serialization libraries is Newtonsoft.Json.

You can use your class and serialize an object to JSON object, and then save it as a string to file.

public static string Serialize(object obj)
{
    var settings = new JsonSerializerSettings
    {
        MissingMemberHandling = MissingMemberHandling.Ignore,
        NullValueHandling = NullValueHandling.Ignore
    };
    return JsonConvert.SerializeObject(obj, settings);
}

After that you can save it to file in the Application.persistentDataPath directory.

var text = Serialize(data);

var tmpFilePath = Path.Combine(Application.persistentDataPath, "filename");
Directory.CreateDirectory(Path.GetDirectoryName(tmpFilePath));
if (File.Exists(tmpFilePath))
{
    File.Delete(tmpFilePath);
}
File.WriteAllText(tmpFilePath, text);

After that you can read the file at any time using File.ReadAllText and deserialize it to an object.

public static T Deserialize<T>(string text)
{
    var settings = new JsonSerializerSettings
    {
        MissingMemberHandling = MissingMemberHandling.Ignore,
        NullValueHandling = NullValueHandling.Ignore
    };
    
    try
    {
        var result = JsonConvert.DeserializeObject<T>(text, settings);
        return result ?? default;
    }
    catch (Exception e)
    {
        Debug.Log(e);
    }
    return default;
}
T result = default;
try
{
    if (File.Exists(path))
    {
        var text = File.ReadAllText(path);
        result = Deserialize<T>(text);
    }
}
catch (Exception e)
{
    Debug.LogException(e);
}
return result;

CodePudding user response:

Unfortunately, there is no easy way to store data persistently between play sessions in Unity. PlayerPrefs and creating your own JSON file are the simplest ways of doing this. The good news is that JSON files are quite easy to make and modify, thanks to the builtin JSONUtility Unity provides.

If you make a separate class or struct to hold your scores, give that clas a [Serializable] tag and keep a reference to that in your current setup (which is probably a MonoBehaviour).

You can use the File class (specifically File.CreateText() and File.OpenText()) to write to/read from a file. If you do this every time a value changes, you should end up with persistent saved data across multiple play sessions.

  • Related