XRInputManager.cs 17 KB

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