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:
//ithinkthisscriptgoesinafoldercalled "Editor" //ithinkthisishowyoumakethingshappenintheEditor, vs. runtime
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...
05-01-2014, 04:20 PM (This post was last modified: 05-01-2014, 05:01 PM by SerkBer.)
Hi!
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,
Lila
(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: https://www.youtube.com/watch?v=t-wShOv8c1E which may help.
If you could point me towards any information on how to make a custom Toolbar Action, that would be awesome!
Thanks,
(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: http://docs.fluffyunderware.com/curvy160/api.html 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:
(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.
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?[/font]
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...
do{ text = reader.ReadLine(); string [] coords = text.Split(''); lineNum++; Points.Add(newVector3(float.Parse(coords[0]),float.Parse(coords[1]),float.Parse(coords[2]))); //Console.WriteLine(text); } while (text != null);
Spline.Add(Points);
}
}
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?
05-26-2014, 07:43 PM (This post was last modified: 05-26-2014, 07:45 PM by Jake.)
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):
Code:
public void ReadFile()
{
if (mSpline && File)
{
mSpline.Clear();
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])));
}
mSpline.Add(Points.ToArray());
}
}
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.