XRInputManager.cs 13 KB

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