-1

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();          
    }  
}
Ehsan Mohammadi
  • 1,168
  • 1
  • 15
  • 21
Nicole
  • 1
  • 3
  • 2
    What is the problem you're having? You can write to the Application.PersistentDataPath directory with no trouble. – Retired Ninja Oct 06 '18 at 01:05
  • We cannot find the .txt file on the internal storage of the android device – Nicole Oct 06 '18 at 01:55
  • This may help you if you're having trouble seeing the file when connected via usb. https://stackoverflow.com/questions/40389108/unity-android-created-csv-file-visible-on-device-not-on-pc-via-usb Short answer, restart the device. Longer answer, write a plugin to update the mediastore. https://stackoverflow.com/questions/3300137/how-can-i-refresh-mediastore-on-android – Retired Ninja Oct 06 '18 at 02:08
  • Thank you we will try it – Nicole Oct 06 '18 at 02:35

1 Answers1

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!

Ehsan Mohammadi
  • 1,168
  • 1
  • 15
  • 21
Technivorous
  • 1,682
  • 2
  • 16
  • 22
  • Thank you we will try it – Nicole Oct 06 '18 at 02:36
  • Errors: DirectoryNotFoundException: Could not find a part of the path "C:\Users\user\Documents\VR\Application.PersistentDataPath\PlacesLog.txt – Nicole Oct 06 '18 at 03:38
  • @Nicole Change first line of `Main()` of the answer to this: `StreamWriter writer = new StreamWriter(Application.PersistentDataPath + "/droidlog.txt");` – Ehsan Mohammadi Oct 06 '18 at 15:23
  • @Ehsan thank you! Will try it and give you an update – Nicole Oct 06 '18 at 15:33
  • @Ehsan we solved the persistent data path error but we cannot find the file on android – Nicole Oct 07 '18 at 03:18
  • 1
    @Ehsan Where is the persistent data path on my Android phone? Code: StreamWriter writer = new   StreamWriter(Application.persistentDataPath +"/PlacesLog.txt"); Now where is this "PlacesLog.txt" file on my android phone? I can't find the file. Path: -Windows: Application.persistentDataPath points to %userprofile%\AppData\Local\Packages\VR -Android: Application.persistentDataPath points to /storage/emulated/0/Android/data/com.desktop.VR/files – Nicole Oct 07 '18 at 03:18
  • @Nicole if you install the game in internal storage, the path is `/Data/Data/com../files/PlaceLog.txt` and if you install the game in SD card, the path is `/storage/sdcard0/Android/data/com../files/PlaceLog.txt` – Ehsan Mohammadi Oct 07 '18 at 06:52
  • @Nicole No problem :) – Ehsan Mohammadi Oct 08 '18 at 08:11