using System; using System.IO; using System.Text; using UnityEngine; using UnityEngine.UI; public class UITimePeriodSelection : MonoBehaviour { // Inspector variables [Header("Set Checkbox Objects")] public Toggle day1; public Toggle day2; public Toggle day3; [Header("Set File Name")] public string logFileName = ""; // Get from LoggingManager private int ParticipantID; private string logPathFolder = ""; // Changes on click private bool allOn = false; private int consideredDay = 0; private float timerDay1 = 0f; private float timerDay2 = 0f; private float timerDay3 = 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); allOn = !day1.isOn ? false : (!day2.isOn ? false : (!day3.isOn ? false : true)); } private string GetLogFileHeader() { StringBuilder header = new StringBuilder("ParticipantID"); header.Append(";Time"); header.Append(";Considered Day"); header.Append(";Acc Time Day 1"); header.Append(";Acc Time Day 2"); header.Append(";Acc Time Day 3"); 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(";" + (allOn ? 123 : (day1.isOn ? 1 : (day2.isOn ? 2 : (day3.isOn ? 3 : 0))))); line.Append(";" + timerDay1); line.Append(";" + timerDay2); line.Append(";" + timerDay3); 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 (day1.isOn) timerDay1 += Time.deltaTime; if (day2.isOn) timerDay2 += Time.deltaTime; if (day3.isOn) timerDay3 += Time.deltaTime; } }