UITimePeriodSelection.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. public class UITimePeriodSelection : MonoBehaviour
  7. {
  8. // Inspector variables
  9. [Header("Set Checkbox Objects")]
  10. public Toggle day1;
  11. public Toggle day2;
  12. public Toggle day3;
  13. [Header("Set File Name")]
  14. public string logFileName = "";
  15. // Get from LoggingManager
  16. private int ParticipantID;
  17. private string logPathFolder = "";
  18. // Changes on click
  19. private bool allOn = false;
  20. private int consideredDay = 0;
  21. private float timerDay1 = 0f;
  22. private float timerDay2 = 0f;
  23. private float timerDay3 = 0f;
  24. private string logPath;
  25. private float time = 0f;
  26. private void Start()
  27. {
  28. ParticipantID = gameObject.GetComponentInParent<LoggingManager>().ParticipantID;
  29. logPathFolder = gameObject.GetComponentInParent<LoggingManager>().LogPathFolder;
  30. logFileName = "log_" + logFileName + ".csv";
  31. logPath = Path.Combine(logPathFolder, logFileName);
  32. using (FileStream stream = File.Open(logPath, FileMode.Create))
  33. {
  34. using (StreamWriter writer = new StreamWriter(stream))
  35. {
  36. writer.WriteLine(GetLogFileHeader());
  37. writer.Flush();
  38. }
  39. }
  40. Debug.Log("Created new Logfile " + logFileName);
  41. allOn = !day1.isOn ? false : (!day2.isOn ? false : (!day3.isOn ? false : true));
  42. }
  43. private string GetLogFileHeader()
  44. {
  45. StringBuilder header = new StringBuilder("ParticipantID");
  46. header.Append(";Time");
  47. header.Append(";Considered Day");
  48. header.Append(";Acc Time Day 1");
  49. header.Append(";Acc Time Day 2");
  50. header.Append(";Acc Time Day 3");
  51. return header.ToString();
  52. }
  53. private void FixedUpdate()
  54. {
  55. if (File.Exists(logPath))
  56. {
  57. try
  58. {
  59. using (StreamWriter writer = new StreamWriter(logPath, true))
  60. {
  61. StringBuilder line = new StringBuilder();
  62. line.Append(ParticipantID);
  63. line.Append(";" + time);
  64. line.Append(";" + (allOn ? 123 : (day1.isOn ? 1 : (day2.isOn ? 2 : (day3.isOn ? 3 : 0)))));
  65. line.Append(";" + timerDay1);
  66. line.Append(";" + timerDay2);
  67. line.Append(";" + timerDay3);
  68. writer.WriteLine(line);
  69. writer.Flush();
  70. }
  71. }
  72. catch (Exception e)
  73. {
  74. throw new ApplicationException("Something went wrong by writing into a csv file: ", e);
  75. }
  76. }
  77. time += Time.deltaTime;
  78. if (day1.isOn)
  79. timerDay1 += Time.deltaTime;
  80. if (day2.isOn)
  81. timerDay2 += Time.deltaTime;
  82. if (day3.isOn)
  83. timerDay3 += Time.deltaTime;
  84. }
  85. }