eye-therapy-2 / Assets / Scripts / Testing / DataCollector.cs
DataCollector.cs
Raw
using System;
using System.IO;

using UnityEngine;

public class DataCollector : MonoBehaviour
{
    public EyeSimulation eyes;
    public CameraEyeRotator cam;

    private const string FILE_PATH = "/Results/Main.csv";
    private StreamWriter file;

    private void Awake()
    {
        file = new StreamWriter(Application.dataPath + FILE_PATH, false);
        file.WriteLine( $"time;eyeLX;eyeLY;eyeRX;eyeRY;camLX;camLY;camRX;camRY");
    }

    private void Update()
    {
        if (Time.time <= 60)
        {
            try
            {
                Vector4 eyeData = eyes.EyeData;
                Vector4 camData = cam.CamData;
                file.WriteLine(
                    $"{Time.time};{eyeData.x};{eyeData.y};{eyeData.z};{eyeData.w};{camData.y};{camData.x};{camData.w};{-camData.z}");
            }
            catch (Exception e)
            {
                Debug.LogError(e);
                throw;
            }
        }
        else Destroy(this);
    }

    private void OnDestroy()
    {
        file.Close();
    }
}