【问题标题】:Unity. Android. Save files disappears after updating an App统一。安卓。更新应用后保存文件消失
【发布时间】:2016-10-11 17:46:50
【问题描述】:

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

那么如何创建更新后不会被删除的文件呢?

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

【问题讨论】:

  • 您是否有不想使用 PlayerPrefs 的特殊原因?
  • 我需要保存数字数组,据我所知,PlayerPerfs 无法做到这一点。
  • 你能贴出你用来保存文件的代码吗? system.io 的东西。
  • 如果我是你,我会考虑使用 JSON 和 PlayerPrefs。这是一个免费且非常酷的 json 类,您可以统一使用 wiki.unity3d.com/index.php?title=JSONObject 基本思想是您将创建一个 json 对象,将所有数据添加到其中,然后将该对象作为单个字符串保存在 Player Prefs 中。然后要读取保存的数据,您可以从 PlayerPrefs 中获取字符串并将其转换回 JSONObject。
  • 问题是我已经创建了具有保存、加载、修复等功能的大型类。并且在不切换到 JSON 的情况下找到解决方案将为我节省很多时间。不过谢谢你的建议,我会看的。

标签: android unity3d save


【解决方案1】:

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

如果您不想使用 PlayerPrefs(我认为这是最强大的可用解决方案),您可以随时直接写入用户设备。

警告:请记住,这样做远非完美的解决方案!用户可以删除和编辑您保存的文件,这与其说是真正的答案,不如说是一种解决方法。

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

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

希望对你有帮助,

【讨论】:

    【解决方案2】:

    三星和许多其他 Android 设备使用序列号作为外部 SD 卡的挂载点。基于@Kardux 对内部存储的(原始)答案,以下将支持外部存储(没有特定目录),然后搜索内部 sdcard 作为后备。

    public static string GetAndroidInternalFilesDir()
    {
        string[] potentialDirectories = new string[]
        {
            "/storage",
            "/sdcard",
            "/storage/emulated/0",
            "/mnt/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 "";
    }
    

    注意:这假设外部存储目录首先出现在目录列表中。然后,它会根据成功的可能性对访问存储的尝试进行优先级排序,并添加/storage/emulated/0,许多设备在无法访问/sdcard 时可以访问。

    【讨论】:

    • 原谅我的无知,我无法让脚本工作,有什么指导吗?
    • @SamuelDiaz 这是一个要添加到现有脚本并从中调用的函数。
    猜你喜欢
    • 1970-01-01
    • 2017-01-20
    • 2013-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 2014-06-02
    相关资源
    最近更新 更多