OVRControllerHelper.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /************************************************************************************
  2. Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved.
  3. Licensed under the Oculus Utilities SDK License Version 1.31 (the "License"); you may not use
  4. the Utilities SDK except in compliance with the License, which is provided at the time of installation
  5. or download, or which otherwise accompanies this software in either electronic or hard copy form.
  6. You may obtain a copy of the License at
  7. https://developer.oculus.com/licenses/utilities-1.31
  8. Unless required by applicable law or agreed to in writing, the Utilities SDK distributed
  9. under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
  10. ANY KIND, either express or implied. See the License for the specific language governing
  11. permissions and limitations under the License.
  12. ************************************************************************************/
  13. using UnityEngine;
  14. using System.Collections;
  15. /// <summary>
  16. /// Simple helper script that conditionally enables rendering of a controller if it is connected.
  17. /// </summary>
  18. public class OVRControllerHelper : MonoBehaviour
  19. {
  20. /// <summary>
  21. /// The root GameObject that represents the GearVr Controller model.
  22. /// </summary>
  23. public GameObject m_modelGearVrController;
  24. /// <summary>
  25. /// The root GameObject that represents the Oculus Go Controller model.
  26. /// </summary>
  27. public GameObject m_modelOculusGoController;
  28. /// <summary>
  29. /// The root GameObject that represents the Oculus Touch for Quest And RiftS Controller model (Left).
  30. /// </summary>
  31. public GameObject m_modelOculusTouchQuestAndRiftSLeftController;
  32. /// <summary>
  33. /// The root GameObject that represents the Oculus Touch for Quest And RiftS Controller model (Right).
  34. /// </summary>
  35. public GameObject m_modelOculusTouchQuestAndRiftSRightController;
  36. /// <summary>
  37. /// The root GameObject that represents the Oculus Touch for Rift Controller model (Left).
  38. /// </summary>
  39. public GameObject m_modelOculusTouchRiftLeftController;
  40. /// <summary>
  41. /// The root GameObject that represents the Oculus Touch for Rift Controller model (Right).
  42. /// </summary>
  43. public GameObject m_modelOculusTouchRiftRightController;
  44. /// <summary>
  45. /// The controller that determines whether or not to enable rendering of the controller model.
  46. /// </summary>
  47. public OVRInput.Controller m_controller;
  48. private enum ControllerType
  49. {
  50. GearVR, Go, QuestAndRiftS, Rift
  51. }
  52. private ControllerType activeControllerType = ControllerType.Rift;
  53. private bool m_prevControllerConnected = false;
  54. private bool m_prevControllerConnectedCached = false;
  55. void Start()
  56. {
  57. OVRPlugin.SystemHeadset headset = OVRPlugin.GetSystemHeadsetType();
  58. switch (headset)
  59. {
  60. case OVRPlugin.SystemHeadset.Oculus_Go:
  61. activeControllerType = ControllerType.Go;
  62. break;
  63. case OVRPlugin.SystemHeadset.Oculus_Quest:
  64. activeControllerType = ControllerType.QuestAndRiftS;
  65. break;
  66. case OVRPlugin.SystemHeadset.Rift_CV1:
  67. activeControllerType = ControllerType.Rift;
  68. break;
  69. case OVRPlugin.SystemHeadset.Rift_S:
  70. activeControllerType = ControllerType.QuestAndRiftS;
  71. break;
  72. case OVRPlugin.SystemHeadset.GearVR_R320:
  73. case OVRPlugin.SystemHeadset.GearVR_R321:
  74. case OVRPlugin.SystemHeadset.GearVR_R322:
  75. case OVRPlugin.SystemHeadset.GearVR_R323:
  76. case OVRPlugin.SystemHeadset.GearVR_R324:
  77. case OVRPlugin.SystemHeadset.GearVR_R325:
  78. activeControllerType = ControllerType.GearVR;
  79. break;
  80. default:
  81. #if UNITY_EDITOR || !UNITY_ANDROID
  82. activeControllerType = ControllerType.Rift;
  83. #else
  84. activeControllerType = ControllerType.GearVR;
  85. #endif
  86. break;
  87. }
  88. Debug.LogFormat("OVRControllerHelp: Active controller type: {0} for product {1}", activeControllerType, OVRPlugin.productName);
  89. if ((activeControllerType != ControllerType.GearVR) && (activeControllerType != ControllerType.Go))
  90. {
  91. if (m_controller == OVRInput.Controller.LTrackedRemote)
  92. {
  93. m_controller = OVRInput.Controller.LTouch;
  94. }
  95. else if (m_controller == OVRInput.Controller.RTrackedRemote)
  96. {
  97. m_controller = OVRInput.Controller.RTouch;
  98. }
  99. }
  100. else
  101. {
  102. if (m_controller == OVRInput.Controller.LTouch)
  103. {
  104. m_controller = OVRInput.Controller.LTrackedRemote;
  105. }
  106. else if (m_controller == OVRInput.Controller.RTouch)
  107. {
  108. m_controller = OVRInput.Controller.RTrackedRemote;
  109. }
  110. }
  111. }
  112. void Update()
  113. {
  114. bool controllerConnected = OVRInput.IsControllerConnected(m_controller);
  115. if ((controllerConnected != m_prevControllerConnected) || !m_prevControllerConnectedCached)
  116. {
  117. if (activeControllerType == ControllerType.GearVR || activeControllerType == ControllerType.Go)
  118. {
  119. m_modelOculusGoController.SetActive(controllerConnected && (activeControllerType == ControllerType.Go));
  120. m_modelGearVrController.SetActive(controllerConnected && (activeControllerType != ControllerType.Go));
  121. m_modelOculusTouchQuestAndRiftSLeftController.SetActive(false);
  122. m_modelOculusTouchQuestAndRiftSRightController.SetActive(false);
  123. m_modelOculusTouchRiftLeftController.SetActive(false);
  124. m_modelOculusTouchRiftRightController.SetActive(false);
  125. }
  126. else if (activeControllerType == ControllerType.QuestAndRiftS)
  127. {
  128. m_modelOculusGoController.SetActive(false);
  129. m_modelGearVrController.SetActive(false);
  130. m_modelOculusTouchQuestAndRiftSLeftController.SetActive(controllerConnected && (m_controller == OVRInput.Controller.LTouch));
  131. m_modelOculusTouchQuestAndRiftSRightController.SetActive(controllerConnected && (m_controller == OVRInput.Controller.RTouch));
  132. m_modelOculusTouchRiftLeftController.SetActive(false);
  133. m_modelOculusTouchRiftRightController.SetActive(false);
  134. }
  135. else // if (activeControllerType == ControllerType.Rift)
  136. {
  137. m_modelOculusGoController.SetActive(false);
  138. m_modelGearVrController.SetActive(false);
  139. m_modelOculusTouchQuestAndRiftSLeftController.SetActive(false);
  140. m_modelOculusTouchQuestAndRiftSRightController.SetActive(false);
  141. m_modelOculusTouchRiftLeftController.SetActive(controllerConnected && (m_controller == OVRInput.Controller.LTouch));
  142. m_modelOculusTouchRiftRightController.SetActive(controllerConnected && (m_controller == OVRInput.Controller.RTouch));
  143. }
  144. m_prevControllerConnected = controllerConnected;
  145. m_prevControllerConnectedCached = true;
  146. }
  147. if (!controllerConnected)
  148. {
  149. return;
  150. }
  151. }
  152. }