ChooseTrackedObjectButton.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /// <summary>
  5. /// Handles an interactable button spawned by ChooseTrackedObjectMenu for choosing which
  6. /// object the ZED is anchored to. Should be attached to a prefab.
  7. /// Each is assigned a specific tracked object, like a Vive Tracker or controller.
  8. /// </summary>
  9. public class ChooseTrackedObjectButton : MonoBehaviour, IXRHoverable, IXRClickable
  10. {
  11. /// <summary>
  12. /// Index of the tracked device that this object represents.
  13. /// Note that if using the Oculus platform, this is defined by constants in ChooseTrackedObjectMenu, TOUCH_INDEX_LEFT and _RIGHT.
  14. /// </summary>
  15. [HideInInspector]
  16. public int deviceIndex = -1;
  17. /// <summary>
  18. /// 3D text object attached to this hbject, used to label it as a Left Controller, Tracker, etc.
  19. /// </summary>
  20. [Tooltip("3D text object attached to this hbject, used to label it as a Left Controller, Tracker, etc. ")]
  21. public TextMesh labelText;
  22. /// <summary>
  23. /// All objects that should be enabled when ZEDXRGrabber is hovering over it, and disabled otherwise.
  24. /// Used to enable a yellow box when hovered over. Done this way to give more flexibility to the prefab.
  25. /// </summary>
  26. [Space(5)]
  27. [Tooltip("All objects that should be enabled when ZEDXRGrabber is hovering over it, and disabled otherwise. " +
  28. "Used to enable a yellow box when hovered over. Done this way to give more flexibility to the prefab.")]
  29. public List<GameObject> enabledWhenHighlighted = new List<GameObject>();
  30. /// <summary>
  31. /// How quickly the controller renderer in the middle of the prefab spins around.
  32. /// </summary>
  33. [Space(5)]
  34. [Tooltip("How quickly the controller renderer in the middle of the prefab spins around. ")]
  35. public float secondsPerRevolution = 10f;
  36. /// <summary>
  37. /// The axis around which the controller renderer in the middle of the prefab spins.
  38. /// </summary>
  39. [Tooltip("The axis around which the controller renderer in the middle of the prefab spins.")]
  40. public Vector3 rotationAxis = Vector3.up;
  41. private SetControllerSkin skin;
  42. /// <summary>
  43. /// Delegate for an event that supplies this device index.
  44. /// </summary>
  45. public delegate void TrackedObjectSelectedDelegate(int deviceindex);
  46. /// <summary>
  47. /// Event called when the user has clicked on this button to choose its tracked object.
  48. /// </summary>
  49. public event TrackedObjectSelectedDelegate OnTrackedObjectSelected;
  50. public void Awake()
  51. {
  52. //if (!controllerTracker) Debug.LogError(gameObject + " controllerTracker value not set.");
  53. skin = GetComponentInChildren<SetControllerSkin>();
  54. if (!skin) skin = gameObject.AddComponent<SetControllerSkin>();
  55. skin.checkDeviceIndexEachUpdate = false;
  56. if (deviceIndex != -1) SetDeviceIndex(deviceIndex);
  57. //Make sure we have a collider.
  58. Collider collider = GetComponent<Collider>();
  59. if (!collider)
  60. {
  61. collider = gameObject.AddComponent<SphereCollider>();
  62. ((SphereCollider)collider).radius = 0.15f;
  63. }
  64. collider.isTrigger = true;
  65. if(!labelText)
  66. {
  67. labelText = GetComponentInChildren<TextMesh>();
  68. }
  69. }
  70. /// <summary>
  71. /// Changes the text on the attached 3D text field. Used to indicate what kind of object it is (Tracker, Left Controller, etc.)
  72. /// </summary>
  73. public void SetLabel(string text)
  74. {
  75. if (!labelText) return;
  76. labelText.text = text;
  77. }
  78. private void Update()
  79. {
  80. //Rotate the controller object slightly.
  81. if (rotationAxis.sqrMagnitude > 0f)
  82. {
  83. float degrees = 360f / secondsPerRevolution * Time.deltaTime;
  84. skin.transform.Rotate(rotationAxis, degrees, Space.World);
  85. Mesh mesh = skin.GetFirstControllerMesh();
  86. if (mesh)
  87. {
  88. Vector3 center = mesh.bounds.center;
  89. skin.transform.localPosition = -(skin.transform.localRotation * center);
  90. }
  91. }
  92. }
  93. /// <summary>
  94. /// Change this device's index and change the controller renderer's model accordingly.
  95. /// </summary>
  96. /// <param name="index"></param>
  97. public void SetDeviceIndex(int index)
  98. {
  99. deviceIndex = index;
  100. skin.SetRenderModelIndex(index);
  101. }
  102. public Transform GetTransform()
  103. {
  104. return transform;
  105. }
  106. /// <summary>
  107. /// Invokes the OnTrackedObjectSelected event to signal that the user chose this tracked object.
  108. /// </summary>
  109. /// <param name="clicker"></param>
  110. void IXRClickable.OnClick(ZEDXRGrabber clicker)
  111. {
  112. if(OnTrackedObjectSelected != null)
  113. {
  114. OnTrackedObjectSelected.Invoke(deviceIndex);
  115. }
  116. }
  117. /// <summary>
  118. /// Enables the hover indicator object(s).
  119. /// </summary>
  120. void IXRHoverable.OnHoverStart()
  121. {
  122. foreach(GameObject go in enabledWhenHighlighted)
  123. {
  124. go.SetActive(true);
  125. }
  126. }
  127. /// <summary>
  128. /// Disables the hover indicator object(s).
  129. /// </summary>
  130. void IXRHoverable.OnHoverEnd()
  131. {
  132. foreach (GameObject go in enabledWhenHighlighted)
  133. {
  134. go.SetActive(false);
  135. }
  136. }
  137. }