【问题标题】:Writing .txt file in android在android中编写.txt文件
【发布时间】:2018-10-06 00:57:08
【问题描述】:

我需要帮助解决我们系统中的问题。我们正在使用 Unity 和 Visual Studio C# 来创建仅使用凝视控件(无控制器)的移动 VR 游戏。我们需要找到一种将调试日志写入文本文件并保存到 android 内部存储的方法。提前感谢您的帮助!

下面是我们的代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using UnityEngine;

 public class VRPlace : MonoBehaviour 
 {
    ...

    void OnTriggerEnter(Collider other)
    {
        string path = "Assets/Resources/PlacesLog.txt";
        StreamWriter testing = new StreamWriter(path, true);

        if (other.gameObject.name == "Hospital")
        {
            GameObject otherObj = other.gameObject;
            Debug.Log("Triggered to: " + otherObj);
        }                
        testing.WriteLine(other.gameObject.name);                 
        testing.Close();          
    }  
}

【问题讨论】:

  • 您遇到了什么问题?您可以毫无问题地写入 Application.PersistentDataPath 目录。
  • 我们在安卓设备的内部存储中找不到.txt文件
  • 如果您在通过 USB 连接时无法查看文件,这可能会对您有所帮助。 stackoverflow.com/questions/40389108/… 简答,重启设备。更长的答案,编写一个插件来更新媒体存储。 stackoverflow.com/questions/3300137/…
  • 谢谢,我们会试试的

标签: c# android unity3d google-vr


【解决方案1】:

这里是使用 StreamWriter 保存 .txt 文件的示例。

class FileSaver
{
    static void Main()
    {
        // Create a StreamWriter instance
        StreamWriter writer = new 
        StreamWriter(Application.PersistentDataPath + "/droidlog.txt");

        // This using statement will ensure the writer will be closed when no longer used   
        using(writer)   
        {
            // Loop through the numbers from 1 to 20 and write them
            for (int i = 1; i <= 20; i++)
            {
                writer.WriteLine(i);
            }
        }
    }
}

这保存了数字 1-20,你会想要填补空白......祝你好运!

【讨论】:

  • 谢谢,我们会试试的
  • 错误:DirectoryNotFoundException:找不到路径的一部分“C:\Users\user\Documents\VR\Application.PersistentDataPath\PlacesLog.txt
  • @Nicole 更改答案Main() 的第一行:StreamWriter writer = new StreamWriter(Application.PersistentDataPath + "/droidlog.txt");
  • @Ehsan 谢谢!会尝试并给你更新
  • @Ehsan 我的 Android 手机上的持久数据路径在哪里?代码:StreamWriter writer = new StreamWriter(Application.persistentDataPath +"/PlacesLog.txt");现在我的安卓手机上的“PlacesLog.txt”文件在哪里?我找不到文件。路径:-Windows:Application.persistentDataPath 指向 %userprofile%\AppData\Local\Packages\VR -Android:Application.persistentDataPath 指向 /storage/emulated/0/Android/data/com.desktop.VR/files
猜你喜欢
  • 2013-08-12
  • 1970-01-01
  • 2012-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-28
相关资源
最近更新 更多