UIChangeView.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System;
  2. using System.IO;
  3. using System.Reflection;
  4. using System.Text;
  5. using UnityEngine;
  6. public class UIChangeView : 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 countChanges = 0;
  15. private bool defaultView = true; // Perspective is default view
  16. private float timerPerspective = 0f;
  17. private float timerTopDown = 0f;
  18. private string logPath;
  19. private float time = 0f;
  20. private string[] buffer;
  21. private int index = 0;
  22. private void Start()
  23. {
  24. ParticipantID = gameObject.GetComponentInParent<LoggingManager>().ParticipantID;
  25. logPathFolder = gameObject.GetComponentInParent<LoggingManager>().LogPathFolder;
  26. logFileName = "log_" + ParticipantID + "_" + logFileName + ".csv";
  27. logPath = Path.Combine(logPathFolder, logFileName);
  28. using (FileStream stream = File.Open(logPath, FileMode.Create))
  29. {
  30. using (StreamWriter writer = new StreamWriter(stream))
  31. {
  32. writer.WriteLine(GetLogFileHeader());
  33. writer.Flush();
  34. }
  35. }
  36. Debug.Log("Created new Logfile " + logFileName);
  37. buffer = new string[1000];
  38. }
  39. private string GetLogFileHeader()
  40. {
  41. StringBuilder header = new StringBuilder("ParticipantID");
  42. header.Append(";Time");
  43. header.Append(";Acc Clicks");
  44. header.Append(";Acc Time Perspective");
  45. header.Append(";Acc Time TopDown");
  46. header.Append(";View");
  47. return header.ToString();
  48. }
  49. public void registerOnButtonClicked()
  50. {
  51. countChanges++;
  52. defaultView = !defaultView;
  53. }
  54. private void FixedUpdate()
  55. {
  56. StringBuilder line = new StringBuilder();
  57. line.Append(ParticipantID);
  58. line.Append(";" + time);
  59. line.Append(";" + countChanges);
  60. line.Append(";" + timerPerspective);
  61. line.Append(";" + timerTopDown);
  62. line.Append(";" + (defaultView ? "Perspective" : "TopDown"));
  63. buffer[index++] = line.ToString();
  64. if (index > buffer.Length - 1)
  65. writeFromBuffer();
  66. time += Time.deltaTime;
  67. if (defaultView)
  68. timerPerspective += Time.deltaTime;
  69. else
  70. timerTopDown += Time.deltaTime;
  71. }
  72. private void writeFromBuffer()
  73. {
  74. if (File.Exists(logPath))
  75. {
  76. try
  77. {
  78. using (StreamWriter writer = new StreamWriter(logPath, true))
  79. {
  80. for (int i = 0; i < buffer.Length; i++)
  81. {
  82. if (buffer[i] != null)
  83. {
  84. writer.WriteLine(buffer[i]);
  85. writer.Flush();
  86. }
  87. }
  88. buffer = new string[1000];
  89. index = 0;
  90. }
  91. }
  92. catch (Exception e)
  93. {
  94. throw new ApplicationException("Something went wrong by writing into a csv file: ", e);
  95. }
  96. }
  97. }
  98. private void OnApplicationQuit()
  99. {
  100. writeFromBuffer();
  101. }
  102. }