CameraPosition.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Text;
  6. using UnityEngine;
  7. [RequireComponent(typeof(LoggingManager))]
  8. public class CameraPosition : MonoBehaviour
  9. {
  10. public Camera movingCamera;
  11. public string cameraLogFileName = "";
  12. // Get from LoggingManager
  13. private int ParticipantID;
  14. private string cameraLogPathFolder = "";
  15. private string cameraLogPath;
  16. private float time = 0f;
  17. private void Start()
  18. {
  19. ParticipantID = gameObject.GetComponentInParent<LoggingManager>().ParticipantID;
  20. cameraLogPathFolder = gameObject.GetComponentInParent<LoggingManager>().LogPathFolder;
  21. cameraLogFileName = "log_" + cameraLogFileName + ".csv";
  22. cameraLogPath = Path.Combine(cameraLogPathFolder, cameraLogFileName);
  23. using (FileStream stream = File.Open(cameraLogPath, FileMode.Create))
  24. {
  25. using (StreamWriter writer = new StreamWriter(stream))
  26. {
  27. writer.WriteLine(GetLogFileHeader());
  28. writer.Flush();
  29. }
  30. }
  31. Debug.Log("Created new Logfile " + cameraLogFileName);
  32. }
  33. private string GetLogFileHeader()
  34. {
  35. StringBuilder header = new StringBuilder("ParticipantID");
  36. header.Append(";Time");
  37. header.Append(";Position");
  38. header.Append(";Rotation");
  39. header.Append(";FOV");
  40. return header.ToString();
  41. }
  42. private void FixedUpdate()
  43. {
  44. if (movingCamera.enabled)
  45. {
  46. if (File.Exists(cameraLogPath))
  47. {
  48. try
  49. {
  50. using (StreamWriter writer = new StreamWriter(cameraLogPath, true))
  51. {
  52. StringBuilder line = new StringBuilder();
  53. line.Append(ParticipantID);
  54. line.Append(";" + time);
  55. line.Append(";" + movingCamera.transform.position.ToString());
  56. line.Append(";" + movingCamera.transform.rotation.ToString());
  57. line.Append(";" + movingCamera.fieldOfView);
  58. writer.WriteLine(line);
  59. writer.Flush();
  60. }
  61. }
  62. catch (Exception e)
  63. {
  64. throw new ApplicationException("Something went wrong by writing into a csv file: ", e);
  65. }
  66. }
  67. }
  68. time += Time.deltaTime;
  69. }
  70. }