统一.安卓.更新应用程序后,保存文件消失 [英] Unity. Android. Save files disappears after updating an App

查看:125
本文介绍了统一.安卓.更新应用程序后,保存文件消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Unity中创建非常简单的游戏,并希望节省结果,时间等. 我为此使用System.IO,并且一切正常,但是当我从".apk"更新我的应用程序时,所有结果都消失了.

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);
}

推荐答案

编辑:根据您的需要,您可能需要更改潜在目录的顺序,甚至希望使用外部存储.如果是这样,请查看@LoungeKatt answer .

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.

如果您不想使用 PlayerPrefs (我认为这是最可靠的解决方案),则始终可以直接写入用户设备.

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.

无论如何,由于内部文件目录路径从一台Android设备更改为另一台Android设备,因此您可以使用一个小脚本来查找它:

Anyway, since the internal files directory paths changes from one Android device to another, you can use a small script to find it :

public static string GetAndroidInternalFilesDir()
{
    string[] potentialDirectories = new string[]
    {
        "/mnt/sdcard",
        "/sdcard",
        "/storage/sdcard0",
        "/storage/sdcard1"
    };

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

希望这会有所帮助,

这篇关于统一.安卓.更新应用程序后,保存文件消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆