using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Text; using UnityEngine; [RequireComponent(typeof(LoggingManager))] 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 void Start() { ParticipantID = gameObject.GetComponentInParent().ParticipantID; cameraLogPathFolder = gameObject.GetComponentInParent().LogPathFolder; cameraLogFileName = "log_" + 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); } private string GetLogFileHeader() { StringBuilder header = new StringBuilder("ParticipantID"); header.Append(";Time"); header.Append(";Position"); header.Append(";Rotation"); header.Append(";FOV"); return header.ToString(); } private void FixedUpdate() { if (movingCamera.enabled) { if (File.Exists(cameraLogPath)) { try { using (StreamWriter writer = new StreamWriter(cameraLogPath, true)) { StringBuilder line = new StringBuilder(); line.Append(ParticipantID); line.Append(";" + time); line.Append(";" + movingCamera.transform.position.ToString()); line.Append(";" + movingCamera.transform.rotation.ToString()); line.Append(";" + movingCamera.fieldOfView); writer.WriteLine(line); writer.Flush(); } } catch (Exception e) { throw new ApplicationException("Something went wrong by writing into a csv file: ", e); } } } time += Time.deltaTime; } }