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 "";
}
希望对你有帮助,
这篇关于统一.安卓.更新应用后保存文件消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:统一.安卓.更新应用后保存文件消失
基础教程推荐
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01