XRInputManager.cs 13 KB

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