Best way to store data locally for multiple users?

I have an application that needs to store the scores and playtime of multiple users(to be viewed from outside of the application) and fetch it again to put into charts in a seperate menu screen for graphs/user info. ( max 200 users)
I have tried this with playerprefs but could only figure out how to do this for only the last player, did I save incorrectly or should I do this in a different way, like SQLite or something?

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class SessionStore : MonoBehaviour
{
  public InputField iField;
  static public string currentName;
  static public int userNumber;

  public void CheckUser()
  {
    Debug.Log(iField.text);
    currentName = iField.text;
  }
  public void SaveStuff()
  {
    PlayerPrefs.SetString("Name", currentName);
  }

  public void LoadStuff()
  {
    currentName = PlayerPrefs.GetString("Name", "");
  }
}

This is what I made on a test setup but as you can see it only saves the current Name and loads it

Try to search Json documentation, it can save / load some info from a Json file, very usefull for server calls too

1 Like

I think what @RoMax92 may have left implied is that you don’t want to store that stuff in PlayerPrefs if you can avoid it. If your target platform supports it, you’ll want to save a file in the persistent data path. You may want to save one file per user or a file with all the users in it. I don’t know which is better for y our situation but I do know that PlayerPrefs has (for me) not been a safe place to store things you want to persist.

1 Like

Yeah I’m looking into using json right now, seems like a nice solution, since it also saves all the users into 1 file in a readable format. Just gotta figure out how to load in the data per specific user

With JSON, you will almost assuredly use a parser that makes the data look like a nested hierarchy of dictionaries…that’s all anything in JavaScript ever is anyway. So there will probably be a collection of things where the keys are integers (their approximation of an array), you’ll loop through those and find the one with a sub-property (like ID) that has a certain value (like the user’s ID).

It’s kind of like a poor man’s xpath…but, you know, JSON is what the kids are doing, these days, and if you don’t do it, too, everyone will laugh at you.

…and nothing is more important than the approval of your “peers”.

Ive been using this, and it has worked well for me.
http://amalgamatelabs.com/Blog/2016/Data_Persistence/
It has a little extra on how to do the saving/loading in WebGL, but more of it deals with System.IO stuff.

To me it’s like a super player prefs, being able to save everything playerprefs can, but in addition, arrays, custom classes, arrays of custom classes, as long as whatever is being saved is marked serializable.

In practice it’s saving a class to binary, so if you use arrays, that’s your hierarchy right there completely usable on load.

Also interested in hearing it’s downsides from more seasoned devs.

Yeah. You can use a formatter. If you do, you have to be very careful about changing your data structures once they are out in the wild.

1 Like

My suggestion:

  • During the game, store all that data in a ScriptableObject… or even a Monobehaviour
  • At the end of the game, simply serialize all that data to a file using JSONUtility
1 Like