Last active
March 14, 2017 21:28
-
-
Save fuzzblob/fec2a197ed7e88c676b008fdadbb1a1a to your computer and use it in GitHub Desktop.
Two classes to make generating report and exporting data for debugging etc easy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CsvHandler | |
{ | |
public static void ExportStringData(string filename, string data, string defaultPath = "Assets/Debug/Log/") | |
{ | |
filename += "_" + MUtil.DateStamp(); | |
string path = defaultPath; | |
#if UNITY_EDITOR | |
path = UnityEditor.EditorUtility.SaveFilePanel("Save Data", defaultPath, filename, "csv"); | |
#else | |
path += filename + ".csv"; | |
#endif | |
MDebug.WriteStream(path, data); | |
} | |
private static string ReadStream(string path) | |
{ | |
using (FileStream fs = new FileStream(path, FileMode.Open)) | |
{ | |
using (StreamReader reader = new StreamReader(fs)) | |
{ | |
return reader.ReadToEnd(); | |
} | |
} | |
} | |
private static void WriteStream(string path, string output) | |
{ | |
if (path == "") | |
{ | |
Debug.LogWarning("Abort CSV export! No path specified."); | |
return; | |
} | |
using (FileStream fs = new FileStream(path, FileMode.Create)) | |
{ | |
using (StreamWriter writer = new StreamWriter(fs)) | |
{ | |
writer.Write(output); | |
} | |
} | |
#if UNITY_EDITOR | |
UnityEditor.AssetDatabase.Refresh(); | |
#endif | |
Debug.Log("CSV export finished."); | |
} | |
} | |
public class CsvData | |
{ | |
private System.Text.StringBuilder _builder = new System.Text.StringBuilder; | |
private void WriteCell(string input) | |
{ | |
_builder.Append(input + "; "); | |
} | |
public int LineCount { get; private set; } | |
public void WriteLine(string[] cells) | |
{ | |
for (int i = 0; i < cells.Length; i++) | |
{ | |
if (i == cells.Length - 1) | |
_builder.AppendLine(cells[i]); | |
else | |
WriteCell(cells[i]); | |
} | |
LineCount++; | |
} | |
public string Export() | |
{ | |
return _builder.ToString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment