CameraPosition.cs 2.5 KB

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