简体   繁体   English

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

[英]Saving a txt file to the Oculus Go Internal Storage

I'm building an Oculus Go app with Unity but I can't figure out how to save a txt file to the Oculus Storage.我正在使用 Unity 构建 Oculus Go 应用程序,但我不知道如何将 txt 文件保存到 Oculus Storage。 I have read everything I've found online about it, including using solutions proposed on this website here and here .我已经阅读了我在网上找到的关于它的所有内容,包括使用本网站在此处此处提出的解决方案。

I'm trying to create a text file that records which button from a toggle group was selected by the user.我正在尝试创建一个文本文件,用于记录用户选择了切换组中的哪个按钮。

When I build the same thing for PC the code works but I can't find the files inside the Oculus Go.当我为 PC 构建相同的东西时,代码可以工作,但我在 Oculus Go 中找不到文件。 I have edited the OVR android manifest and have a very simple script made from following a couple of tutorials.我编辑了 OVR android 清单,并根据几个教程制作了一个非常简单的脚本。

Any help would be appreciated, thank you!任何帮助将不胜感激,谢谢!

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

    }
}

There are several things that could be the error here:有几件事可能是这里的错误:

1) Try to set your rootpath like this 1)尝试像这样设置你的根路径

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

2) Even if your rootpath is set correctly the file might not show up. 2) 即使您的根路径设置正确,该文件也可能不会显示。 Try putting Answers.txt manually onto your GO, write something into Answers.txt from inside your App, restart your go and check again in Explorer if it wrote to your file.尝试将 Answers.txt 手动放入您的 GO,从您的应用程序内部将一些内容写入 Answers.txt,重新启动您的 Go 并在资源管理器中再次检查它是否已写入您的文件。 I read that it has to do with the Android file system not realizing that this file has been created/updated, so it doesn't show.我读到它与 Android 文件系统有关,没有意识到这个文件已经被创建/更新,所以它没有显示。

3) Make sure you have write permissions on externalSD via PlayerSettings 3) 确保您通过 PlayerSettings 拥有对 externalSD 的写入权限

Here is my solution for a StreamWriter on Android:这是我在 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");
    }

Try if that, in combination with all of the tips above, helps you to reach your goal and let me know.尝试一下,结合上述所有提示,是否可以帮助您实现目标并告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM