Best way to save large amount of data locally in unity3D android? [closed]

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!

Leave a Comment