1

I am working on a 2D game which will be later used as advertisement activity on a stall, I need to store user information, Name, Number, Email and score. There data may exceed to thousands entries. What will be the best and free way to implement it in unity5? Game will be deployed on android.

Affan Shahab
  • 838
  • 3
  • 17
  • 39
  • I would start with SQL database... – Łukasz Motyczka Mar 11 '16 at 09:44
  • Can you please elaborate, how? @ŁukaszMotyczka – Affan Shahab Mar 11 '16 at 09:56
  • 1
    Hi, this was only an idea and I did not look into it more but as I can remember I had working SQLite database that i replaced with playerprefs (I did not need to store much data). Please look into: http://forum.unity3d.com/threads/tutorial-how-to-integrate-sqlite-in-c.192282/ or look for SQLiter whick is simple and free asset in asset store – Łukasz Motyczka Mar 11 '16 at 13:19
  • There is more info on the SQLLite in wiki page: http://wiki.unity3d.com/index.php/SQLite . Problem is code is in javascript, but with small effort its easy to translate it to C# (or maybe you do not need to). Anyway you can find there working class for DB management with example code how to use it :) – Łukasz Motyczka Mar 11 '16 at 14:11
  • Just for clarity, if you have less than say a million items, there is no reason to use a database. A few thousand items is absolutely nothing. You just hold it as any sort of list or dictionary, and just save it as a text file with one line of code. – Fattie Feb 20 '21 at 16:08

3 Answers3

4

NOTE - do not use "StreamWriter" for any reason.

Just use the trivial File.Write commands.

This is a common misunderstanding in Unity!

Regarding this topic, a bad example code was propagated on the www for years. Simply use File.Write.


Regarding the question on this page, "thousands" of entries is absolutely nothing.

Note that for example: any tiny icon image in your app will completely dwarf the size of your name/address data!

(1) extremely easy to save a text file:

// IO crib sheet..
// filePath = Application.persistentDataPath+"/"+fileName;
// check if file exists System.IO.File.Exists(f)
// write to file File.WriteAllText(f,t)
// delete the file if needed File.Delete(f)
// read from a file File.ReadAllText(f)

that's all there is to it.

   string currentText = File.ReadAllText(filePath);

NOTE WELL...........

// filePath = Application.persistentDataPath+"/"+fileName;
// YOU MUST USE "Application.persistentDataPath"
// YOU CANNOT USE ANYTHING ELSE/
// NOTHING OTHER THAN "Application.persistentDataPath" WORKS/
// ALL OTHER OPTIONS FAIL ON ALL PLATFORMS/
// YOU CAN >ONLY< USE Application.persistentDataPath IN UNITY.

Store the info any way you want, probably JSON or csv.

It is dead easy to use Json in Unity, there are 100s of examples on stackoverflow.

example https://stackoverflow.com/a/38535392/294884

(2) once you have say one million items. you can learn about using the local SQL database, which is well worth learning. Do what Łukasz Motyczka says in the comments.

(3) Never use PlayerPrefs - it is much harder and messy!

Fattie
  • 27,874
  • 70
  • 431
  • 719
1

For anyone else that comes here because they're trying to access Application.StreamingDataPath (or anything else within an APK for that matter) you cannot simply use the File api as discussed in Fattie's response. You need to use a web request as the APK is essentially a compressed folder and System.IO cannot access the internals of these packages.

You will need to use UnityWebRequest to GET the data from the desired location within the PKG. You can include raw files in your game by adding a folder anywhere in the project and calling it StreamingAssets (Resource folders are reconstructed inside the APK and can't be directly accessed, they're also loaded into memory at runtime which makes the game start very slowly), then point your UWR at Application.streamingDataPath and get the data from request.downloadHandler.data.

For saving new data, you have to use Application.persistantDataPath, at which point using System.IO.File is just fine.

Here is an example of grabbing data out of the APK!/assets/ folder (where android puts the StreamingAssets folder) and then saving it to a location for later use.

public IEnumerator GetSomeDataAndThenSave(string path)
{
    UnityWebRequest request = UnityWebRequest.Get(path);

    request.Send();

    while(!request.isDone)
    {
        yield return null;
    }

    if (!request.isNetworkError && (request.responseCode == 0 || request.responseCode == (long)System.Net.HttpStatusCode.OK))
    {
        WrapperClass yourClassList = new WrapperClass();
        YourClassList.members = JsonUtility.FromJson<YourClass>(request.downloadHandler.text);

        var bytes = System.Text.Encoding.UTF8.GetBytes(JsonUtility.ToJson(instance));
        File.WriteAllBytes(Application.persistentDataPath + "/SubDirectory/" + Path.GetFileName(path), bytes);
    }

    request.Dispose();
}

Hope that clears things up for anyone else having the same issue.

  • Hi Seamus - the OP on this question (very simply) wants to just write, then read, some data. (FYI there was considerable discussion about this in the comments, now gone!) It's just a line of code, as stated in my answer. Almost every game does this - it's nothing, totally trivial. Seamus' example here explains how to load from "packages you send with your app install" (very useful to some readers I'm sure). – Fattie Mar 06 '18 at 22:03
0

You use the PlayerPrefs(Key,Value) to Store the Data in Unity. it is one of the Simple Way To Store Data in Unity.

Darshan Soni
  • 319
  • 3
  • 14
  • ya i read your Question.. another simple way To Store Large Data Using File. you Store the bunch of Data in File ( Ex :- text File ) . its easy to Write data using File I/O in Unity. – Darshan Soni Mar 11 '16 at 13:41
  • @JoeBlow sorry for being un-responsive. I left that project. It was urgent and didn't had time for experiments. – Affan Shahab Apr 12 '16 at 09:21