WriteInFile.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using UnityEngine;
  2. using System;
  3. using System.IO;
  4. public class WriteInFile : 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\Data_position\";
  12. string directory = currentPath + @"\Assets\Data_position";
  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 Update()
  26. {
  27. // Save position of Human leaders in file
  28. string pos = this.gameObject.transform.position.ToString("f6");
  29. string rot = this.gameObject.transform.rotation.ToString("f6");
  30. if (File.Exists(path))
  31. {
  32. try
  33. {
  34. using (StreamWriter file = new StreamWriter(path, true))
  35. {
  36. file.WriteLine(pos + "," + rot);
  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. }