ViveTrackerDebugDisplay.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System.Globalization;
  2. using TMPro;
  3. using Tracking;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using Valve.VR;
  7. using Valve.VR.InteractionSystem;
  8. namespace Display
  9. {
  10. public class ViveTrackerDebugDisplay : MonoBehaviour
  11. {
  12. public TextMeshProUGUI posText;
  13. public TextMeshProUGUI rotText;
  14. public TextMeshProUGUI batteryTextFw;
  15. public TextMeshProUGUI batteryTextLeg;
  16. public TextMeshProUGUI statusTextFw;
  17. public TextMeshProUGUI statusTextLeg;
  18. public Image fwImage;
  19. public Image legImage;
  20. public FrontWheelTracker fwTracker;
  21. public KineticLegTracker legTracker;
  22. private SteamVR_TrackedObject fwTrackedObject;
  23. private SteamVR_TrackedObject legTrackedObject;
  24. private bool isFwTrackerNull;
  25. private bool islegTrackerNull;
  26. private bool isFwTrackedObjectNull;
  27. private bool isLegTrackedObjectNull;
  28. private bool isrotTextNotNull;
  29. private bool isposTextNotNull;
  30. private bool isbatteryTextFwNotNull;
  31. private bool isbatteryTextLegNotNull;
  32. private bool isstatusTextFwNotNull;
  33. private bool isstatusTextLegNotNull;
  34. // Start is called before the first frame update
  35. private void Start()
  36. {
  37. isstatusTextLegNotNull = statusTextLeg != null;
  38. isstatusTextFwNotNull = statusTextFw != null;
  39. isbatteryTextLegNotNull = batteryTextLeg != null;
  40. isbatteryTextFwNotNull = batteryTextFw != null;
  41. isposTextNotNull = posText != null;
  42. isrotTextNotNull = rotText != null;
  43. isFwTrackerNull = fwTracker == null;
  44. islegTrackerNull = legTracker == null;
  45. fwTrackedObject = fwTracker.GetComponent<SteamVR_TrackedObject>();
  46. legTrackedObject = legTracker.GetComponent<SteamVR_TrackedObject>();
  47. isFwTrackedObjectNull = fwTrackedObject == null;
  48. isLegTrackedObjectNull = legTrackedObject == null;
  49. }
  50. // Update is called once per frame
  51. private void Update()
  52. {
  53. if (!isFwTrackerNull & fwTracker.isActiveAndEnabled & !isFwTrackerNull && fwTrackedObject.isValid)
  54. {
  55. if (isposTextNotNull) posText.text = $"Pos: {fwTracker.RelativePosition.ToString()}";
  56. if (isrotTextNotNull) rotText.text = $"Rot: {fwTracker.SteerRotation:n2}";
  57. if (isbatteryTextFwNotNull) batteryTextFw.text = $"Battery: {fwTracker.BatteryLevel}";
  58. if (isstatusTextFwNotNull) statusTextFw.text = $"Status: Connected";
  59. fwImage.color = Color.green;
  60. }
  61. else
  62. {
  63. if (isposTextNotNull) posText.text = $"Pos: -";
  64. if (isrotTextNotNull) rotText.text = $"Rot: -";
  65. fwImage.color = Color.grey.ColorWithAlpha(100);
  66. if (isstatusTextFwNotNull) statusTextFw.text = $"Status: Disconnected";
  67. }
  68. if (!islegTrackerNull & legTracker.isActiveAndEnabled && !isLegTrackedObjectNull &&
  69. legTrackedObject.isValid)
  70. {
  71. legImage.color = Color.green;
  72. if (isbatteryTextLegNotNull) batteryTextLeg.text = $"Battery: {legTracker.BatteryLevel}";
  73. if (isstatusTextLegNotNull) statusTextLeg.text = $"Status: Connected";
  74. }
  75. else
  76. {
  77. legImage.color = Color.grey.ColorWithAlpha(100);
  78. if (isstatusTextLegNotNull) statusTextLeg.text = $"Status: Disconnected";
  79. }
  80. }
  81. }
  82. }