- Home /
Import .txt and make vector3 array
I try to import a list of coordinate from .txt file (0,0,0,1,1,1,2,3,4,2,2,2 // x,y,z,x1,y1,z1...) and then make a vector3 array to draw line. I'm using this code:
import System.IO;
var Filename = "Point.txt";
var lineMaterial : Material;
var lineThickness = 1.0;
var line3D = false;
private var PointsList : float[];
private var Lines : Array;
private var LinesArray : Vector3[];
private var i : int;
//PointsList = []; // in old version I copy point directly, but now i will do copy directly from .txt external file
function Start () {
var sr = new StreamReader(Application.dataPath + "/" + Filename);
var fileContents = sr.ReadToEnd();
sr.Close();
var PointsList = fileContents.Split(","[0]);
Lines = new Array();
for (i = 0; i < PointsList.length / 3; i++){
Lines.Add(Vector3(-PointsList[i*3],PointsList[i*3+2],-PointsList[i*3+1]));
}
LinesArray = Lines.ToBuiltin(Vector3);
var line = new VectorLine("Line", LinesArray, Color.white, lineMaterial, lineThickness);
VectorManager.useDraw3D = line3D;
VectorManager.ObjectSetup (gameObject, line, Visibility.Dynamic, Brightness.None);
}
Unity give me error:
Assets/Script/Wireframe.js(24,62): BCE0050: Operator '-' cannot be used with an expression of type 'String'. Somebody help me? Thanks.
Answer by whydoidoit · May 13, 2012 at 04:48 PM
You need to convert the strings in the array to floats in your loop line:
Lines.Add(Vector3(-PointsList[i*3],PointsList[i*3+2],-PointsList[i*3+1]));
You can do that using float.Parse(someString).
So something like Lines.Add(Vector3(-float.Parse(PointsList[i*3]), etc.
thanks for answer. Single.Parse doesn't work. I try with float.Parse and no problem with compilation but when I play scene give me error: !IsFinite(outDistanceForSort) Any idea?
Oops, sorry about that Single thing, will fix the answer. Thinking about the next problem :)
Ok so I'm guessing something in your input string isn't a float - that error would indicate that it was NaN somewhere (Not A Number). I'm not too familiar with Vectrosity, but check your file. (BTW your comment indicates that the file is in the format x,y,z but that's not how you are constructing the Vector3 - which uses the 3rd element in the second location - this is NOT your current problem however).
By the way float.Parse is more aggressive than the alternate Convert.ToSingle(someString) which will do its best - rather than saying the string must be only a float - you know Convert.ToSingle("0 is a number") will return 0 where float.Parse will not
Answer by benfattino · May 13, 2012 at 08:32 PM
I try another way:
import System.IO;
//var Filename = "Point.txt";
var Filename = String;
var lineMaterial : Material;
var lineThickness = 1.0;
var line3D = false;
private var LinesArray : Vector3[];
private var i : int;
function Start () {
var sr = new StreamReader(Application.dataPath + "/" + Filename);
var fileContents = sr.ReadToEnd();
sr.Close();
var Points = fileContents.Split(","[0]);
PointsList = new Array();
for (i = 0; i < Points.length; i++){
PointsList.Add(float.Parse(Points[i]));
}
Lines = new Array();
for (i = 0; i < PointsList.length / 3; i++){
Lines.Add(Vector3(-PointsList[i*3],PointsList[i*3+2],-PointsList[i*3+1]));
}
LinesArray = Lines.ToBuiltin(Vector3);
var line = new VectorLine("Line", LinesArray, Color.white, lineMaterial, lineThickness);
VectorManager.useDraw3D = line3D;
VectorManager.ObjectSetup (gameObject, line, Visibility.Dynamic, Brightness.None);
}
Works. But, if the string start with 0 (for example 0,0,0,1,1,1) unity give me error when play (but it play again):
!IsFinite(outDistanceForSort)
Any ideas?
What is the full version of that error in the console, under the message line there should be a stack trace
You should probably print() each of your Vector3s after parsing to make sure that it is working. It feels a bit like they are all zero or something
Unity give me only: !IsFinite(outDistanceForSort) But it seem to be work...