Saving streamed Data from Unity in a csv File
Hi, I have to record the coordinate-data (vector of x,y,z) of an eye-tracking System and save it, for later evaluation. The whole eye-tracking System is integrated inside a Head-mounted-Display and the software runs over Unity. After some research, I figured out that saving the Data in a CSV file would probably the easiest way. This is what I got so far:
void Start()
{
smiInstance = SMI.SMIEyeTrackingUnity.Instance;
if (File.Exists(FilePath))
File.Delete(FilePath);
filePath = filePath + DateTime.Now.ToString("yyyy-mm-dd-hh-mm-ss") + ".txt";
update();
}
void update()
{
Vector3 leftGazeDirection = smiInstance.smi_GetLeftGazeDirection();
Vector3 rightGazeDirection = smiInstance.smi_GetRightGazeDirection();
float[] output = new float[]{
leftGazeDirection.x,
leftGazeDirection.y,
leftGazeDirection.z,
rightGazeDirection.x,
rightGazeDirection.y,
rightGazeDirection.z};
int length = output.Length;
StringBuilder sb = new StringBuilder();
for (int index = 0; index < length; index++)
sb.AppendLine(string.Join(delimiter, output[index]));
if (!File.Exists(filePath))
File.WriteAllText(filePath, sb.ToString());
else
File.AppendAllText(filePath, sb.ToString()); }
}
The Result of this is a CSV file with the Vector of the latest Position of the Gazedirection. What I need would be a Record of all the Gazedirections that were made in one Session. Is it possible to get something like this? Can I somehow modify my Code to achieve this or should I try something completely different? Since I'm very newbie to unity and programming in general I might just have a lack of vocabulary and don't know what to search for to solve my problem.. I would be very thankful if somebody could help me. :)
hello, can you delete the
if (!File.Exists(filePath))
File.WriteAllText(filePath, sb.ToString());
else
and check if it works? the AppendAllText method already creates the file if it doesnt exist. that should not override your previous files, Documentation
Hey, the code still works. But I just get the latest Information back.
Answer by xxmariofer · Feb 14, 2019 at 02:29 PM
i think your problem is easier than all of that, just change
update()
to
Update()
and there is not need for you to call it from the start since unity will call it per frame. right now you are only calling it once, and you are deleting the file in the start just before that.
Haha yeah, that actually was the problem.. Thank you very much!
you are welcome, i will convert it to an answer :)
Your answer
Follow this Question
Related Questions
JSON string to list with Simple JSON 0 Answers
I want to import CFD data. 1 Answer
How do I modify animation parameters from script (C#) 1 Answer
Performance question: Is it better to Instantiate particle systems OR use same particle system? 1 Answer
Changing between Prone, Crouch and Stand with one key. 2 Answers