Writing .txt file in android

醉酒当歌 提交于 2019-12-13 07:51:36

问题


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!!

That is our code below

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

回答1:


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

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



来源:https://stackoverflow.com/questions/52674819/writing-txt-file-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!