UIPlayback.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System;
  2. using System.IO;
  3. using System.Reflection;
  4. using System.Text;
  5. using UnityEngine;
  6. public class UIPlayback : MonoBehaviour
  7. {
  8. // Inspector variables
  9. public string logFileName = "";
  10. // Get from LoggingManager
  11. private int ParticipantID;
  12. private string logPathFolder = "";
  13. // Changes on click
  14. private int current = 0; // -1 == Rewind; 0 == Pause; 1 == Play
  15. private int countClicksRewind = 0;
  16. private int countClicksPause = 0;
  17. private int countClicksPlay = 0;
  18. private float timerRewind = 0f;
  19. private float timerPause = 0f;
  20. private float timerPlay = 0f;
  21. private string logPath;
  22. private float time = 0f;
  23. private string[] buffer;
  24. private int index = 0;
  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. buffer = new string[1000];
  41. }
  42. private string GetLogFileHeader()
  43. {
  44. StringBuilder header = new StringBuilder("ParticipantID");
  45. header.Append(";Time");
  46. header.Append(";Acc Clicks Rewind");
  47. header.Append(";Acc Clicks Pause");
  48. header.Append(";Acc Clicks Play");
  49. header.Append(";Acc Time Rewind");
  50. header.Append(";Acc Time Pause");
  51. header.Append(";Acc Time Play");
  52. header.Append(";Current");
  53. return header.ToString();
  54. }
  55. public void registerOnRewindClicked()
  56. {
  57. countClicksRewind++;
  58. current = -1;
  59. }
  60. public void registerOnPauseClicked()
  61. {
  62. countClicksPause++;
  63. current = 0;
  64. }
  65. public void registerOnPlayClicked()
  66. {
  67. countClicksPlay++;
  68. current = 1;
  69. }
  70. private void FixedUpdate()
  71. {
  72. StringBuilder line = new StringBuilder();
  73. line.Append(ParticipantID);
  74. line.Append(";" + time);
  75. line.Append(";" + countClicksRewind);
  76. line.Append(";" + countClicksPause);
  77. line.Append(";" + countClicksPlay);
  78. line.Append(";" + timerRewind);
  79. line.Append(";" + timerPause);
  80. line.Append(";" + timerPlay);
  81. line.Append(";" + current);
  82. buffer[index++] = line.ToString();
  83. if (index > buffer.Length - 1)
  84. writeFromBuffer();
  85. time += Time.deltaTime;
  86. if (current == -1)
  87. timerRewind += Time.deltaTime;
  88. else if(current == 0)
  89. timerPause += Time.deltaTime;
  90. else if (current == 1)
  91. timerPlay += Time.deltaTime;
  92. }
  93. private void writeFromBuffer()
  94. {
  95. if (File.Exists(logPath))
  96. {
  97. try
  98. {
  99. using (StreamWriter writer = new StreamWriter(logPath, true))
  100. {
  101. for (int i = 0; i < buffer.Length; i++)
  102. {
  103. if (buffer[i] != null)
  104. {
  105. writer.WriteLine(buffer[i]);
  106. writer.Flush();
  107. }
  108. }
  109. buffer = new string[1000];
  110. index = 0;
  111. }
  112. }
  113. catch (Exception e)
  114. {
  115. throw new ApplicationException("Something went wrong by writing into a csv file: ", e);
  116. }
  117. }
  118. }
  119. private void OnApplicationQuit()
  120. {
  121. writeFromBuffer();
  122. }
  123. }