using UnityEngine; using System; using System.IO; public class WriteInCSV : MonoBehaviour { public int index = 0; private string path; private void Start() { 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("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 Update() { // Save position of Human leaders in file Vector3 pos = this.gameObject.transform.position; // (x,y,z) Quaternion rot = this.gameObject.transform.rotation; // (x,y,z,w) if (File.Exists(path)) { try { using (StreamWriter file = new StreamWriter(path, true)) { // var line = string.Format("{0};{1};{2};{3};{4};{5};{6}", 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); } } } }