using System; using System.IO; using System.Text; using UnityEngine; public class MousePosition : MonoBehaviour { public string logFileName = ""; // Get from LoggingManager private int ParticipantID; private string logPathFolder = ""; private string logPath; private float time = 0f; private void Start() { ParticipantID = gameObject.GetComponentInParent().ParticipantID; logPathFolder = gameObject.GetComponentInParent().LogPathFolder; logFileName = "log_" + 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(";Position"); return header.ToString(); } 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(";" + Input.mousePosition.ToString()); writer.WriteLine(line); writer.Flush(); } } catch (Exception e) { throw new ApplicationException("Something went wrong by writing into a csv file: ", e); } } time += Time.deltaTime; } }