123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- using UnityEngine;
- using System.Collections.Generic;
- using System.IO;
- using System;
- [DefaultExecutionOrder(20)]
- [RequireComponent(typeof(InstantiatePrefab))]
- public class ReadFromCSV : MonoBehaviour
- {
- private Tuple<List<float>, List<Vector3>, List<Quaternion>, List<float>>[][] humansData;
- // Get Thief
- [HideInInspector]
- public int thief_i;
- [HideInInspector]
- public int thief_j;
- private void Start()
- {
- GameObject[][] humansGO = gameObject.GetComponent<InstantiatePrefab>().humanGameObject;
- humansData = new Tuple<List<float>, List<Vector3>, List<Quaternion>, List<float>>[humansGO.Length][];
- for(int i = 0; i < humansGO.Length; i++)
- {
- humansData[i] = new Tuple<List<float>, List<Vector3>, List<Quaternion>, List<float>>[humansGO[i].Length];
- for(int j = 0; j < humansData[i].Length; j++)
- {
- 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>());
- }
- }
- }
- /// <summary>
- /// Reads from the given path the csv file
- /// </summary>
- /// <param name="path">The path were the csv file is located</param>
- /// <returns>A matrix of tuples with the following order in the tuple: time; Position; Rotation; Speed</returns>
- /// <exception cref="ApplicationException"></exception>
- public Tuple<List<float>, List<Vector3>, List<Quaternion>, List<float>>[][] 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;
- }
- }
|