Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
splines from .txt files
Hi, I am having a great time making splines manually.
I want to make splines automatically from txt files, though, cos some are really huge.
I am very new to C#, but I want to use curvy like this:

1. drop a txt file into the editor of a CurvySpline
2. ask Curvy to make new control points specified by each line in the txt file

I want this to happen in the editor, not instantiate in gameplay.
I have alot of splines to work with, so this is pretty important to me.
I could drop each txt file onto a new CurvySpline, but if I could drop a folder onto a CurvySplineGroup that would be even better.
In the end, the goal is to have a bunch of different splines defined by txt. I could put each spline in the same txt file instead of separate ones, if that is easier.
So far they are all separate txt files.

This is my broken code idea so far for dropping a txt file into a CurvySpline:

//i think this script goes in a folder called "Editor"
//i think this is how you make things happen in the Editorvsruntime

using UnityEditor;
using System.Collections;

public class read_vector : MonoBehaviour
        //drop text asset here in editor:
    public TextAsset track

        //read text asset as string:
    static void Create() //not sure if should be hereguessing

        //print(track.text); (prob dont need this)
        string fileRead = track.text;

        //read file as separated by return lines and number each
        string lines = fileRead.Split("\n"[0]);
        float int i = Parse(pt[0]);
        vectors = new Vector3[lines.Length];
        for (int i = 0i < lines.Lengthi++)

        //read each line and label "xyz"
        string ()()() lines = fileRead.Split (","[0])
        float int x = Parse(pt[0]);
        float int y = Parse(pt[1]);
        float int z = Parse(pt[2]);

        //i think i need some way to associate each vector 3 with each line
        //maybe with the same int variable like x(i), y(i), z(i)?

        //create new curvyspline control point with vector x,y,z for each line
        vectors [i] = new Vector3 ("x","y","z")
        CurvySpline.Add.(vectors [i])

        //stop when there are no more lines


My text files look like this:

-4.096196175 13.23249722 49.63834381
-4.012486458 13.07298183 49.5514679
-3.953039408 12.89356709 49.48607635
-3.910502434 12.70170784 49.44891739
-3.84428215 12.51645088 49.41293716
-3.742287636 12.34783459 49.37879944
-3.630933762 12.1874485 49.33547974
-3.524027586 12.02439499 49.2909317
-3.40421176 11.87058163 49.24637222
-3.317773581 11.6994257 49.18950272
-3.231610537 11.52034092 49.16704178
-3.151533842 11.33750439 49.17963409
-3.047248125 11.16755295 49.16411591
-2.970181704 10.98312473 49.17096329
-2.896412134 10.80042648 49.20530319
-2.829617023 10.61301899 49.22572327

etc etc etc

Any advice is welcome. I am a dedicated learner.
I see much potential in this plug in, but I am not sure how to get it to do what I want yet.

Thanks <3,







the idea is good, but I would create a custom Toolbar Action to handle the task. The big question is the input format. How do you create those text files? Usually one uses XML to define data of that sort, most likely SVG.

Thinking about it, I will add a SVG importer to our Roadmap, but implementing it will take some time, I guess. On the other side, your idea will make p for a good tutorial on how to implement a custom toolbar action, so I will write it if I find some spare time.

In the meanwhile, if anyone want's to improve the given code (quickly reading over it and it looks like it won't compile without errors), don't hesitate...

The files are created in an other visualizing software, but just as line segments. They can be exported in multiple formats, all human readable, including obj.
I will look up how to make a cutom tool bar action, thanks for the tip.

I am thinking the direction of my script is not adequate to begin with.
The solution probably involves making blank prefabs of and empty CurvySplineGroup, an empty CurvySpline with a preset CurvySplinePathMeshBuilder on it, and an empty CurvySplineSegment, then making a script to populate the group with a spline for each file, a segment for each line in the file. Then I get a 3d tube shaped path for each set of data. 

My script is bad, and more like a rough sketch for what I want to do. I will keep working on this and post my progress.
Getting a script to read the text file into a spline is the first step, and I think would be very useful to others : D

Thanks for the reply,



(04-30-2014, 08:44 PM)'Jake' Wrote: Hi,

the idea is good, but I would create a custom Toolbar Action to handle the task. 


I am not sure what you mean by custom Toolbar Action at the moment. Looking into Advanced Editor Scripting: which may help.
If you could point me towards any information on how to make a custom Toolbar Action, that would be awesome!


(05-01-2014, 04:20 PM)'Lila' Wrote: If you could point me towards any information on how to make a custom Toolbar Action, that would be awesome!

Sure, please see "Extending Curvy" here: If you're unsure how that all fits together, just examine the code of the existing actions to get the hang on it.

Hey, thanks for the pointers.
I am learning about Editor scripting as fast as I can. It seems the most useful aspect of working in Unity.
It still doesn't work, but here is my new attempt:

My idea so far is to drop the custom editor on a prefab of a CurvySplineSegment, then drop a text file into it, click READ and automate adding a CurvySpline control point for each line. I hope I am going about this the right way. I shall keep doing research.

Heres the script, and advice or tips or direction would very welcome:

[font]using UnityEngine;
using UnityEditor;
using System.Collections;

[CustomEditortypeofsplineReader ) )]
public class splineReader : Editor

    public TextAsset track;
    string fileRead = track.text;

    public override void OnInspectorGUI()


        base.OnInspectorGUI ();

        EditorGUILayout.LabelField ("Spine Reader");

        ifGUILayout.Button ( "READ!"))
            Debug.Log("pressed read");

            string lines = fileRead.Split("\n")

            while (lines != null)
                string[] position = fileRead.Split (" ");
                CurvySpline.Add(new vector3 ([0],[1],[2]))

                Debug.Log("made a CP")

                string lines = fileRead.Split ("\n");


            Debug.Log("done making a spline")



i found these tutorials the most helpful ~

building a custom editor:
Reading text files into strings (also awesome accent hehe):
(05-14-2014, 10:49 PM)'Lila' Wrote: My idea so far is to drop the custom editor on a prefab of a CurvySplineSegment, then drop a text file into it, click READ and automate adding a CurvySpline control point for each line.

It isn't working the way you describe it. Custom Editors are tied to Monobehaviours, so in your example you'll need to have a Monobehaviour with the name "splineReader" attached to a GameObject. Just attach your Monobehaviour to the CurvySpline GameObject and read data&set CP's from there, much like the CurvyShape classes do (Hint: examine how the legacy SplineShaper.cs (or one of the new CurvyShape subclasses) manages a spline).

Speaking about Shapes, be warned that this may cause Editor stalls, there are some bugs in there. They will be fixed in 1.61 which I'm about to submit today.

Thank you, good to know about 1.61 and the related bugs. Does this mean a new version is coming out soon?

I have slightly changed directions again. I am a beginner at code, so the learning curve is a bit steep.

This works to read and put the data into strings:


[font]using UnityEngine;
using System.Collections;

public class readTXT : MonoBehaviour {

    public TextAsset track;

    void Start()    

        string[] dataLines = track.text.Split('\n');
        string[][] dataPoints = new string[dataLines.Length][];
        int lineNum = 0;
        foreach (string line in dataLines)
            dataPoints[lineNum++] = line.Split(' ');

        //DEBUGGING: (it works)
        //Debug.Log(track.text); //print document
        //Debug.Log(dataPoints[2][1]);  // prints 10.65696526
        //Debug.Log(dataLines[5]); // prints -27.31221962 11.09237289 76.1122818



And this works just to reference this script and make a button:


using UnityEngine;
using UnityEditor;
using System.Collections;

[CustomEditortypeofreadTXT ) )]
public class Read : Editor
    public override void OnInspectorGUI()
        base.OnInspectorGUI ();
        EditorGUILayout.LabelField ( "Spine Reader" );
        ifGUILayout.Button ( "READ!"))
            Debug.Log"pressed read" );


But as far as getting it to read the file into a spline, I am not sure. When I try I get errors for trying to access a non static variable. But really, I am not sure how to address curvy. Should I make an array of vector3? Or a for / foreach look to cycle through the document and instantiate a control point?

I looked at the information on SplineShaper and CurvyShape, but I am afraid as a beginner I wasn't able to glean much from them.
Does this mean CurvySpline.Add() is not the right way to make a control point?
Try something like this (not tested, but may work):

public class readTXT : MonoBehaviour {
    public CurvySpline Spline;
    public TextAsset track;
    public void ReadPoints()
     string[] dataLines = track.text.Split('\n');
        List<Vector3>Points= new List<Vector3>();
        int lineNum = 0;
        foreach (string line in dataLines)
            string[] vec3string=line.Split(' ');
            Points.Add(new Vector3(float.Parse(vec3string[0]),float.Parse(vec3string[1]),float.Parse(vec3string[2])));


[CustomEditor( typeof( readTXT ) )]
public class Read : Editor
    public readTXT Target { get { return target as readTXT;}}
    public override void OnInspectorGUI()
        base.OnInspectorGUI ();
        EditorGUILayout.LabelField ( "Spine Reader" );
        if( GUILayout.Button ( "READ!"))

Also, a good idea is to name and capitalize classes properly (almost everyone would name "readTXT" ReadTxt, or even better something like FileToSpline, which is more descriptive).
The code looks like what I need! I tried it with some colleagues and seem to run into problems like:

In MonoDevelop:
! Line 36 The best overloaded method math for 'CurvySpline.Add(CurvySplineSegment)' has some invalid arguments (CS1502)
! Line 36 Argument '#1' cannot convert 'System.Collections.Generic.List<UnityEngine.Vecotr3>' expression to type 'CurvySplineSegment' (CS1503)

In Unity:
Assets/_Scripts/readTXT2.cs(17,47): error CS1061: Type `System.IO.FileInfo' does not contain a definition for `OpenText' and no extension method `OpenText' of type `System.IO.FileInfo' could be found (are you missing a using directive or an assembly reference?)

We tried a few different way to try to make the script work, perhaps it is an issue with the imported libraries?
Here's what we had so far with the above errors...

[font]using UnityEngine;
using System.Collections;
using System.IO;
using System.Collections.Generic;

public class readTXT2 : MonoBehaviour {

public CurvySpline Spline;

    void Start()    
        FileInfo trackFile = new FileInfo ("/Users/lila/Desktop/track11.txt");

        TextReader reader = trackFile.OpenText();
        int lineNum = 0;

        List<Vector3>Pointsnew List<Vector3>();

        string text;

                text = reader.ReadLine();
                string [] coords = text.Split(' ');
                Points.Add(new Vector3(float.Parse(coords[0]),float.Parse(coords[1]),float.Parse(coords[2])));
                } while (text != null);




Thank you for the code : D I will keep trying using both stream reader and as a text asset. But I wonder if the problem is just with the vector3 list somehow?


Ok, that's the problem when writing code out of the head without testing it. Spline.Add() takes an array only, so instead of a list you could use an array, though I prefer lists when timing isn't critical due to ease of use. It turns out that you can solve your problem with incredible few lines of code. Note that Unity features a TextAsset that I'm using here (it's the easiest way to read a string from a file in Unity and you can drag&drop them from the project window):

public void ReadFile()
if (mSpline && File)
string lines = File.text.Split('\n');
List<Vector3> Points = new List<Vector3>;();

foreach (var line in lines)
string coords = line.Split(' ');
Points.Add(new Vector3(float.Parse(coords[0]), float.Parse(coords[1]), float.Parse(coords[2])));


I've attached the script, it's editor script and a textfile with your data in the package. Just create an empty GameObject, drag&drop SplineFromFile to it, select the textfile and hit the button.







Attached Files
.zip (Size: 2.89 KB / Downloads: 10)

Possibly Related Threads…
Thread Author Replies Views Last Post
  Frequent runtime updates of Curvy Generator for simulating plant growth using Splines h4ck3rd4wg 6 706 10-02-2020, 07:57 PM
Last Post: _Aka_
  Creating splines at run time jamesunity 6 1,201 07-17-2020, 10:17 PM
Last Post: jamesunity
  Letting users create and edit splines at runtime skote 1 517 07-12-2020, 04:49 PM
Last Post: _Aka_
  How do join 3 splines && Orientation problem athos_k 3 901 07-09-2020, 10:41 AM
Last Post: _Aka_

Forum Jump: