ReadFromCSV.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System;
  5. [DefaultExecutionOrder(20)]
  6. [RequireComponent(typeof(InstantiatePrefab))]
  7. public class ReadFromCSV : MonoBehaviour
  8. {
  9. private Tuple<List<float>, List<Vector3>, List<Quaternion>, List<float>>[][] humansData;
  10. // Get Thief
  11. [HideInInspector]
  12. public int thief_i;
  13. [HideInInspector]
  14. public int thief_j;
  15. private void Start()
  16. {
  17. GameObject[][] humansGO = gameObject.GetComponent<InstantiatePrefab>().humanGameObject;
  18. humansData = new Tuple<List<float>, List<Vector3>, List<Quaternion>, List<float>>[humansGO.Length][];
  19. for(int i = 0; i < humansGO.Length; i++)
  20. {
  21. humansData[i] = new Tuple<List<float>, List<Vector3>, List<Quaternion>, List<float>>[humansGO[i].Length];
  22. for(int j = 0; j < humansData[i].Length; j++)
  23. {
  24. humansData[i][j] = new Tuple<List<float>, List<Vector3>, List<Quaternion>, List<float>>(new List<float>(), new List<Vector3>(), new List<Quaternion>(), new List<float>());
  25. }
  26. }
  27. }
  28. /// <summary>
  29. /// Reads from the given path the csv file
  30. /// </summary>
  31. /// <param name="path">The path were the csv file is located</param>
  32. /// <returns>A matrix of tuples with the following order in the tuple: time; Position; Rotation; Speed</returns>
  33. /// <exception cref="ApplicationException"></exception>
  34. public Tuple<List<float>, List<Vector3>, List<Quaternion>, List<float>>[][] ReadFromCSVFile(string path)
  35. {
  36. if(!File.Exists(path)) return null;
  37. try
  38. {
  39. using (StreamReader reader = new StreamReader(path, true))
  40. {
  41. // Skip header
  42. reader.ReadLine();
  43. while (!reader.EndOfStream)
  44. {
  45. var line = reader.ReadLine();
  46. var values = line.Split(';');
  47. /*
  48. * Value 0 : Time
  49. * Value 1-2: i,j
  50. * Value 3-5: Position
  51. * Value 6-9: Rotation
  52. * Value 10 : Speed
  53. * Value 11 : Thief YES/NO
  54. */
  55. // Set i, j position in matrix
  56. int i = int.Parse(values[1]);
  57. int j = int.Parse(values[2]);
  58. // Add into timer list
  59. humansData[i][j].Item1.Add(float.Parse(values[0]));
  60. // Add into position list
  61. humansData[i][j].Item2.Add(new Vector3(
  62. float.Parse(values[3]),
  63. float.Parse(values[4]),
  64. float.Parse(values[5])));
  65. // Add into rotation list
  66. humansData[i][j].Item3.Add(new Quaternion(
  67. float.Parse(values[6]),
  68. float.Parse(values[7]),
  69. float.Parse(values[8]),
  70. float.Parse(values[9])));
  71. humansData[i][j].Item4.Add(float.Parse(values[10]));
  72. if (values[11] == "1")
  73. {
  74. thief_i = i;
  75. thief_j = j;
  76. }
  77. }
  78. }
  79. }
  80. catch (Exception e)
  81. {
  82. throw new ApplicationException("Something went wrong by reading from a csv file: ", e);
  83. }
  84. return humansData;
  85. }
  86. }