|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Text; |
| 4 | +using System.Collections; |
| 5 | +using System.Collections.Generic; |
| 6 | +using UnityEngine; |
| 7 | +using UnityEditor; |
| 8 | + |
| 9 | +[System.Serializable] |
| 10 | +public class CSVWriter |
| 11 | +{ |
| 12 | + public string fileName = null; |
| 13 | + public string dirName = null; |
| 14 | + public bool writeUnixTime = true; |
| 15 | + public bool append_zero_to_filename = false; |
| 16 | + public List<string> columns; |
| 17 | + |
| 18 | + [SerializeField, ReadOnly] private string filePath; |
| 19 | + private StreamWriter eventWriter; |
| 20 | + private List<string> payload = new List<string>(); |
| 21 | + |
| 22 | + public bool Initialize() { |
| 23 | + string dname = Application.persistentDataPath; |
| 24 | + if (dirName != null && dirName.Length > 0) { |
| 25 | + dname = $"Assets/{dirName}"; |
| 26 | + if (!AssetDatabase.IsValidFolder(dname)) dname = AssetDatabase.GUIDToAssetPath(AssetDatabase.CreateFolder("Assets", dirName)); |
| 27 | + } |
| 28 | + string fname = (fileName != null && fileName.Length > 0) ? fileName : System.DateTime.Now.ToString("HH-mm-ss"); |
| 29 | + filePath = (append_zero_to_filename) ? Path.Combine(dname, fname+"_0.csv") : Path.Combine(dname, fname+".csv"); |
| 30 | + |
| 31 | + int counter = 1; |
| 32 | + while(File.Exists(filePath)) { |
| 33 | + filePath = Path.Combine(dname, fname+$"_{counter}.csv"); |
| 34 | + counter++; |
| 35 | + } |
| 36 | + |
| 37 | + eventWriter = new StreamWriter(new FileStream(filePath, FileMode.Create), Encoding.UTF8); |
| 38 | + // Header Line, if any columns are added to `columns` |
| 39 | + if (columns.Count > 0) { |
| 40 | + if (writeUnixTime) columns.Add("unix_ms"); |
| 41 | + eventWriter.WriteLine(String.Join(',', columns)); |
| 42 | + } |
| 43 | + |
| 44 | + return true; |
| 45 | + } |
| 46 | + |
| 47 | + |
| 48 | + public bool WriteLine() { |
| 49 | + if (payload.Count == 0) return false; |
| 50 | + WriteLine(String.Join(",",payload)); |
| 51 | + payload = new List<string>(); |
| 52 | + return true; |
| 53 | + } |
| 54 | + public bool WriteLine(bool add_unix) { |
| 55 | + if (payload.Count == 0) return false; |
| 56 | + WriteLine(String.Join(",",payload), add_unix); |
| 57 | + payload = new List<string>(); |
| 58 | + return true; |
| 59 | + } |
| 60 | + public bool WriteLine(string newLine) { |
| 61 | + eventWriter.WriteLine(newLine); |
| 62 | + return true; |
| 63 | + } |
| 64 | + public bool WriteLine(string newLine, bool add_unix) { |
| 65 | + if (add_unix) eventWriter.WriteLine($"{GetUnixTime()},{newLine}"); |
| 66 | + else eventWriter.WriteLine(newLine); |
| 67 | + return true; |
| 68 | + } |
| 69 | + |
| 70 | + public void AddPayload(string to_add) { |
| 71 | + payload.Add(to_add); |
| 72 | + } |
| 73 | + public void AddPayload(int to_add) { |
| 74 | + payload.Add(to_add.ToString()); |
| 75 | + } |
| 76 | + public void AddPayload(float to_add) { |
| 77 | + payload.Add(to_add.ToString()); |
| 78 | + } |
| 79 | + public void AddPayload(long to_add) { |
| 80 | + payload.Add($"{to_add}"); |
| 81 | + } |
| 82 | + public void AddPayload(Vector3 to_add) { |
| 83 | + payload.Add(to_add.x.ToString()); |
| 84 | + payload.Add(to_add.y.ToString()); |
| 85 | + payload.Add(to_add.z.ToString()); |
| 86 | + } |
| 87 | + public void AddPayload(Quaternion to_add) { |
| 88 | + AddPayload(to_add.eulerAngles); |
| 89 | + } |
| 90 | + |
| 91 | + public static long GetUnixTime() { |
| 92 | + DateTime currentTime = DateTime.UtcNow; |
| 93 | + return ((DateTimeOffset)currentTime).ToUnixTimeMilliseconds(); |
| 94 | + } |
| 95 | + |
| 96 | + public void Disable() { |
| 97 | + eventWriter.Flush(); |
| 98 | + eventWriter.Close(); |
| 99 | + } |
| 100 | +} |
0 commit comments