统一.安卓.更新应用后保存文件消失

Unity. Android. Save files disappears after updating an App(统一.安卓.更新应用后保存文件消失)

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

问题描述

我正在 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设备更改为另一个,您可以使用一个小脚本找到它:

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

希望对你有帮助,

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

本文标题为:统一.安卓.更新应用后保存文件消失

基础教程推荐