将 txt 文件保存到 Oculus Go 内部存储



我正在使用Unity构建一个Oculus Go应用程序,但我不知道如何将txt文件保存到Oculus存储中。我已经阅读了我在网上找到的所有关于它的内容,包括在这里和这里使用这个网站上提出的解决方案。

我正在尝试创建一个文本文件,该文件记录用户从切换组中选择了哪个按钮。

当我为 PC 构建同样的东西时,代码可以工作,但我在 Oculus Go 中找不到文件。我已经编辑了 OVR android 清单,并通过以下几个教程制作了一个非常简单的脚本。

任何帮助将不胜感激,谢谢!

using UnityEngine;
using UnityEngine.UI;
using System.Linq;
using System.IO;
public class SubmitandWriteButtonOVR : MonoBehaviour
{
//GameObject
public ToggleGroup toggleGroup;
public void onClick()
{
string selectedLabel = toggleGroup.ActiveToggles()
.First().GetComponentsInChildren<Text>()
.First(t => t.name == "Label").text;
//Path of the file
string path = Application.persistentDataPath + "/Answers.txt";
//Create file if it doesn't exist
if (!File.Exists(path))
{
File.WriteAllText(path, "Answers");
}
//Content of the file Get the label in activated toggles
string content = "Selected Answers: n" + System.DateTime.Now + "n" + selectedLabel;
//Add some text
File.AppendAllText(path, content);
}
}

这里有几件事可能是错误:

1(尝试像这样设置根路径

rootPath = Application.persistentDataPath.Substring(0, Application.persistentDataPath.IndexOf("Android", StringComparison.Ordinal));

2( 即使根路径设置正确,文件也可能不显示。尝试将答案.txt手动放入您的 GO,将一些内容写入答案.txt从您的应用程序内部,重新启动您的 go,然后在资源管理器中再次检查它是否写入您的文件。我读到它与Android文件系统有关,没有意识到此文件已被创建/更新,因此它没有显示。

3(确保您通过播放器设置对外部SD具有写入权限

这是我在Android上为StreamWriter提供的解决方案:

private void WriteLogToFile()
{
Debug.Log("Starting to Write Logfile");
string path = Path.Combine(rootPath, "Logs.txt");
StreamWriter writer = new StreamWriter(path, true);
writer.WriteLine("TEST");
writer.Close();
Debug.Log("Logging Done");
}

尝试一下,结合上面的所有提示,可以帮助您实现目标并让我知道。

最新更新