using System; using System.IO; using System.Reflection; using System.Text; using UnityEngine; public class UIChangeView : MonoBehaviour { // Inspector variables public string logFileName = ""; // Get from LoggingManager private int ParticipantID; private string logPathFolder = ""; // Changes on click private int countChanges = 0; private bool defaultView = true; // Perspective is default view private float timerPerspective = 0f; private float timerTopDown = 0f; private string logPath; private float time = 0f; private string[] buffer; private int index = 0; private void Start() { ParticipantID = gameObject.GetComponentInParent().ParticipantID; logPathFolder = gameObject.GetComponentInParent().LogPathFolder; logFileName = "log_" + ParticipantID + "_" + logFileName + ".csv"; logPath = Path.Combine(logPathFolder, logFileName); using (FileStream stream = File.Open(logPath, FileMode.Create)) { using (StreamWriter writer = new StreamWriter(stream)) { writer.WriteLine(GetLogFileHeader()); writer.Flush(); } } Debug.Log("Created new Logfile " + logFileName); buffer = new string[1000]; } private string GetLogFileHeader() { StringBuilder header = new StringBuilder("ParticipantID"); header.Append(";Time"); header.Append(";Acc Clicks"); header.Append(";Acc Time Perspective"); header.Append(";Acc Time TopDown"); header.Append(";View"); return header.ToString(); } public void registerOnButtonClicked() { countChanges++; defaultView = !defaultView; } private void FixedUpdate() { StringBuilder line = new StringBuilder(); line.Append(ParticipantID); line.Append(";" + time); line.Append(";" + countChanges); line.Append(";" + timerPerspective); line.Append(";" + timerTopDown); line.Append(";" + (defaultView ? "Perspective" : "TopDown")); buffer[index++] = line.ToString(); if (index > buffer.Length - 1) writeFromBuffer(); time += Time.deltaTime; if (defaultView) timerPerspective += Time.deltaTime; else timerTopDown += Time.deltaTime; } private void writeFromBuffer() { if (File.Exists(logPath)) { try { using (StreamWriter writer = new StreamWriter(logPath, true)) { for (int i = 0; i < buffer.Length; i++) { if (buffer[i] != null) { writer.WriteLine(buffer[i]); writer.Flush(); } } buffer = new string[1000]; index = 0; } } catch (Exception e) { throw new ApplicationException("Something went wrong by writing into a csv file: ", e); } } } private void OnApplicationQuit() { writeFromBuffer(); } }