In Unity3d, How can I read the position data from .txt file continuously?

In Unity3D, I want to load position data from text file. Here is an example of the text file. These are simulation results. The simulation program generate 30 data per a second. So, I want to visualize result in realtime. The contents are x, y, z position.

data_01.txt

1 -5 -10

data_ 02.txt

2 2 5

data_03.txt

3 2 4

All files consist of 1 line.

I’d like to input these data into a object.

I want to load text file in 30 text files per 1 second.

I write code that read first text file. But, I need some advice about reading text file continuously. I want to read 1 text file per frame. I know the socket communication is required. But, my coding skill is insufficient. After I implement file loading approach, I will try communication.
Thanks.


using UnityEngine;
using System.Collections;
using System;
using System.IO;

public class parsing : MonoBehaviour {

public string fileName = “Data_01.txt”

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

if (!File.Exists(fileName))
{
Debug.Log(fileName + “does not exist.”);
return;
}

using (StreamReader sr = File.OpenText(fileName))
{
String fileData;
fileData = sr.ReadToEnd();
sr.Close();

Debug.Log("Data Read: " + fileData);

}

}
}

Use code tags:

I’m assuming this is in relation to this:

And you say here:

How do you know that? Is it because I mentioned sockets in the last thread? Because I didn’t say you need one, I said depending the API of the program you’re integrating with you may need one.

Note, you also never answered my questions in the last thread.

Also, looking at your code, it appears you’d just have to increment your file name by 1 each frame…

using UnityEngine;
using System.Collections;
using System;
using System.IO;

public class parsing : MonoBehaviour {

    //a format string for the file name
    public string fileFormat = "Data_{0:00}.txt"

    private int currentIndex;
    private string fileName;

    // Use this for initialization
    void Start () {
        currentIndex = 1;
        fileName = string.Format(fileFormat, currentIndex);
    }

    // Update is called once per frame
    void Update ()
    {
        if (!File.Exists(fileName))
        {
            Debug.Log(fileName + "does not exist.");
            return;
        }

        using (StreamReader sr = File.OpenText(fileName))
        {
            String fileData;
            fileData = sr.ReadToEnd();
            sr.Close();

            Debug.Log("Data Read: " + fileData);

            //prep file name for next update
            currentIndex++;
            fileName = string.Format(fileFormat, currentIndex);
        }

    }
}

This sounds like a really horrible interface though… is there no better way to integrate with the software in question? What is the software? Is there not an API for it?

3 Likes

API is provided. But I have to complete it short time, So I want to work by using text file…
Also, I have low coding skill
Thank you for answer.

It’s been a week since your last post… so it must not be that short of a time…

I’m just saying, this sounds like a really long way around route to get real time data from this program. Especially at a rate that looks animated. Which honestly… can really put the hurt on the hard disk these files are getting written to.

Like for instance, you’re naming standard suggests that it supports up to 2 digits on the end. What happens after you get to Data_99.txt? Because that’s only 3 seconds of animation… does it keep growing after that? If so, why the 00 format? Does it reset to 1 ever X numbers? Well that’s going to beat the heck out of the hard disk even more. Do these files get cleaned up at any point? Cause you’re chewing up storage space with every frame of animation.

Is there something tough about the API that you don’t want to use it?

Also did my previous post with the supplied code example get you moving forward?

I want to use API, But my coding skill is low. There are not other reason.
In case of file name, Data_1.txt, Data_2.txt,…Data_99.txt,…Data_100.txt
Each file has 20 kbyte.

Parsing code??
I’m applying your code to my project.
Your code don’t have error. good.
Thank you for many answer.
I’m sorry. Because I’m studying language. my major is mechanical engineering. So, It is difficult to understand code.