CurvedUIHandSwitcher.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Security.Permissions;
  4. namespace CurvedUI {
  5. /// <summary>
  6. /// This script switches the hand controlling the UI when a click on the other controller's trigger is detected.
  7. /// This emulates the functionality seen in SteamVR overlay or Oculus Home.
  8. /// Works both for SteamVR and Oculus SDK.
  9. /// </summary>
  10. public class CurvedUIHandSwitcher : MonoBehaviour
  11. {
  12. #pragma warning disable 0649
  13. #pragma warning disable 414
  14. [SerializeField]
  15. GameObject LaserBeam;
  16. [SerializeField]
  17. [Tooltip("If true, when player clicks the trigger on the other hand, we'll instantly set it as UI controlling hand and move the pointer to it.")]
  18. bool autoSwitchHands = true;
  19. [Header("Optional")]
  20. [SerializeField] [Tooltip("If set, pointer will be placed as a child of this transform, instead of the current VR SDKs Camera Rig.")]
  21. private Transform leftHandOverride;
  22. [SerializeField] [Tooltip("If set, pointer will be placed as a child of this transform, instead of the current VR SDKs Camera Rig.")]
  23. private Transform rightHandOverride;
  24. #pragma warning restore 414
  25. #pragma warning restore 0649
  26. #if CURVEDUI_OCULUSVR
  27. //variables
  28. OVRInput.Controller activeCont;
  29. bool initialized = false;
  30. void Update()
  31. {
  32. if (CurvedUIInputModule.ControlMethod != CurvedUIInputModule.CUIControlMethod.OCULUSVR) return;
  33. activeCont = OVRInput.GetActiveController();
  34. if (!initialized && CurvedUIInputModule.Instance.OculusTouchUsedControllerTransform != null)
  35. {
  36. //Launch Hand Switch. This will place the laser pointer in the current hand.
  37. SwitchHandTo(CurvedUIInputModule.Instance.UsedHand);
  38. initialized = true;
  39. }
  40. //for Oculus Go and GearVR, switch automatically if a different controller is connected.
  41. //This covers the case where User changes hand setting in Oculus Go menu and gets back to our app.
  42. if ((activeCont == OVRInput.Controller.LTouch || activeCont == OVRInput.Controller.LHand)
  43. && CurvedUIInputModule.Instance.UsedHand != CurvedUIInputModule.Hand.Left)
  44. SwitchHandTo(CurvedUIInputModule.Hand.Left);
  45. else if ((activeCont == OVRInput.Controller.RTouch || activeCont == OVRInput.Controller.RHand)
  46. && CurvedUIInputModule.Instance.UsedHand != CurvedUIInputModule.Hand.Right)
  47. SwitchHandTo(CurvedUIInputModule.Hand.Right);
  48. if(autoSwitchHands){
  49. //For Oculus Rift, we wait for the click before we change the pointer.
  50. if (IsButtonDownOnController(OVRInput.Controller.LTouch) && CurvedUIInputModule.Instance.UsedHand != CurvedUIInputModule.Hand.Left)
  51. {
  52. SwitchHandTo(CurvedUIInputModule.Hand.Left);
  53. }
  54. else if (IsButtonDownOnController(OVRInput.Controller.RTouch) && CurvedUIInputModule.Instance.UsedHand != CurvedUIInputModule.Hand.Right)
  55. {
  56. SwitchHandTo(CurvedUIInputModule.Hand.Right);
  57. }
  58. }
  59. }
  60. bool IsButtonDownOnController(OVRInput.Controller cont, OVRInput.Controller cont2 = OVRInput.Controller.None)
  61. {
  62. return OVRInput.GetDown(CurvedUIInputModule.Instance.OculusTouchInteractionButton, cont) || (cont2 != OVRInput.Controller.None && OVRInput.GetDown(CurvedUIInputModule.Instance.OculusTouchInteractionButton, cont2));
  63. }
  64. #elif CURVEDUI_STEAMVR_LEGACY
  65. void Start()
  66. {
  67. //connect to steamVR's OnModelLoaded events so we can update the pointer the moment controller is detected.
  68. CurvedUIInputModule.Right.ModelLoaded += OnModelLoaded;
  69. CurvedUIInputModule.Left.ModelLoaded += OnModelLoaded;
  70. }
  71. void OnModelLoaded(object sender)
  72. {
  73. SwitchHandTo(CurvedUIInputModule.Instance.UsedHand);
  74. }
  75. void Update()
  76. {
  77. if (CurvedUIInputModule.ControlMethod != CurvedUIInputModule.CUIControlMethod.STEAMVR_LEGACY) return;
  78. if(autoSwitchHands){
  79. if (CurvedUIInputModule.Right != null && CurvedUIInputModule.Right.IsTriggerDown && CurvedUIInputModule.Instance.UsedHand != CurvedUIInputModule.Hand.Right)
  80. {
  81. SwitchHandTo(CurvedUIInputModule.Hand.Right);
  82. }
  83. else if (CurvedUIInputModule.Left != null && CurvedUIInputModule.Left.IsTriggerDown && CurvedUIInputModule.Instance.UsedHand != CurvedUIInputModule.Hand.Left)
  84. {
  85. SwitchHandTo(CurvedUIInputModule.Hand.Left);
  86. }
  87. }
  88. }
  89. #elif CURVEDUI_STEAMVR_2
  90. void Start()
  91. {
  92. //initial setup in proper hand
  93. SwitchHandTo(CurvedUIInputModule.Instance.UsedHand);
  94. }
  95. void Update()
  96. {
  97. if (CurvedUIInputModule.ControlMethod != CurvedUIInputModule.CUIControlMethod.STEAMVR_2) return;
  98. //Switch hands during runtime when user clicks the action button on another controller
  99. if (autoSwitchHands && CurvedUIInputModule.Instance.SteamVRClickAction != null)
  100. {
  101. if (CurvedUIInputModule.Instance.SteamVRClickAction.GetState(Valve.VR.SteamVR_Input_Sources.RightHand) && CurvedUIInputModule.Instance.UsedHand != CurvedUIInputModule.Hand.Right){
  102. SwitchHandTo(CurvedUIInputModule.Hand.Right);
  103. }
  104. else if (CurvedUIInputModule.Instance.SteamVRClickAction.GetState(Valve.VR.SteamVR_Input_Sources.LeftHand) && CurvedUIInputModule.Instance.UsedHand != CurvedUIInputModule.Hand.Left ){
  105. SwitchHandTo(CurvedUIInputModule.Hand.Left);
  106. }
  107. }
  108. }
  109. #elif CURVEDUI_UNITY_XR
  110. void Start()
  111. {
  112. //initial setup in proper hand
  113. SwitchHandTo(CurvedUIInputModule.Instance.UsedHand);
  114. }
  115. void Update()
  116. {
  117. if (!autoSwitchHands || CurvedUIInputModule.ControlMethod != CurvedUIInputModule.CUIControlMethod.UNITY_XR) return;
  118. bool pressed = false;
  119. if (CurvedUIInputModule.Instance.RightXRController != null && CurvedUIInputModule.Instance.UsedHand != CurvedUIInputModule.Hand.Right)
  120. {
  121. //get pressed ui button state on right controller.
  122. CurvedUIInputModule.Instance.RightXRController.inputDevice.IsPressed(CurvedUIInputModule.Instance.RightXRController.uiPressUsage,
  123. out pressed, CurvedUIInputModule.Instance.RightXRController.axisToPressThreshold);
  124. if(pressed)
  125. SwitchHandTo(CurvedUIInputModule.Hand.Right);
  126. }
  127. if (CurvedUIInputModule.Instance.LeftXRController != null && CurvedUIInputModule.Instance.UsedHand != CurvedUIInputModule.Hand.Left)
  128. {
  129. //get pressed ui button state on left controller.
  130. CurvedUIInputModule.Instance.LeftXRController.inputDevice.IsPressed(CurvedUIInputModule.Instance.LeftXRController.uiPressUsage,
  131. out pressed, CurvedUIInputModule.Instance.LeftXRController.axisToPressThreshold);
  132. if(pressed)
  133. SwitchHandTo(CurvedUIInputModule.Hand.Left);
  134. }
  135. }
  136. #endif
  137. #region HELPER FUNCTIONS
  138. void SwitchHandTo(CurvedUIInputModule.Hand newHand)
  139. {
  140. CurvedUIInputModule.Instance.UsedHand = newHand;
  141. if (CurvedUIInputModule.Instance.ControllerTransform)
  142. {
  143. //hand overrides
  144. if (newHand == CurvedUIInputModule.Hand.Left && leftHandOverride)
  145. {
  146. CurvedUIInputModule.Instance.PointerTransformOverride = leftHandOverride;
  147. }
  148. if (newHand == CurvedUIInputModule.Hand.Right && rightHandOverride)
  149. {
  150. CurvedUIInputModule.Instance.PointerTransformOverride = rightHandOverride;
  151. }
  152. LaserBeam.transform.SetParent(CurvedUIInputModule.Instance.ControllerTransform);
  153. LaserBeam.transform.ResetTransform();
  154. LaserBeam.transform.LookAt(LaserBeam.transform.position + CurvedUIInputModule.Instance.ControllerPointingDirection);
  155. }
  156. else Debug.LogError("CURVEDUI: No Active controller that can be used as a parent of the pointer. Is the controller gameobject present on the scene and active?");
  157. }
  158. #endregion
  159. }
  160. }