CVR_InteractableVRTK.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. namespace ChivaXR.VR
  2. {
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using VRTK;
  7. [RequireComponent(typeof(CVR_ToolTypeFlags))]
  8. public class CVR_InteractableVRTK : VRTK_InteractableObject, IVR_Interactable
  9. {
  10. private GameObject interactableCurrentHand;
  11. #region 接口IVR_Interactable
  12. /// <summary>
  13. /// 当前工具类型
  14. /// </summary>
  15. public CVR_ToolType toolType
  16. {
  17. get
  18. {
  19. if (toolTypeFlags == null)
  20. {
  21. toolTypeFlags = this.GetComponent<CVR_ToolTypeFlags>();
  22. }
  23. return toolTypeFlags.CVR_ToolType;
  24. }
  25. }
  26. private CVR_ToolTypeFlags toolTypeFlags;
  27. /// <summary>
  28. /// 获取当前工具类型
  29. /// </summary>
  30. /// <returns></returns>
  31. public CVR_ToolType GetToolType()
  32. {
  33. return toolType;
  34. }
  35. public event CVR_InteractableObjectEventHandler CVR_InteractableObjectGrabbed;
  36. public event CVR_InteractableObjectEventHandler CVR_InteractableObjectPreUnGrabbed;
  37. public event CVR_InteractableObjectEventHandler CVR_InteractableObjectUsed;
  38. public event CVR_InteractableObjectEventHandler CVR_InteractableObjectUnused;
  39. protected override void Awake()
  40. {
  41. base.Awake();
  42. CVR_ToolManager.Instance.RegisterTool(toolType, this);
  43. }
  44. /// <summary>
  45. /// 检测按键状态
  46. /// </summary>
  47. /// <param name="btnType"></param>
  48. /// <param name="pressType"></param>
  49. /// <param name="controllerHand"></param>
  50. /// <returns></returns>
  51. public bool CVR_CheckButtonState(CVR_ButtonTypes btnType, CVR_ButtonPressTypes pressType)
  52. {
  53. return ChivaVRToVRTK.CheckButtonState(btnType, pressType, CVR_GetCurrentHand());
  54. }
  55. public Vector2 CVR_GetControllerAxis(CVR_ButtonTypes btnType)
  56. {
  57. return ChivaVRToVRTK.GetControllerAxis(btnType, CVR_GetCurrentHand());
  58. }
  59. /// <summary>
  60. /// 获取当前手部类型
  61. /// </summary>
  62. /// <returns></returns>
  63. public CVR_ControllerHand CVR_GetCurrentHand()
  64. {
  65. return ChivaVRToVRTK.GetChivaVR_ControllerHand(VRTK_DeviceFinder.GetControllerHand(interactableCurrentHand));
  66. }
  67. public GameObject CVR_GetHeadsetCamera()
  68. {
  69. return VRTK_SDK_Bridge.GetHeadsetCamera().gameObject;
  70. }
  71. public GameObject CVR_GetInteractableObject()
  72. {
  73. return interactableCurrentHand;
  74. }
  75. public GameObject CVR_GetCurrentObject()
  76. {
  77. return this.gameObject;
  78. }
  79. public bool CVR_IsGrabbed()
  80. {
  81. return IsGrabbed();
  82. }
  83. public bool CVR_IsUsing()
  84. {
  85. return IsUsing();
  86. }
  87. public void ForceStopUsing()
  88. {
  89. GetUsingScript().ForceStopUsing();
  90. }
  91. #endregion
  92. #region VRTK
  93. public override void Grabbed(VRTK_InteractGrab currentGrabbingObject = null)
  94. {
  95. base.Grabbed(currentGrabbingObject);
  96. interactableCurrentHand = currentGrabbingObject.gameObject;
  97. OnChivaVRInteractableObjectGrabbde(SetChivaVRInteractableObjectEvent(interactableCurrentHand));
  98. }
  99. public override void Ungrabbed(VRTK_InteractGrab previousGrabbingObject = null)
  100. {
  101. GameObject previousGrabbingGameObject = (previousGrabbingObject != null ? previousGrabbingObject.gameObject : null);
  102. OnChivaVRInteractableObjectPreUnGrabbde(SetChivaVRInteractableObjectEvent(previousGrabbingGameObject));
  103. base.Ungrabbed(previousGrabbingObject);
  104. interactableCurrentHand = null;
  105. }
  106. public override void StartUsing(VRTK_InteractUse currentUsingObject = null)
  107. {
  108. OnChivaVRInteractableObjectUsed(SetChivaVRInteractableObjectEvent(interactableCurrentHand));
  109. base.StartUsing(currentUsingObject);
  110. }
  111. public override void StopUsing(VRTK_InteractUse previousUsingObject = null, bool resetUsingObjectState = true)
  112. {
  113. OnChivaVRInteractableObjectUnUsed(SetChivaVRInteractableObjectEvent(interactableCurrentHand));
  114. base.StopUsing(previousUsingObject, resetUsingObjectState);
  115. }
  116. public virtual void OnChivaVRInteractableObjectPreUnGrabbde(CVR_InteractableObjectEventArgs e)
  117. {
  118. CVR_InteractableObjectPreUnGrabbed?.Invoke(this, e);
  119. }
  120. public virtual void OnChivaVRInteractableObjectGrabbde(CVR_InteractableObjectEventArgs e)
  121. {
  122. CVR_InteractableObjectGrabbed?.Invoke(this, e);
  123. }
  124. public virtual void OnChivaVRInteractableObjectUsed(CVR_InteractableObjectEventArgs e)
  125. {
  126. CVR_InteractableObjectUsed?.Invoke(this, e);
  127. }
  128. public virtual void OnChivaVRInteractableObjectUnUsed(CVR_InteractableObjectEventArgs e)
  129. {
  130. CVR_InteractableObjectUnused?.Invoke(this, e);
  131. }
  132. public CVR_InteractableObjectEventArgs SetChivaVRInteractableObjectEvent(GameObject interactingObject)
  133. {
  134. CVR_InteractableObjectEventArgs e;
  135. e.interactingObject = interactingObject;
  136. return e;
  137. }
  138. #endregion
  139. }
  140. /// <summary>
  141. /// ChivaVR跟VRTK的转换工具
  142. /// </summary>
  143. public class ChivaVRToVRTK
  144. {
  145. /// <summary>
  146. /// 检测按钮状态
  147. /// </summary>
  148. /// <param name="btnType">按键类型</param>
  149. /// <param name="pressType">按下类型</param>
  150. /// <param name="controllerHand">手部类型</param>
  151. /// <returns></returns>
  152. public static bool CheckButtonState(CVR_ButtonTypes btnType, CVR_ButtonPressTypes pressType, CVR_ControllerHand controllerHand)
  153. {
  154. return VRTK_SDK_Bridge.GetControllerButtonState(GetButtonTypes(btnType), GetPressTypes(pressType)
  155. , VRTK_ControllerReference.GetControllerReference(GetControllerHand(controllerHand)));
  156. }
  157. /// <summary>
  158. /// 检测按钮状态
  159. /// </summary>
  160. /// <param name="btnType">按键类型</param>
  161. /// <param name="pressType">按下类型</param>
  162. /// <param name="controllerHand">手部类型</param>
  163. /// <returns></returns>
  164. public static Vector2 GetControllerAxis(CVR_ButtonTypes btnType, CVR_ControllerHand controllerHand)
  165. {
  166. return VRTK_SDK_Bridge.GetControllerAxis(GetButtonTypes(btnType),
  167. VRTK_ControllerReference.GetControllerReference(GetControllerHand(controllerHand)));
  168. }
  169. /// <summary>
  170. /// 转化按键类型
  171. /// </summary>
  172. /// <param name="types"></param>
  173. /// <returns></returns>
  174. public static SDK_BaseController.ButtonTypes GetButtonTypes(CVR_ButtonTypes types)
  175. {
  176. switch (types)
  177. {
  178. case CVR_ButtonTypes.ButtonOne:
  179. return SDK_BaseController.ButtonTypes.ButtonOne;
  180. case CVR_ButtonTypes.ButtonTwo:
  181. return SDK_BaseController.ButtonTypes.ButtonTwo;
  182. case CVR_ButtonTypes.Grip:
  183. return SDK_BaseController.ButtonTypes.Grip;
  184. case CVR_ButtonTypes.Trigger:
  185. return SDK_BaseController.ButtonTypes.Trigger;
  186. case CVR_ButtonTypes.Touchpad:
  187. return SDK_BaseController.ButtonTypes.Touchpad;
  188. case CVR_ButtonTypes.TouchpadTwo:
  189. return SDK_BaseController.ButtonTypes.TouchpadTwo;
  190. }
  191. return SDK_BaseController.ButtonTypes.GripHairline;
  192. }
  193. public static SDK_BaseController.ButtonPressTypes GetPressTypes(CVR_ButtonPressTypes pressTypes)
  194. {
  195. switch (pressTypes)
  196. {
  197. case CVR_ButtonPressTypes.Press:
  198. return SDK_BaseController.ButtonPressTypes.Press;
  199. case CVR_ButtonPressTypes.PressDown:
  200. return SDK_BaseController.ButtonPressTypes.PressDown;
  201. case CVR_ButtonPressTypes.PressUp:
  202. return SDK_BaseController.ButtonPressTypes.PressUp;
  203. case CVR_ButtonPressTypes.Touch:
  204. return SDK_BaseController.ButtonPressTypes.Touch;
  205. case CVR_ButtonPressTypes.TouchDown:
  206. return SDK_BaseController.ButtonPressTypes.TouchDown;
  207. case CVR_ButtonPressTypes.TouchUp:
  208. return SDK_BaseController.ButtonPressTypes.TouchUp;
  209. default:
  210. return SDK_BaseController.ButtonPressTypes.Touch;
  211. }
  212. }
  213. public static SDK_BaseController.ControllerHand GetControllerHand(CVR_ControllerHand controllerHand)
  214. {
  215. switch (controllerHand)
  216. {
  217. case CVR_ControllerHand.None:
  218. return SDK_BaseController.ControllerHand.None;
  219. case CVR_ControllerHand.Left:
  220. return SDK_BaseController.ControllerHand.Left;
  221. case CVR_ControllerHand.Right:
  222. return SDK_BaseController.ControllerHand.Right;
  223. default:
  224. return SDK_BaseController.ControllerHand.None;
  225. }
  226. }
  227. public static CVR_ControllerHand GetChivaVR_ControllerHand(SDK_BaseController.ControllerHand controllerHand)
  228. {
  229. switch (controllerHand)
  230. {
  231. case SDK_BaseController.ControllerHand.None:
  232. return CVR_ControllerHand.None;
  233. case SDK_BaseController.ControllerHand.Left:
  234. return CVR_ControllerHand.Left;
  235. case SDK_BaseController.ControllerHand.Right:
  236. return CVR_ControllerHand.Right;
  237. default:
  238. return CVR_ControllerHand.None;
  239. }
  240. }
  241. }
  242. }