Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
splines from .txt files
#16
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;

[ExecuteInEditModeRequireComponent(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<Vector3Points = 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]
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.IO;
  4. using System.Collections.Generic;
  5.  
  6. [ExecuteInEditMode, RequireComponent(typeof(CurvySpline))]
  7. public class SplineFromFileAll : MonoBehaviour {
  8.  
  9. public TextAsset File;
  10.  
  11. CurvySpline mSpline;
  12. public GameObject allTracts;
  13.  
  14. void Awake()
  15. {
  16. mSpline = GetComponent<CurvySpline>();
  17. }
  18.  
  19. public void ReadFile()
  20. {
  21.  
  22. if (mSpline && File) // check for not null and file exist / readable
  23. {
  24.  
  25. mSpline.Clear();
  26. string[] linesOfFile = File.text.Split('\n');
  27. List<Vector3> splineCoords = new List<Vector3>();
  28.  
  29. List<CurvySpline> allSplines = new List<CurvySpline>();
  30.  
  31.  
  32.  
  33. var begin = true;
  34. var id = 0;
  35. string tractName = "Tract_" + id;
  36. foreach (var line in linesOfFile)
  37. {
  38.  
  39. string[] coords = line.Split('\t');
  40.  
  41. if(coords[0] == "newTrack" || coords[0] == " newTrack"){
  42.  
  43. if(begin == true){
  44. Debug.Log("first");
  45. begin = false;
  46.  
  47. }else {
  48. mSpline.Clear();
  49.  
  50. mSpline.Add(splineCoords.ToArray());
  51. allSplines.Add(mSpline);
  52. tractName = "Tract_" + id;
  53. gameObject.AddComponent(tractName) as CurvySpline;
  54.  
  55.  
  56. id++;
  57. }
  58.  
  59. splineCoords.Clear();
  60.  
  61. }else if(coords.Length == 3){
  62. splineCoords.Add(new Vector3(float.Parse(coords[0]), float.Parse(coords[1]), float.Parse(coords[2])));
  63. }
  64. }
  65. mSpline.Clear();
  66. mSpline.Add(splineCoords.ToArray());
  67. allSplines.Add(mSpline);
  68. Debug.Log("number allsplines: " + allSplines.Count);
  69. }
  70. }
  71. }

Any advice, as always, would be really awesome.

Danke,

Lila
Reply


Messages In This Thread
splines from .txt files - by Lila - 04-30-2014, 01:18 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Twisting Splines ZiggZagg 6 10 11 hours ago
Last Post: _Aka_
Information New free asset: Converter For Unity Splines _Aka_ 7 24 05-13-2024, 09:33 AM
Last Post: _Aka_
  Help with procedural roads and splines ramiroflores 1 6 05-07-2024, 08:57 AM
Last Post: _Aka_
  snap to the curve created by the curvy splines segment points ShiroeYamamoto 3 11 04-02-2024, 02:24 PM
Last Post: _Aka_

Forum Jump: