using System; using System.IO; using System.Text; using UnityEngine; public class UIPlayback : MonoBehaviour { // Inspector variables public string logFileName = ""; // Get from LoggingManager private int ParticipantID; private string logPathFolder = ""; // Changes on click private int current = 0; // -1 == Rewind; 0 == Pause; 1 == Play private int countClicksRewind = 0; private int countClicksPause = 0; private int countClicksPlay = 0; private float timerRewind = 0f; private float timerPause = 0f; private float timerPlay = 0f; private string logPath; private float time = 0f; 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); } private string GetLogFileHeader() { StringBuilder header = new StringBuilder("ParticipantID"); header.Append(";Time"); header.Append(";Acc Clicks Rewind"); header.Append(";Acc Clicks Pause"); header.Append(";Acc Clicks Play"); header.Append(";Acc Time Rewind"); header.Append(";Acc Time Pause"); header.Append(";Acc Time Play"); header.Append(";Current"); return header.ToString(); } public void registerOnRewindClicked() { countClicksRewind++; current = -1; } public void registerOnPauseClicked() { countClicksPause++; current = 0; } public void registerOnPlayClicked() { countClicksPlay++; current = 1; } private void FixedUpdate() { if (File.Exists(logPath)) { try { using (StreamWriter writer = new StreamWriter(logPath, true)) { StringBuilder line = new StringBuilder(); line.Append(ParticipantID); line.Append(";" + time); line.Append(";" + countClicksRewind); line.Append(";" + countClicksPause); line.Append(";" + countClicksPlay); line.Append(";" + timerRewind); line.Append(";" + timerPause); line.Append(";" + timerPlay); line.Append(";" + current); writer.WriteLine(line); writer.Flush(); } } catch (Exception e) { throw new ApplicationException("Something went wrong by writing into a csv file: ", e); } } time += Time.deltaTime; if (current == -1) timerRewind += Time.deltaTime; else if(current == 0) timerPause += Time.deltaTime; else if (current == 1) timerPlay += Time.deltaTime; } }