06-17-2014, 11:48 AM
Hej!
This is the script changed slightly so it doesnt error if there are unexpected charaters in the txt file (like extra returns, etc)
I am learning very slowlz but with lots of help : D
[font]using UnityEngine;
using System.Collections;
using System.IO;
using System.Collections.Generic;
[ExecuteInEditMode, RequireComponent(typeof(CurvySpline))]
public class SplineFromFile : MonoBehaviour {
public TextAsset File;
CurvySpline mSpline;
void Awake()
{
mSpline = GetComponent<CurvySpline>();
}
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(' ');
//Debug.Log(coords.Length.ToString);
if(coords.Length == 3){
Points.Add(new Vector3(float.Parse(coords[0]), float.Parse(coords[1]), float.Parse(coords[2])));
}
}
mSpline.Add(Points.ToArray());
}
}
}
YAY it works!!!
So, now since there are so many splines to make, we are trying to read them all from one file.
So far, this just reads the last entry...
Just thought I'd post the progress. We will keep working on it.[/font]
Any advice, as always, would be really awesome.
Danke,
Lila
This is the script changed slightly so it doesnt error if there are unexpected charaters in the txt file (like extra returns, etc)
I am learning very slowlz but with lots of help : D
[font]using UnityEngine;
using System.Collections;
using System.IO;
using System.Collections.Generic;
[ExecuteInEditMode, RequireComponent(typeof(CurvySpline))]
public class SplineFromFile : MonoBehaviour {
public TextAsset File;
CurvySpline mSpline;
void Awake()
{
mSpline = GetComponent<CurvySpline>();
}
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(' ');
//Debug.Log(coords.Length.ToString);
if(coords.Length == 3){
Points.Add(new Vector3(float.Parse(coords[0]), float.Parse(coords[1]), float.Parse(coords[2])));
}
}
mSpline.Add(Points.ToArray());
}
}
}
YAY it works!!!
So, now since there are so many splines to make, we are trying to read them all from one file.
So far, this just reads the last entry...
Just thought I'd post the progress. We will keep working on it.[/font]
- using UnityEngine;
- using System.Collections;
- using System.IO;
- using System.Collections.Generic;
-
- [ExecuteInEditMode, RequireComponent(typeof(CurvySpline))]
- public class SplineFromFileAll : MonoBehaviour {
-
- public TextAsset File;
-
- CurvySpline mSpline;
- public GameObject allTracts;
-
- void Awake()
- {
- mSpline = GetComponent<CurvySpline>();
- }
-
- public void ReadFile()
- {
-
- if (mSpline && File) // check for not null and file exist / readable
- {
-
- mSpline.Clear();
- string[] linesOfFile = File.text.Split('\n');
- List<Vector3> splineCoords = new List<Vector3>();
-
- List<CurvySpline> allSplines = new List<CurvySpline>();
-
-
-
- var begin = true;
- var id = 0;
- string tractName = "Tract_" + id;
- foreach (var line in linesOfFile)
- {
-
- string[] coords = line.Split('\t');
-
- if(coords[0] == "newTrack" || coords[0] == " newTrack"){
-
- if(begin == true){
- Debug.Log("first");
- begin = false;
-
- }else {
- mSpline.Clear();
-
- mSpline.Add(splineCoords.ToArray());
- allSplines.Add(mSpline);
- tractName = "Tract_" + id;
- gameObject.AddComponent(tractName) as CurvySpline;
-
-
- id++;
- }
-
- splineCoords.Clear();
-
- }else if(coords.Length == 3){
- splineCoords.Add(new Vector3(float.Parse(coords[0]), float.Parse(coords[1]), float.Parse(coords[2])));
- }
- }
- mSpline.Clear();
- mSpline.Add(splineCoords.ToArray());
- allSplines.Add(mSpline);
- Debug.Log("number allsplines: " + allSplines.Count);
- }
- }
- }
Any advice, as always, would be really awesome.
Danke,
Lila