UITimePeriodSelection.cs 3.6 KB

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