Search code examples
unity-game-enginepositionloadtext-parsingcontinuous

How can I continuously load the position data from .txt file?


In Unity3D, I want to load position data from text file. Here is an example of the text file.

data_01.txt

1; -5; -10

data_ 02.txt

2; 2; 5

data_03.txt

3; 2; 4

...............

all files are 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. There is code saving position data.

 public void    Start()

 {
     try
     {
         System.IO.StreamWriter    saveFile = new System.IO.StreamWriter("savefile.txt", false);
         Transform[]    transforms = GameObject.FindObjectsOfType(typeof(Transform)) as Transform[];
         foreach (Transform t in transforms)
         {
             this.saveFile.WriteLine(t.position);
         }
     }
     catch (System.Exception ex)
     {
         Debug.Log(ex.Message);
     }
     finally
     {
         saveFile.Close();
     }
 }

How can I implement code loading position data in Unity continuously?


Solution

  • You could write some code where you use System.IO and use that to read the text file. Then you could use Regex to split the string at a semicolon. Here is some example code. The code is written on my iPhone so it may not be perfect.

    using System.io
    using System.Regex
    using unityengine;
    
    StreamReader reader = new StreamReader("file name");
    
    String read = reader.Read();
    String[] points = Regex.Split(read,";");
    //now you can use the points array to visualize your data
    

    You will probably need to put this In a for loop to get all of your files but this should work. Also tell me about any mistakes in my code and I will try to fix them.