XRInputManager.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. using GxrSdk;
  2. using SC.XR.Unity.Module_InputSystem;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. using UnityEngine.InputSystem;
  8. using UnityEngine.XR;
  9. using UnityEngine.XR.OpenXR.Input;
  10. public class XRInputManager : MonoBehaviour, IGxrControllerPoseHandler, IGxrButtonHandler, IGxrTouchHandler
  11. {
  12. public Transform left = null;
  13. public Transform right = null;
  14. public InputActionProperty inputEvent;
  15. public List<InputActionReference> listinput;
  16. int xy = 8;
  17. // Start is called before the first frame update
  18. void Start()
  19. {
  20. Invoke("startXR", 1f);
  21. }
  22. GameObject RightX3C01Pointer;
  23. GameObject RightX3C01Controller;
  24. void startXR()
  25. {
  26. Module_InputSystem.instance.SetActiveInputDevice(InputDeviceType.KS, true);
  27. Module_InputSystem.instance.SetActiveInputDevice(InputDeviceType.Head, false);
  28. #if UNITY_EDITOR
  29. Module_InputSystem.instance.SetActiveInputDevice(InputDeviceType.KS, false);
  30. Module_InputSystem.instance.SetActiveInputDevice(InputDeviceType.Head, true);
  31. #endif
  32. left.gameObject.SetActive(false);
  33. right.gameObject.SetActive(true);
  34. }
  35. private void Update()
  36. {
  37. if (Input.GetKeyDown(KeyCode.X))
  38. {
  39. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.X, InputKeyState.DOWN);
  40. }
  41. if (Input.GetKeyUp(KeyCode.X))
  42. {
  43. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.X, InputKeyState.UP);
  44. }
  45. }
  46. private void onDeviceDisConnected(UnityEngine.XR.InputDevice obj)
  47. {
  48. if (obj.characteristics.ToString().Contains(InputDeviceCharacteristics.Left.ToString()))
  49. {
  50. Debug.Log("Left===>" + obj.isValid);
  51. left.gameObject.SetActive(obj.isValid);
  52. }
  53. if (obj.characteristics.ToString().Contains(InputDeviceCharacteristics.Right.ToString()))
  54. {
  55. Debug.Log("Right===>" + obj.isValid);
  56. right.gameObject.SetActive(obj.isValid);
  57. }
  58. }
  59. private void OnApplicationPause(bool pause)
  60. {
  61. if (!pause)
  62. {
  63. List<UnityEngine.XR.InputDevice> inputDevices = new List<UnityEngine.XR.InputDevice>();
  64. InputDevices.GetDevices(inputDevices);
  65. for (int i = 0; i < inputDevices.Count; i++)
  66. {
  67. onDeviceConnected(inputDevices[i]);
  68. }
  69. }
  70. }
  71. private void onDeviceConnected(UnityEngine.XR.InputDevice obj)
  72. {
  73. if (obj.characteristics.ToString().Contains(InputDeviceCharacteristics.Left.ToString()))
  74. {
  75. Debug.Log("Left===>" + obj.isValid);
  76. left.gameObject.SetActive(obj.isValid);
  77. }
  78. if (obj.characteristics.ToString().Contains(InputDeviceCharacteristics.Right.ToString()))
  79. {
  80. Debug.Log("Right===>" + obj.isValid);
  81. right.gameObject.SetActive(obj.isValid);
  82. }
  83. }
  84. private void OnCanceledEventL(InputAction.CallbackContext context)
  85. {
  86. Debug.Log("OnCanceledEventL" + context.control.name);
  87. switch (context.control.name)
  88. {
  89. case "secondarytouched":
  90. break;
  91. case "secondarybutton":
  92. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.Y, InputKeyState.UP);
  93. break;
  94. case "primarytouched":
  95. break;
  96. case "primarybutton":
  97. Debug.Log("InputKeyCode.A===》UP");
  98. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.X, InputKeyState.UP);
  99. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.Enter, InputKeyState.UP);
  100. break;
  101. case "touchpadtouched":
  102. break;
  103. case "thumbstickclicked":
  104. break;
  105. case "thumbstick":
  106. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.JoystickX = xy + (int)(context.ReadValue<Vector2>().x * 10);
  107. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.JoystickY = xy + (int)(context.ReadValue<Vector2>().y * 10);
  108. break;
  109. case "grip":
  110. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.HallInside = 10 - (int)((context.ReadValue<float>()) * 10f);
  111. break;
  112. case "grippressed":
  113. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.LHallInside, InputKeyState.UP);
  114. break;
  115. case "triggerButton":
  116. // API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.HallFoward = 10 - (int)((context.ReadValue<float>()) * 10f);
  117. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.Enter, InputKeyState.UP);
  118. break;
  119. case "menu":
  120. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.LFunction, InputKeyState.UP);
  121. break;
  122. }
  123. }
  124. void OnEventL(InputAction.CallbackContext context)
  125. {
  126. Debug.Log("OnEventL" + context.control.name);
  127. switch (context.control.name)
  128. {
  129. case "secondarytouched":
  130. break;
  131. case "secondarybutton":
  132. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.Y, InputKeyState.DOWN);
  133. break;
  134. case "primarytouched":
  135. break;
  136. case "primarybutton":
  137. Debug.Log("InputKeyCode.A===》DOWN");
  138. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.X, InputKeyState.DOWN);
  139. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.Enter, InputKeyState.DOWN);
  140. break;
  141. case "touchpadtouched":
  142. break;
  143. case "thumbstickclicked":
  144. break;
  145. case "thumbstick":
  146. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.JoystickX = xy + (int)(context.ReadValue<Vector2>().x * 10);
  147. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.JoystickY = xy + (int)(context.ReadValue<Vector2>().y * 10);
  148. break;
  149. case "grip":
  150. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.HallInside = 10 - (int)((context.ReadValue<float>()) * 10f);
  151. break;
  152. case "grippressed":
  153. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.LHallInside, InputKeyState.DOWN);
  154. break;
  155. case "triggerButton":
  156. // API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.HallFoward = 10 - (int)((context.ReadValue<float>()) * 10f);
  157. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.Enter, InputKeyState.DOWN);
  158. break;
  159. case "menu":
  160. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.LFunction, InputKeyState.DOWN);
  161. break;
  162. }
  163. }
  164. private void OnCanceledEventR(InputAction.CallbackContext context)
  165. {
  166. Debug.Log("OnCanceledEventR" + context.control.name);
  167. switch (context.control.name)
  168. {
  169. case "secondarytouched":
  170. break;
  171. case "secondarybutton":
  172. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.B, InputKeyState.UP);
  173. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.Enter, InputKeyState.UP);
  174. break;
  175. case "primarytouched":
  176. break;
  177. case "primarybutton":
  178. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.A, InputKeyState.UP);
  179. break;
  180. case "touchpadtouched":
  181. break;
  182. case "thumbstickclicked":
  183. break;
  184. case "thumbstick":
  185. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.JoystickX = xy + (int)(context.ReadValue<Vector2>().x * 10);
  186. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.JoystickY = xy + (int)(context.ReadValue<Vector2>().y * 10);
  187. break;
  188. case "grip":
  189. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.HallInside = 10 - (int)((context.ReadValue<float>()) * 10f);
  190. break;
  191. case "grippressed":
  192. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.RHallInside, InputKeyState.UP);
  193. break;
  194. case "trigger":
  195. // API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.HallFoward = 10 - (int)((context.ReadValue<float>()) * 10f);
  196. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.Enter, InputKeyState.UP);
  197. break;
  198. case "menu":
  199. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.RFunction, InputKeyState.UP);
  200. break;
  201. }
  202. }
  203. void OnEventR(InputAction.CallbackContext context)
  204. {
  205. Debug.Log("OnEventR" + context.control.name);
  206. switch (context.control.name)
  207. {
  208. case "secondarytouched":
  209. break;
  210. case "secondarybutton":
  211. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.B, InputKeyState.DOWN);
  212. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.Enter, InputKeyState.DOWN);
  213. break;
  214. case "primarytouched":
  215. break;
  216. case "primarybutton":
  217. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.A, InputKeyState.DOWN);
  218. break;
  219. case "touchpadtouched":
  220. break;
  221. case "thumbstickclicked":
  222. break;
  223. case "thumbstick":
  224. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.JoystickX = xy + (int)(context.ReadValue<Vector2>().x * 10);
  225. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.JoystickY = xy + (int)(context.ReadValue<Vector2>().y * 10);
  226. break;
  227. case "grip":
  228. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.HallInside = 10 - (int)((context.ReadValue<float>()) * 10f);
  229. break;
  230. case "grippressed":
  231. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.RHallInside, InputKeyState.DOWN);
  232. break;
  233. case "xri_right_trigger":
  234. // API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.HallFoward = 10 - (int)((context.ReadValue<float>()) * 10f);
  235. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.Enter, InputKeyState.DOWN);
  236. break;
  237. case "menu":
  238. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.RFunction, InputKeyState.DOWN);
  239. break;
  240. }
  241. }
  242. public void OnControllerPoseUpdated(GxrControllerPoseEventData eventData)
  243. {
  244. if (right != null)
  245. {
  246. right.rotation = eventData.CurrentRotation;
  247. right.position = new Vector3(OpenXRCamera.Instance.head.transform.position.x, OpenXRCamera.Instance.head.transform.position.y - 0.3f, OpenXRCamera.Instance.head.transform.position.z);
  248. }
  249. }
  250. void OnEnable()
  251. {
  252. // 注册控制器姿态监听器
  253. GxrSystemAccessor.InputSystem?.RegisterHandler<IGxrControllerPoseHandler>(this);
  254. // 注册按键事件监听器
  255. GxrSystemAccessor.InputSystem?.RegisterHandler<IGxrButtonHandler>(this);
  256. // 注册触摸事件监听器
  257. GxrSystemAccessor.InputSystem?.RegisterHandler<IGxrTouchHandler>(this);
  258. }
  259. void OnDisable()
  260. {
  261. // 注销控制器姿态监听器
  262. GxrSystemAccessor.InputSystem?.UnregisterHandler<IGxrControllerPoseHandler>(this);
  263. // 注销按键事件监听器
  264. GxrSystemAccessor.InputSystem?.UnregisterHandler<IGxrButtonHandler>(this);
  265. // 注销触摸事件监听器
  266. GxrSystemAccessor.InputSystem?.UnregisterHandler<IGxrTouchHandler>(this);
  267. }
  268. public void OnButtonDown(GxrButtonEventData eventData)
  269. {
  270. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.Enter, InputKeyState.DOWN);
  271. }
  272. public void OnButtonUp(GxrButtonEventData eventData)
  273. {
  274. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.Enter, InputKeyState.UP);
  275. }
  276. public void OnButtonClick(GxrButtonEventData eventData)
  277. {
  278. }
  279. public void OnButtonLongClick(GxrButtonEventData eventData)
  280. {
  281. }
  282. public void OnButtonDoubleClick(GxrButtonEventData eventData)
  283. {
  284. }
  285. public void OnTouchBegin(GxrTouchEventData eventData)
  286. {
  287. }
  288. public void OnTouchEnd(GxrTouchEventData eventData)
  289. {
  290. }
  291. public void OnTouchSwipe(GxrTouchEventData eventData)
  292. {
  293. }
  294. public void OnTouchPositionUpdated(GxrTouchEventData eventData)
  295. {
  296. }
  297. }