Ironlors
Ironlors

Reputation: 193

Unity export create new .txt files

I'm currently working on a thesis for high school in Germany. My topic is to figure out how we can make traffic intersections more efficient through automation. For this, I'm building a small simulation in Unity3D and right now I'm working on the script and settings for the car. In order to compare multiple settings, I would like to export some data in order to analyze it with NumPy and Matplotlib. But to do so I have to export the data out of Unity.


Right now the car is driving through some waypoints and I would like to get the time when the car is reaching those points. So now I only have to save them outside of Unity in a format so I could easily import it inside of my jupyter notebook in order to analyze it. So how would I do this? I know this might be a simple question but most guides are for game objects or characters. So they've saved it for a different purpose.


Update:

Thanks for the help. I got it fixed and now it is writing into a txt file. Code

Is there a possibility that Unity is creating a new txt file every single time when I run the script. I'm not quite sure if this is even possible at all since I'm technical starting the whole system again and there is no information from last time. As an example that now it's called data1.txt and the next time I'm starting the code it is now saving it into data2.txt.

This is the current output just for some reference. Data.txt

void FixedUpdate()
{
    speed = Engine.currentSpeed;
    time = Time.time;
    node = Engine.node;
    xPos = Auto.transform.position.x;
    zPos = Auto.transform.position.z;
    if (IsRunning == true)
    {
        StartCoroutine(Wait());
    }


}

public IEnumerator Wait()
{
    IsRunning = false;
    yield return new WaitForSeconds(WaitTimer);
    Output();
    IsRunning = true;
}

void Output()
{
    print("[" + motorTorque + "," + maxSpeed + "," + steerAngle + "," + node + "," + time + "," + speed + "," + xPos + "," + zPos + "]");

    StreamWriter writer = File.AppendText(@"C:\Users\kaykl\Documents\Schule\Info\Data\CarControle\data.txt");
    writer.WriteLine("[" + motorTorque + "," + maxSpeed + "," + steerAngle + "," + node + "," + time + "," + speed + "," + xPos + "," + zPos + "]");
    writer.Close();
    if (IsRunning == true)
    {
        StartCoroutine(Wait());
    }
    if (node == 0 && cancel == false)
    {
        cancel = true;
    }
    else if (node == 0 && cancel == true)
    {
        writer.Close();
    }

}

Upvotes: 0

Views: 3151

Answers (1)

Helder Sepulveda
Helder Sepulveda

Reputation: 17594

An easy way will be save what you need to a file:
https://forum.unity3d.com/threads/how-to-write-a-file.8864/
And for coding a different file every time, just include a time-stamp in the filename.

And alternative will be to use Unity Analytics:
https://unity3d.com/unity/features/analytics
It comes with some nice graphs that could be useful when presenting your thesis.

Good luck!

Upvotes: 1

Related Questions