I'm creating pretty simple game in Unity, and want to save results, time, etc. I use System.IO for it, and everything works fine, but when i update my app from '.apk' all the results disappear.
So how can i create file, which won't be deleted after update?
public static void Save()
{
string[] lines = new string[] {
Language,
First_Test.ToString(),
Last_Test.ToString(),
MakeLine(ref Attention_Results),
MakeLine(ref Memory_Results),
MakeLine(ref Reaction_Results),
Attention_Best.ToString(),
Memory_Best.ToString(),
Reaction_Best.ToString(),
LastSend.ToString(),
UserName,
FlyTime.ToString() };
File.WriteAllLines(Application.persistentDataPath+ @"/Progres.save", lines);
}
Samsung and many other Android devices use a serial number as the mount point for external sdcards. Building on @Kardux's (original) answer for internal storage, the following will favor external storage (without a specific directory), then search for the internal sdcard as a fallback.
Note: This assumes the external storage directory appears first in a directory list. It then prioritizes the attempts to access storage by likelihood of success and adds
/storage/emulated/0
, which many devices can access when unable to access/sdcard
.EDIT: Depending on your needs you may want to change the order of the potential directories and even favor external storage. If so please look at @LoungeKatt answer.
If you don't want to use the PlayerPrefs (which is I think the most robust available solution), you can always write directly into the user device.
WARNING: keep in mind doing it this way is far from a perfect solution ! The user can delete and edit the files you save and this is more of a workaround than a real answer.
Anyway, since the internal files directory paths changes from one Android device to another, you can use a small script to find it :
Hope this helps,