Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
210 views
in Technique[技术] by (71.8m points)

c# - Writing .txt file in 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!!

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();          
    }  
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...