在Android中编写.txt文件 [英] Writing .txt file in android

查看:179
本文介绍了在Android中编写.txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要我们系统中的问题的帮助.我们正在使用unity和visual studio C#来创建仅使用注视控件(无控制器)的移动VR游戏.我们需要找到一种将调试日志写入文本文件并保存到android内部存储中的方法.预先感谢您的帮助!

I need help with a problem in our system. We are using unity and visual studio C# to create a mobile VR game using gaze controls only (no controller). We need to find a way to write debug logs into a text file and save to android internal storage. Thanks in advance for the help!!

这是下面的代码

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

推荐答案

以下是使用StreamWriter保存.txt文件的示例.

here is a sample for saving a .txt file with StreamWriter.

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,您将需要填写空白...祝您好运!

this saves the numbers 1-20, you will want to fill in the blanks... good luck!

这篇关于在Android中编写.txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆