123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System.Text;
- using UnityEngine;
- public class CameraPosition : MonoBehaviour
- {
- public Camera movingCamera;
- public string cameraLogFileName = "";
-
- // Get from LoggingManager
- private int ParticipantID;
- private string cameraLogPathFolder = "";
-
- private string cameraLogPath;
- private float time = 0f;
- private string[] buffer;
- private int index = 0;
- private void Start()
- {
- ParticipantID = gameObject.GetComponentInParent<LoggingManager>().ParticipantID;
- cameraLogPathFolder = gameObject.GetComponentInParent<LoggingManager>().LogPathFolder;
- cameraLogFileName = "log_" + ParticipantID + "_" + cameraLogFileName + ".csv";
- cameraLogPath = Path.Combine(cameraLogPathFolder, cameraLogFileName);
- using (FileStream stream = File.Open(cameraLogPath, FileMode.Create))
- {
- using (StreamWriter writer = new StreamWriter(stream))
- {
- writer.WriteLine(GetLogFileHeader());
- writer.Flush();
- }
- }
- Debug.Log("Created new Logfile " + cameraLogFileName);
- buffer = new string[1000];
- }
- private string GetLogFileHeader()
- {
- StringBuilder header = new StringBuilder("ParticipantID");
- header.Append(";Time");
- header.Append(";Position");
- header.Append(";Rotation");
- return header.ToString();
- }
- private void FixedUpdate()
- {
- if (movingCamera.enabled)
- {
- StringBuilder line = new StringBuilder();
- line.Append(ParticipantID);
- line.Append(";" + time);
- line.Append(";" + movingCamera.transform.position.ToString());
- line.Append(";" + movingCamera.transform.rotation.ToString());
- buffer[index++] = line.ToString();
- if (index > buffer.Length - 1)
- writeFromBuffer();
- }
- time += Time.deltaTime;
- }
- private void writeFromBuffer()
- {
- if (File.Exists(cameraLogPath))
- {
- try
- {
- using (StreamWriter writer = new StreamWriter(cameraLogPath, true))
- {
- for (int i = 0; i < buffer.Length; i++)
- {
- if (buffer[i] != null)
- {
- writer.WriteLine(buffer[i]);
- writer.Flush();
- }
- }
- buffer = new string[1000];
- index = 0;
- }
- }
- catch (Exception e)
- {
- throw new ApplicationException("Something went wrong by writing into a csv file: ", e);
- }
- }
- }
- private void OnApplicationQuit()
- {
- writeFromBuffer();
- }
- }
|