MousePosition.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using UnityEngine;
  5. public class MousePosition : MonoBehaviour
  6. {
  7. public string logFileName = "";
  8. // Get from LoggingManager
  9. private int ParticipantID;
  10. private string logPathFolder = "";
  11. private string logPath;
  12. private float time = 0f;
  13. private void Start()
  14. {
  15. ParticipantID = gameObject.GetComponentInParent<LoggingManager>().ParticipantID;
  16. logPathFolder = gameObject.GetComponentInParent<LoggingManager>().LogPathFolder;
  17. logFileName = "log_" + logFileName + ".csv";
  18. logPath = Path.Combine(logPathFolder, logFileName);
  19. using (FileStream stream = File.Open(logPath, FileMode.Create))
  20. {
  21. using (StreamWriter writer = new StreamWriter(stream))
  22. {
  23. writer.WriteLine(GetLogFileHeader());
  24. writer.Flush();
  25. }
  26. }
  27. Debug.Log("Created new Logfile " + logFileName);
  28. }
  29. private string GetLogFileHeader()
  30. {
  31. StringBuilder header = new StringBuilder("ParticipantID");
  32. header.Append(";Time");
  33. header.Append(";Position");
  34. return header.ToString();
  35. }
  36. private void FixedUpdate()
  37. {
  38. if (File.Exists(logPath))
  39. {
  40. try
  41. {
  42. using (StreamWriter writer = new StreamWriter(logPath, true))
  43. {
  44. StringBuilder line = new StringBuilder();
  45. line.Append(ParticipantID);
  46. line.Append(";" + time);
  47. line.Append(";" + Input.mousePosition.ToString());
  48. writer.WriteLine(line);
  49. writer.Flush();
  50. }
  51. }
  52. catch (Exception e)
  53. {
  54. throw new ApplicationException("Something went wrong by writing into a csv file: ", e);
  55. }
  56. }
  57. time += Time.deltaTime;
  58. }
  59. }