1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- using UnityEngine;
- using System;
- using System.IO;
- public class WriteInCSVNew : MonoBehaviour
- {
- public int index = 0;
- private string path;
- private float time;
- private GameObject[][] humansGO;
- private void Start()
- {
- humansGO = gameObject.GetComponent<InstantiatePrefab>().humanGameObject;
- string currentPath = Directory.GetCurrentDirectory();
- string reference = @"\Assets\CSV_files\";
- string directory = currentPath + @"\Assets\CSV_files";
- // create directory if not existing
- if (!Directory.Exists(directory))
- {
- Directory.CreateDirectory(directory);
- }
- // create file for path
- using (File.Create(currentPath + reference + "Walk" + index + ".csv"))
- {
- path = currentPath + reference + "Walk" + index + ".csv";
- }
- // write column name
- using (StreamWriter firstLine = new StreamWriter(path))
- {
- var line = string.Format("Delta Time;Human i;Human j;Position x;Position y;Position z;Rotation x;Rotation y;Rotation z;Rotation w");
- firstLine.WriteLine(line);
- firstLine.Flush();
- }
- }
- // Start is called before the first frame update
- private void FixedUpdate()
- {
- time += Time.deltaTime;
- if (File.Exists(path))
- {
- try
- {
- using (StreamWriter file = new StreamWriter(path, true))
- {
- for(int i = 0; i < humansGO.Length; i++)
- {
- for(int j = 0; j < humansGO[i].Length; j++)
- {
- // Save position of Human leaders in file
- Vector3 pos = humansGO[i][j].transform.position; // (x,y,z)
- Quaternion rot = humansGO[i][j].transform.rotation; // (x,y,z,w)
-
- var line = string.Format("{0};{1};{2};{3};{4};{5};{6};{7};{8};{9}",
- time, i, j, pos.x.ToString("f6"), pos.y.ToString("f6"), pos.z.ToString("f6"), rot.x.ToString("f6"), rot.y.ToString("f6"), rot.z.ToString("f6"), rot.w.ToString("f6"));
- file.WriteLine(line);
- file.Flush();
- }
- }
- }
- }
- catch (Exception e)
- {
- throw new ApplicationException("Something went wrong by writing into a csv file: ", e);
- }
- }
- }
- }
|