UISlider.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using UnityEngine;
  5. public class UISlider : MonoBehaviour
  6. {
  7. // Inspector variables
  8. public string logFileName = "";
  9. // Get from LoggingManager
  10. private int ParticipantID;
  11. private string logPathFolder = "";
  12. // Changes on click
  13. private int sliderValue = 0;
  14. private string logPath;
  15. private float time = 0f;
  16. private void Start()
  17. {
  18. ParticipantID = gameObject.GetComponentInParent<LoggingManager>().ParticipantID;
  19. logPathFolder = gameObject.GetComponentInParent<LoggingManager>().LogPathFolder;
  20. logFileName = "log_" + ParticipantID + "_" + logFileName + ".csv";
  21. logPath = Path.Combine(logPathFolder, logFileName);
  22. using (FileStream stream = File.Open(logPath, FileMode.Create))
  23. {
  24. using (StreamWriter writer = new StreamWriter(stream))
  25. {
  26. writer.WriteLine(GetLogFileHeader());
  27. writer.Flush();
  28. }
  29. }
  30. Debug.Log("Created new Logfile " + logFileName);
  31. }
  32. private string GetLogFileHeader()
  33. {
  34. StringBuilder header = new StringBuilder("ParticipantID");
  35. header.Append(";Time");
  36. header.Append(";Value");
  37. return header.ToString();
  38. }
  39. public void registerOnValueChanged(float value)
  40. {
  41. sliderValue = (int) value;
  42. }
  43. private void FixedUpdate()
  44. {
  45. if (File.Exists(logPath))
  46. {
  47. try
  48. {
  49. using (StreamWriter writer = new StreamWriter(logPath, true))
  50. {
  51. StringBuilder line = new StringBuilder();
  52. line.Append(ParticipantID);
  53. line.Append(";" + time);
  54. line.Append(";" + sliderValue);
  55. writer.WriteLine(line);
  56. writer.Flush();
  57. }
  58. }
  59. catch (Exception e)
  60. {
  61. throw new ApplicationException("Something went wrong by writing into a csv file: ", e);
  62. }
  63. }
  64. time += Time.deltaTime;
  65. }
  66. }