using UnityEngine; using System.Collections.Generic; using System.IO; using System; [DefaultExecutionOrder(20)] [RequireComponent(typeof(InstantiatePrefab))] public class ReadFromCSV : MonoBehaviour { private Tuple, List, List, List>[][] humansData; // Get Thief [HideInInspector] public int thief_i; [HideInInspector] public int thief_j; private void Start() { GameObject[][] humansGO = gameObject.GetComponent().humanGameObject; humansData = new Tuple, List, List, List>[humansGO.Length][]; for(int i = 0; i < humansGO.Length; i++) { humansData[i] = new Tuple, List, List, List>[humansGO[i].Length]; for(int j = 0; j < humansData[i].Length; j++) { humansData[i][j] = new Tuple, List, List, List>(new List(), new List(), new List(), new List()); } } } /// /// Reads from the given path the csv file /// /// The path were the csv file is located /// A matrix of tuples with the following order in the tuple: time; Position; Rotation; Speed /// public Tuple, List, List, List>[][] ReadFromCSVFile(string path) { if(!File.Exists(path)) return null; try { using (StreamReader reader = new StreamReader(path, true)) { // Skip header reader.ReadLine(); while (!reader.EndOfStream) { var line = reader.ReadLine(); var values = line.Split(';'); /* * Value 0 : Time * Value 1-2: i,j * Value 3-5: Position * Value 6-9: Rotation * Value 10 : Speed * Value 11 : Thief YES/NO */ // Set i, j position in matrix int i = int.Parse(values[1]); int j = int.Parse(values[2]); // Add into timer list humansData[i][j].Item1.Add(float.Parse(values[0])); // Add into position list humansData[i][j].Item2.Add(new Vector3( float.Parse(values[3]), float.Parse(values[4]), float.Parse(values[5]))); // Add into rotation list humansData[i][j].Item3.Add(new Quaternion( float.Parse(values[6]), float.Parse(values[7]), float.Parse(values[8]), float.Parse(values[9]))); humansData[i][j].Item4.Add(float.Parse(values[10])); if (values[11] == "1") { thief_i = i; thief_j = j; } } } } catch (Exception e) { throw new ApplicationException("Something went wrong by reading from a csv file: ", e); } return humansData; } }