UITimePeriodSelection.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 float timerDay1 = 0f;
  21. private float timerDay2 = 0f;
  22. private float timerDay3 = 0f;
  23. private string logPath;
  24. private float time = 0f;
  25. private void Start()
  26. {
  27. ParticipantID = gameObject.GetComponentInParent<LoggingManager>().ParticipantID;
  28. logPathFolder = gameObject.GetComponentInParent<LoggingManager>().LogPathFolder;
  29. logFileName = "log_" + ParticipantID + "_" + logFileName + ".csv";
  30. logPath = Path.Combine(logPathFolder, logFileName);
  31. using (FileStream stream = File.Open(logPath, FileMode.Create))
  32. {
  33. using (StreamWriter writer = new StreamWriter(stream))
  34. {
  35. writer.WriteLine(GetLogFileHeader());
  36. writer.Flush();
  37. }
  38. }
  39. Debug.Log("Created new Logfile " + logFileName);
  40. allOn = !day1.isOn ? false : (!day2.isOn ? false : (!day3.isOn ? false : true));
  41. }
  42. private string GetLogFileHeader()
  43. {
  44. StringBuilder header = new StringBuilder("ParticipantID");
  45. header.Append(";Time");
  46. header.Append(";Considered Day");
  47. header.Append(";Acc Time Day 1");
  48. header.Append(";Acc Time Day 2");
  49. header.Append(";Acc Time Day 3");
  50. return header.ToString();
  51. }
  52. private void FixedUpdate()
  53. {
  54. if (File.Exists(logPath))
  55. {
  56. try
  57. {
  58. using (StreamWriter writer = new StreamWriter(logPath, true))
  59. {
  60. StringBuilder line = new StringBuilder();
  61. line.Append(ParticipantID);
  62. line.Append(";" + time);
  63. line.Append(";" + (allOn ? 123 : (day1.isOn ? 1 : (day2.isOn ? 2 : (day3.isOn ? 3 : 0)))));
  64. line.Append(";" + timerDay1);
  65. line.Append(";" + timerDay2);
  66. line.Append(";" + timerDay3);
  67. writer.WriteLine(line);
  68. writer.Flush();
  69. }
  70. }
  71. catch (Exception e)
  72. {
  73. throw new ApplicationException("Something went wrong by writing into a csv file: ", e);
  74. }
  75. }
  76. time += Time.deltaTime;
  77. if (day1.isOn)
  78. timerDay1 += Time.deltaTime;
  79. if (day2.isOn)
  80. timerDay2 += Time.deltaTime;
  81. if (day3.isOn)
  82. timerDay3 += Time.deltaTime;
  83. }
  84. }