UIChangeView.cs 2.7 KB

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