SavePosInFile.cs 1.3 KB

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