SavePosInFile.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using UnityEngine;
  2. using System;
  3. using System.IO;
  4. public class SavePosInFile : MonoBehaviour
  5. {
  6. public string directory = @"C:\Users\furka\Git_Repositories\Bachelor-Thesis_Furkan_Karakocaoglu\testumgebung\CrowdModelling\Assets\CSV_files";
  7. public string filepath = @"C:\Users\furka\Git_Repositories\Bachelor-Thesis_Furkan_Karakocaoglu\testumgebung\CrowdModelling\Assets\CSV_files\";
  8. public int index = 0;
  9. private string path;
  10. private void Start()
  11. {
  12. // create directory if not existing
  13. if (!Directory.Exists(directory))
  14. {
  15. Directory.CreateDirectory(directory);
  16. }
  17. // create file for path
  18. string currentPath = Directory.GetCurrentDirectory();
  19. string reference = @"\Assets\CSV_files\";
  20. using (File.Create(currentPath + reference + "Walk" + index + ".txt"))
  21. {
  22. path = currentPath + reference + "Walk" + index + ".txt";
  23. }
  24. }
  25. // Start is called before the first frame update
  26. private void FixedUpdate()
  27. {
  28. // Save position of Human leaders in csv - file
  29. string pos = this.gameObject.transform.position.ToString();
  30. if (File.Exists(path))
  31. {
  32. try
  33. {
  34. using (System.IO.StreamWriter file = new System.IO.StreamWriter(path, true))
  35. {
  36. file.WriteLine(pos);
  37. }
  38. }
  39. catch (Exception e)
  40. {
  41. throw new ApplicationException("Something went wrong by writing into a csv file : ", e);
  42. }
  43. }
  44. }
  45. }