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 exce
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.
There's a huge problem in Unity that folks see totally wrong example code on the net, and ridiculous problems get propagated for years. Never, ever use "StreamWriter" - just use the trivial File.Write functions. Couldn't be easier.
Regarding the question above: "thousands" of entries is absolutely nothing. (Any irrelevant small icon image in your app will totally dwarf the size of your name/address data.)
(1) you can just save a text file(s),
It's incredibly easy to write and read files in Unity.
// 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
You can store the info in any of may formats such as JSON or csv. Since you have a tiny amount of information, you can trivially just keep the json as a List and access it easily.
Note that if you are unfamiliar with very basic programming tasks like using JSON or csv (all extremely easy) you will need to study that. SO questions/answers cannot be tutorials.
(2) you can learn about using the local SQL database, which is well worth learning. Do what Łukasz Motyczka says in the comments.
(3) it's true that your task is so small you could just use PlayerPrefs
. note however this is actually the hardest way! It would be a real mess, trying to use PlayerPrefs as a simple database. So don't do that.
(4) Note that in practice there are no "local apps" these days. To be an "app developer" is to be a cloud developer, likely you will need to use Firebase or the like (Parse having surprisingly closed down this year!)
You use the PlayerPrefs(Key,Value)
to Store the Data in Unity. it is one of the Simple Way To Store Data in Unity.