XRInputManager.cs 14 KB

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