ViveTrackerDebugDisplay.cs 3.5 KB

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