XRInputManager.cs 17 KB

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