ReadFromFileOld.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using UnityEngine;
  2. using System.Globalization;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System;
  6. public class ReadFromFileOld : MonoBehaviour
  7. {
  8. //private void Start()
  9. //{
  10. // string dir = Directory.GetCurrentDirectory();
  11. // string reference = @"\Assets\Data_position\Walk1.txt";
  12. // List<Vector3> vec = ReadFromTxtFile(dir + reference);
  13. //}
  14. //Reads from txt files
  15. public Tuple<List<Vector3>, List<Quaternion>> ReadFromTxtFile(string path)
  16. {
  17. if(!File.Exists(path))
  18. {
  19. return null;
  20. }
  21. List<Quaternion> rotList = new List<Quaternion>();
  22. string[] splitPosRot = new string[] { "),(" };
  23. List<Vector3> posList = new List<Vector3>();
  24. char[] seperator = new char[] { ',' };
  25. foreach (string line in File.ReadLines(path))
  26. {
  27. if(line != null)
  28. {
  29. // removing parentheses
  30. string lineCleaned = line.Substring(1, line.Length - 2);
  31. // splitting into 0 -> Position and 1 -> Rotation
  32. string[] posRow = lineCleaned.Split(splitPosRot, StringSplitOptions.RemoveEmptyEntries);
  33. // Adding into position list
  34. string[] pos = posRow[0].Split(seperator, StringSplitOptions.RemoveEmptyEntries);
  35. posList.Add(new Vector3(
  36. float.Parse(pos[0], CultureInfo.InvariantCulture),
  37. float.Parse(pos[1], CultureInfo.InvariantCulture),
  38. float.Parse(pos[2], CultureInfo.InvariantCulture)));
  39. // Adding into rotation list
  40. string[] rot = posRow[1].Split(seperator, StringSplitOptions.RemoveEmptyEntries);
  41. rotList.Add(new Quaternion(
  42. float.Parse(rot[0], CultureInfo.InvariantCulture),
  43. float.Parse(rot[1], CultureInfo.InvariantCulture),
  44. float.Parse(rot[2], CultureInfo.InvariantCulture),
  45. float.Parse(rot[3], CultureInfo.InvariantCulture)));
  46. }
  47. }
  48. return Tuple.Create(posList, rotList);
  49. }
  50. }