我是Unity的新手。我有这样的问题。如何在Unity中从sdcard写入/读取txt文件?
我的游戏分数很高,我想将它保存到移动设备上的文本文件中。在这种情况下,我想将该文本文件保存到我的SD卡,但我不知道如何。
下面的代码显示了它如何读写文件。它在我的电脑上完美工作,但它不在模拟器或真正的移动设备上。
我认为它对目录感到困惑。
void writeHighScore() {
string path = "\\sdcard\\colorbounce\\highscore.txt";
int highscore = 0;
// This text is added only once to the file.
if (!File.Exists(path))
{
System.IO.Directory.CreateDirectory("\\sdcard\\colorbounce\\");
// Create a file to write to.
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine(score);
}
}
// Open the file to read from.
using (StreamReader sr = File.OpenText(path))
{
string s = "";
while ((s = sr.ReadLine()) != null)
{
highscore = int.Parse(s);
}
}
if (score > highscore) {
// This text is always added, making the file longer over time
// if it is not deleted.
using (StreamWriter sw = File.AppendText(path))
{
sw.WriteLine(highscore);
}
}
highScoreText.text = highscore.ToString();
}
请帮我了解一下。 谢谢。