Tomas Tomas - 1 year ago 98
Android Question

Unity. Android. Save files disappears after updating an App

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[] {
MakeLine(ref Attention_Results),
MakeLine(ref Memory_Results),
MakeLine(ref Reaction_Results),
FlyTime.ToString() };

File.WriteAllLines(Application.persistentDataPath+ @"/", lines);

Answer Source

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 external files directory paths changes from one Android device to another, you can use a small script to find it :

public static string GetAndroidExternalFilesDir()
    string[] potentialDirectories = new string[]

    if(Application.platform == RuntimePlatform.Android)
        for(int i = 0; i < potentialDirectories.Length; i++)
                return potentialDirectories[i];
    return "";

Also you have to make sure you allow external writing before doing it this way.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download