CalibrationRecorderBehavior.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using Assets.StreetLight.Scripts;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Concurrent;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9. using UnityEngine;
  10. namespace Assets
  11. {
  12. public class CalibrationRecorderBehavior : MonoBehaviour
  13. {
  14. PersonManager PersonManager => personManagerLazy.Value;
  15. Lazy<PersonManager> personManagerLazy;
  16. private void Awake()
  17. {
  18. personManagerLazy = new Lazy<PersonManager>(FindObjectOfType<PersonManager>);
  19. }
  20. BlockingCollection<Action> taskQueue;
  21. string fileName = @$"C:\Users\nick.steyer\Desktop\{DateTime.Now:yyyy-dd-M_HH-mm-ss}.csv";
  22. void Start()
  23. {
  24. taskQueue = new BlockingCollection<Action>();
  25. if (!File.Exists(fileName))
  26. {
  27. File.WriteAllLines(fileName, new string[] { "Time,WorldX,WorldY,WorldZ,UnityX,UnityY,UnityZ" });
  28. }
  29. }
  30. // Update is called once per frame
  31. void Update()
  32. {
  33. var persons = PersonManager.Persons;
  34. if (PersonManager.Persons.Count == 1)
  35. {
  36. var person = PersonManager.Persons.Single();
  37. var marker = GameObject.Find("CalibrationMarker");
  38. var personPosition = person.WorldPosition;
  39. var markerPosition = marker.transform.position;
  40. taskQueue.Add(() => File.AppendAllLines(fileName, new string[] { FormattableString.Invariant($"{DateTime.Now:yyyy-MM-dd-HH-mm-ss-ff},{personPosition.x},{personPosition.y},{personPosition.z},{markerPosition.x},{markerPosition.y},{markerPosition.z}") }));
  41. Task.Run(() => taskQueue.Take().Invoke());
  42. }
  43. }
  44. }
  45. }