XRInputManager.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. using NRKernal;
  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
  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. NRInput.AddDownListener(ControllerHandEnum.Right, ControllerButton.APP, () => {
  21. Debug.Log("ControllerButton.APP===>Down");
  22. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.Enter, InputKeyState.DOWN);
  23. });
  24. NRInput.AddDownListener(ControllerHandEnum.Right, ControllerButton.BEGIN, () => {
  25. Debug.Log("ControllerButton.BEGIN===>Down");
  26. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.Enter, InputKeyState.DOWN);
  27. });
  28. NRInput.AddDownListener(ControllerHandEnum.Right, ControllerButton.END, () => {
  29. Debug.Log("ControllerButton.END===>Down");
  30. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.Enter, InputKeyState.DOWN);
  31. });
  32. NRInput.AddDownListener(ControllerHandEnum.Right, ControllerButton.GRIP, () => {
  33. Debug.Log("ControllerButton.GRIP===>Down");
  34. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.Enter, InputKeyState.DOWN);
  35. });
  36. NRInput.AddDownListener(ControllerHandEnum.Right, ControllerButton.HOME, () => {
  37. Debug.Log("ControllerButton.HOME===>Down");
  38. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.Enter, InputKeyState.DOWN);
  39. });
  40. NRInput.AddDownListener(ControllerHandEnum.Right, ControllerButton.TRIGGER, () => {
  41. Debug.Log("ControllerButton.TRIGGER===>Down");
  42. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.Enter, InputKeyState.DOWN);
  43. });
  44. NRInput.AddDownListener(ControllerHandEnum.Right, ControllerButton.TOUCHPAD_BUTTON, () => {
  45. Debug.Log("ControllerButton.TOUCHPAD_BUTTON===>Down");
  46. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.Enter, InputKeyState.DOWN);
  47. });
  48. NRInput.AddUpListener(ControllerHandEnum.Right, ControllerButton.APP, () => {
  49. Debug.Log("ControllerButton.APP===>UP");
  50. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.Enter, InputKeyState.UP);
  51. });
  52. NRInput.AddUpListener(ControllerHandEnum.Right, ControllerButton.BEGIN, () => {
  53. Debug.Log("ControllerButton.BEGIN===>UP");
  54. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.Enter, InputKeyState.UP);
  55. });
  56. NRInput.AddUpListener(ControllerHandEnum.Right, ControllerButton.END, () => {
  57. Debug.Log("ControllerButton.END===>UP");
  58. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.Enter, InputKeyState.UP);
  59. });
  60. NRInput.AddUpListener(ControllerHandEnum.Right, ControllerButton.GRIP, () => {
  61. Debug.Log("ControllerButton.GRIP===>UP");
  62. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.Enter, InputKeyState.UP);
  63. });
  64. NRInput.AddUpListener(ControllerHandEnum.Right, ControllerButton.HOME, () => {
  65. Debug.Log("ControllerButton.HOME===>UP");
  66. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.Enter, InputKeyState.UP);
  67. });
  68. NRInput.AddUpListener(ControllerHandEnum.Right, ControllerButton.TRIGGER, () => {
  69. Debug.Log("ControllerButton.HOME===>TRIGGER");
  70. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.Enter, InputKeyState.UP);
  71. });
  72. NRInput.AddUpListener(ControllerHandEnum.Right, ControllerButton.TOUCHPAD_BUTTON, () => {
  73. Debug.Log("ControllerButton.HOME===>TOUCHPAD_BUTTON");
  74. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.Enter, InputKeyState.UP);
  75. });
  76. return;
  77. for (int i = 0; i < 15; i++)
  78. {
  79. listinput[i].action.Enable();
  80. listinput[i].action.performed += OnEventL;
  81. listinput[i].action.canceled += OnCanceledEventL;
  82. }
  83. for (int i = 15; i < listinput.Count; i++)
  84. {
  85. listinput[i].action.Enable();
  86. listinput[i].action.performed += OnEventR;
  87. listinput[i].action.canceled += OnCanceledEventR;
  88. }
  89. InputDevices.deviceConnected += onDeviceConnected;
  90. InputDevices.deviceDisconnected += onDeviceDisConnected;
  91. Invoke("startXR", 1f);
  92. }
  93. private void downTP()
  94. {
  95. }
  96. List<Vector3> leftInfo = new List<Vector3>();
  97. List<Vector3> rightInfo = new List<Vector3>();
  98. void startXR()
  99. {
  100. left.gameObject.SetActive(false);
  101. right.gameObject.SetActive(false);
  102. List<UnityEngine.XR.InputDevice> inputDevices = new List<UnityEngine.XR.InputDevice>();
  103. InputDevices.GetDevices(inputDevices);
  104. for (int i = 0; i < inputDevices.Count; i++)
  105. {
  106. onDeviceConnected(inputDevices[i]);
  107. }
  108. }
  109. private void Update()
  110. {
  111. if (Input.GetKeyDown(KeyCode.X))
  112. {
  113. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.X, InputKeyState.DOWN);
  114. }
  115. if (Input.GetKeyUp(KeyCode.X))
  116. {
  117. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.X, InputKeyState.UP);
  118. }
  119. }
  120. private void onDeviceDisConnected(UnityEngine.XR.InputDevice obj)
  121. {
  122. if (obj.characteristics.ToString().Contains(InputDeviceCharacteristics.Left.ToString()))
  123. {
  124. Debug.Log("Left===>" + obj.isValid);
  125. left.gameObject.SetActive(obj.isValid);
  126. }
  127. if (obj.characteristics.ToString().Contains(InputDeviceCharacteristics.Right.ToString()))
  128. {
  129. Debug.Log("Right===>" + obj.isValid);
  130. right.gameObject.SetActive(obj.isValid);
  131. }
  132. }
  133. private void OnApplicationPause(bool pause)
  134. {
  135. if (!pause)
  136. {
  137. List<UnityEngine.XR.InputDevice> inputDevices = new List<UnityEngine.XR.InputDevice>();
  138. InputDevices.GetDevices(inputDevices);
  139. for (int i = 0; i < inputDevices.Count; i++)
  140. {
  141. onDeviceConnected(inputDevices[i]);
  142. }
  143. }
  144. }
  145. private void onDeviceConnected(UnityEngine.XR.InputDevice obj)
  146. {
  147. if (obj.characteristics.ToString().Contains(InputDeviceCharacteristics.Left.ToString()))
  148. {
  149. Debug.Log("Left===>" + obj.isValid);
  150. left.gameObject.SetActive(obj.isValid);
  151. }
  152. if (obj.characteristics.ToString().Contains(InputDeviceCharacteristics.Right.ToString()))
  153. {
  154. Debug.Log("Right===>" + obj.isValid);
  155. right.gameObject.SetActive(obj.isValid);
  156. }
  157. }
  158. private void OnCanceledEventL(InputAction.CallbackContext context)
  159. {
  160. Debug.Log("OnCanceledEventL" + context.control.name);
  161. switch (context.control.name)
  162. {
  163. case "secondarytouched":
  164. break;
  165. case "secondarybutton":
  166. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.Y, InputKeyState.UP);
  167. break;
  168. case "primarytouched":
  169. break;
  170. case "primarybutton":
  171. Debug.Log("InputKeyCode.A===¡·UP");
  172. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.X, InputKeyState.UP);
  173. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.Enter, InputKeyState.UP);
  174. break;
  175. case "touchpadtouched":
  176. break;
  177. case "thumbstickclicked":
  178. break;
  179. case "thumbstick":
  180. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.JoystickX = xy + (int)(context.ReadValue<Vector2>().x * 10);
  181. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.JoystickY = xy + (int)(context.ReadValue<Vector2>().y * 10);
  182. break;
  183. case "grip":
  184. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.HallInside = 10 - (int)((context.ReadValue<float>()) * 10f);
  185. break;
  186. case "grippressed":
  187. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.LHallInside, InputKeyState.UP);
  188. break;
  189. case "trigger":
  190. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.HallFoward = 10 - (int)((context.ReadValue<float>()) * 10f);
  191. break;
  192. case "menu":
  193. Module_InputSystem.instance.SetActiveInputDevice(InputDeviceType.KS, true);
  194. Module_InputSystem.instance.SetActiveInputDevice(InputDeviceType.Head, false);
  195. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.LFunction, InputKeyState.UP);
  196. break;
  197. }
  198. }
  199. void OnEventL(InputAction.CallbackContext context)
  200. {
  201. Debug.Log("OnEventL" + context.control.name);
  202. switch (context.control.name)
  203. {
  204. case "secondarytouched":
  205. break;
  206. case "secondarybutton":
  207. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.Y, InputKeyState.DOWN);
  208. break;
  209. case "primarytouched":
  210. break;
  211. case "primarybutton":
  212. Debug.Log("InputKeyCode.A===¡·DOWN");
  213. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.X, InputKeyState.DOWN);
  214. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.Enter, InputKeyState.DOWN);
  215. break;
  216. case "touchpadtouched":
  217. break;
  218. case "thumbstickclicked":
  219. break;
  220. case "thumbstick":
  221. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.JoystickX = xy + (int)(context.ReadValue<Vector2>().x * 10);
  222. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.JoystickY = xy + (int)(context.ReadValue<Vector2>().y * 10);
  223. break;
  224. case "grip":
  225. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.HallInside = 10 - (int)((context.ReadValue<float>()) * 10f);
  226. break;
  227. case "grippressed":
  228. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.LHallInside, InputKeyState.DOWN);
  229. break;
  230. case "trigger":
  231. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.HallFoward = 10 - (int)((context.ReadValue<float>()) * 10f);
  232. break;
  233. case "menu":
  234. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.LFunction, InputKeyState.DOWN);
  235. break;
  236. }
  237. }
  238. private void OnCanceledEventR(InputAction.CallbackContext context)
  239. {
  240. Debug.Log("OnCanceledEventR" + context.control.name);
  241. switch (context.control.name)
  242. {
  243. case "secondarytouched":
  244. break;
  245. case "secondarybutton":
  246. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.B, InputKeyState.UP);
  247. break;
  248. case "primarytouched":
  249. break;
  250. case "primarybutton":
  251. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.A, InputKeyState.UP);
  252. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.Enter, InputKeyState.UP);
  253. break;
  254. case "touchpadtouched":
  255. break;
  256. case "thumbstickclicked":
  257. break;
  258. case "thumbstick":
  259. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.JoystickX = xy + (int)(context.ReadValue<Vector2>().x * 10);
  260. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.JoystickY = xy + (int)(context.ReadValue<Vector2>().y * 10);
  261. break;
  262. case "grip":
  263. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.HallInside = 10 - (int)((context.ReadValue<float>()) * 10f);
  264. break;
  265. case "grippressed":
  266. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.RHallInside, InputKeyState.UP);
  267. break;
  268. case "trigger":
  269. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.HallFoward = 10 - (int)((context.ReadValue<float>()) * 10f);
  270. break;
  271. case "menu":
  272. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.RFunction, InputKeyState.UP);
  273. break;
  274. }
  275. }
  276. void OnEventR(InputAction.CallbackContext context)
  277. {
  278. Debug.Log("OnEventR" + context.control.name);
  279. switch (context.control.name)
  280. {
  281. case "secondarytouched":
  282. break;
  283. case "secondarybutton":
  284. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.B, InputKeyState.DOWN);
  285. break;
  286. case "primarytouched":
  287. break;
  288. case "primarybutton":
  289. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.A, InputKeyState.DOWN);
  290. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.Enter, InputKeyState.DOWN);
  291. break;
  292. case "touchpadtouched":
  293. break;
  294. case "thumbstickclicked":
  295. break;
  296. case "thumbstick":
  297. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.JoystickX = xy + (int)(context.ReadValue<Vector2>().x * 10);
  298. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.JoystickY = xy + (int)(context.ReadValue<Vector2>().y * 10);
  299. break;
  300. case "grip":
  301. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.HallInside = 10 - (int)((context.ReadValue<float>()) * 10f);
  302. break;
  303. case "grippressed":
  304. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.RHallInside, InputKeyState.DOWN);
  305. break;
  306. case "trigger":
  307. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.HallFoward = 10 - (int)((context.ReadValue<float>()) * 10f);
  308. break;
  309. case "menu":
  310. Module_InputSystem.instance.SetActiveInputDevice(InputDeviceType.KS, true);
  311. Module_InputSystem.instance.SetActiveInputDevice(InputDeviceType.Head, false);
  312. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.RFunction, InputKeyState.DOWN);
  313. break;
  314. }
  315. }
  316. // Debug.Log("OnEvent=name==>" + context.control.name);
  317. /*
  318. Debug.Log("OnEvent=layout==>" + context.control.layout);
  319. Debug.Log("OnEvent=noisy==>" + context.control.noisy);
  320. Debug.Log("OnEvent=path==>" + context.control.path);
  321. Debug.Log("OnEvent=shortDisplayName==>" + context.control.shortDisplayName);
  322. Debug.Log("OnEvent=synthetic==>" + context.control.synthetic);
  323. Debug.Log("OnEvent=valueSizeInBytes==>" + context.control.valueSizeInBytes);
  324. Debug.Log("OnEvent=valueType==>" + context.control.valueType.Name);
  325. Debug.Log("OnEvent=variants==>" + context.control.variants);
  326. Debug.Log("OnEvent=variants==>" + context.control.EvaluateMagnitude());*/
  327. }