ReadPosFromFile.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using UnityEngine;
  2. using System.Globalization;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System;
  6. public class ReadPosFromFile : MonoBehaviour
  7. {
  8. //private void Start()
  9. //{
  10. // string dir = Directory.GetCurrentDirectory();
  11. // string reference = @"\Assets\CSV_files\Walk1.txt";
  12. // List<Vector3> vec = ReadFromTxtFile(dir + reference);
  13. //}
  14. //Reads from txt files
  15. public List<Vector3> ReadFromTxtFile(string path)
  16. {
  17. if(!File.Exists(path))
  18. {
  19. return null;
  20. }
  21. List<Vector3> vec = new List<Vector3>();
  22. char[] seperator = new char[] { ',' };
  23. foreach (string line in File.ReadLines(path))
  24. {
  25. if(line != null)
  26. {
  27. // removing parentheses
  28. string lineCleaned = line.Substring(1, line.Length - 2);
  29. string[] lineSplited = lineCleaned.Split(seperator, StringSplitOptions.RemoveEmptyEntries);
  30. vec.Add(new Vector3(
  31. float.Parse(lineSplited[0], CultureInfo.InvariantCulture),
  32. float.Parse(lineSplited[1], CultureInfo.InvariantCulture),
  33. float.Parse(lineSplited[2], CultureInfo.InvariantCulture)));
  34. }
  35. }
  36. return vec;
  37. }
  38. }