ControllerInputFromAction.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. using SC.InputSystem;
  2. using SC.InputSystem.InputDeviceHandShank;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using UnityEngine;
  7. using UnityEngine.InputSystem;
  8. using UnityEngine.XR;
  9. using WCQ.CommonV1_20200325;
  10. public class ControllerInputFromAction : MonoBehaviour
  11. {
  12. public ControllerIndex index = ControllerIndex.Left;
  13. public List<InputActionReference> listinput;
  14. public List<InputActionReference> listtrack;
  15. public Transform Controller
  16. {
  17. get {
  18. return GetController(index)?.inputDeviceHandShankUI.transform;
  19. }
  20. }
  21. private void Start() {
  22. SC.InputSystem.InputSystem.ISInitializeCallBack += InputSystemVaildCallBack;
  23. }
  24. private void OnDestroy() {
  25. SC.InputSystem.InputSystem.ISInitializeCallBack -= InputSystemVaildCallBack;
  26. }
  27. private void Update()
  28. {
  29. //GetController(index)?.gameObject.SetActive(true);
  30. //if (index == ControllerIndex.Left)
  31. // InputDataHandShank.HSData.HS0Connect = true;
  32. //else
  33. // InputDataHandShank.HSData.HS1Connect = true;
  34. //if (GetController(index))
  35. //{
  36. // GetController(index).inputDataHandShank.blueToothName = "K07";
  37. // GetController(index).inputDataHandShank.handShankType = HandShankType.K07;
  38. // GetController(index).inputDataHandShank.isVaild = true;
  39. //}
  40. //Debug.Log("LateUpdate " + GetController(index).inputDataHandShank.rotation);
  41. }
  42. void InputSystemVaildCallBack()
  43. {
  44. Invoke("DelayInvoke", 1);
  45. }
  46. void DelayInvoke() {
  47. Debug.Log("InputSystem Initialize ===" + index + " ");
  48. var HandedControllers = new List<UnityEngine.XR.InputDevice>();
  49. var desiredCharacteristics = (index == ControllerIndex.Left ? UnityEngine.XR.InputDeviceCharacteristics.Left : UnityEngine.XR.InputDeviceCharacteristics.Right) | UnityEngine.XR.InputDeviceCharacteristics.Controller;
  50. UnityEngine.XR.InputDevices.GetDevicesWithCharacteristics(desiredCharacteristics, HandedControllers);
  51. Debug.Log(HandedControllers.Count + " ===========");
  52. foreach (var device in HandedControllers)
  53. {
  54. Debug.Log(string.Format("Device name '{0}' has characteristics '{1}'", device.name, device.characteristics.ToString()));
  55. }
  56. if (HandedControllers.Count == 1)
  57. {
  58. onDeviceConnected(HandedControllers[0]);
  59. }
  60. if (Application.platform != RuntimePlatform.Android)
  61. {
  62. onDeviceConnected(new UnityEngine.XR.InputDevice());
  63. }
  64. InputDevices.deviceConnected += onDeviceConnected;
  65. InputDevices.deviceDisconnected += onDeviceDisConnected;
  66. }
  67. private void onDeviceDisConnected(UnityEngine.XR.InputDevice obj) {
  68. if (Application.platform == RuntimePlatform.Android)
  69. {
  70. if (obj.characteristics.ToString().Contains(InputDeviceCharacteristics.Left.ToString()))
  71. {
  72. Debug.Log("Left onDeviceDisConnected===>" + index);
  73. InputDataHandShank.HSData.HS0Connect = false;
  74. GetController(ControllerIndex.Left)?.gameObject.SetActive(false);
  75. GetController(index).inputDataHandShank.blueToothName = "K07";
  76. GetController(index).inputDataHandShank.handShankType = HandShankType.K07;
  77. GetController(index).inputDataHandShank.isVaild = false;
  78. GetController(index).inputDataGetHandShank.inputDataGetHandShank3DOF.updateRotationCallBack -= updateRotationCallBack;
  79. }
  80. if (obj.characteristics.ToString().Contains(InputDeviceCharacteristics.Right.ToString()))
  81. {
  82. Debug.Log("Right onDeviceDisConnected===>" + index);
  83. InputDataHandShank.HSData.HS1Connect = false;
  84. GetController(ControllerIndex.Right)?.gameObject.SetActive(false);
  85. GetController(index).inputDataHandShank.blueToothName = "K07";
  86. GetController(index).inputDataHandShank.handShankType = HandShankType.K07;
  87. GetController(index).inputDataHandShank.isVaild = false;
  88. GetController(index).inputDataGetHandShank.inputDataGetHandShank3DOF.updateRotationCallBack -= updateRotationCallBack;
  89. }
  90. }
  91. else {
  92. GetController(index)?.gameObject.SetActive(false);
  93. if (index == ControllerIndex.Left)
  94. InputDataHandShank.HSData.HS0Connect = false;
  95. else
  96. InputDataHandShank.HSData.HS1Connect = false;
  97. GetController(index).inputDataHandShank.blueToothName = "K07";
  98. GetController(index).inputDataHandShank.handShankType = HandShankType.K07;
  99. GetController(index).inputDataHandShank.isVaild = false;
  100. GetController(index).inputDataGetHandShank.inputDataGetHandShank3DOF.updateRotationCallBack -= updateRotationCallBack;
  101. }
  102. foreach ( var inputactionreference in listinput ) {
  103. inputactionreference.action.Disable();
  104. inputactionreference.action.performed -= OnInputPerformed;
  105. inputactionreference.action.canceled -= OnInputCanceled;
  106. }
  107. foreach ( var inputactionreference in listtrack ) {
  108. inputactionreference.action.Disable();
  109. inputactionreference.action.performed -= OnTrackPerformed;
  110. //inputactionreference.action.canceled -= OnTrackCanceled;
  111. }
  112. }
  113. private void onDeviceConnected(UnityEngine.XR.InputDevice obj)
  114. {
  115. if (Application.platform == RuntimePlatform.Android)
  116. {
  117. if (obj.characteristics.ToString().Contains(InputDeviceCharacteristics.Left.ToString()))
  118. {
  119. Debug.Log("Left onDeviceConnected===>" + index);
  120. InputDataHandShank.HSData.HS0Connect = true;
  121. GetController(ControllerIndex.Left)?.gameObject.SetActive(true);
  122. GetController(index).inputDataHandShank.blueToothName = "K07";
  123. GetController(index).inputDataHandShank.handShankType = HandShankType.K07;
  124. GetController(index).inputDataHandShank.isVaild = true;
  125. GetController(index).inputDataGetHandShank.inputDataGetHandShank3DOF.updateRotationCallBack += updateRotationCallBack;
  126. }
  127. if (obj.characteristics.ToString().Contains(InputDeviceCharacteristics.Right.ToString()))
  128. {
  129. Debug.Log("Right onDeviceConnected===>" + index);
  130. InputDataHandShank.HSData.HS1Connect = true;
  131. GetController(ControllerIndex.Right)?.gameObject.SetActive(true);
  132. GetController(index).inputDataHandShank.blueToothName = "K07";
  133. GetController(index).inputDataHandShank.handShankType = HandShankType.K07;
  134. GetController(index).inputDataHandShank.isVaild = true;
  135. GetController(index).inputDataGetHandShank.inputDataGetHandShank3DOF.updateRotationCallBack += updateRotationCallBack;
  136. }
  137. }
  138. else
  139. {
  140. GetController(index)?.gameObject.SetActive(true);
  141. if (index == ControllerIndex.Left)
  142. InputDataHandShank.HSData.HS0Connect = true;
  143. else
  144. InputDataHandShank.HSData.HS1Connect = true;
  145. GetController(index).inputDataHandShank.blueToothName = "K07";
  146. GetController(index).inputDataHandShank.handShankType = HandShankType.K07;
  147. GetController(index).inputDataHandShank.isVaild = true;
  148. GetController(index).inputDataGetHandShank.inputDataGetHandShank3DOF.updateRotationCallBack += updateRotationCallBack;
  149. }
  150. foreach ( var inputactionreference in listinput ) {
  151. inputactionreference.action.Enable();
  152. inputactionreference.action.performed += OnInputPerformed;
  153. inputactionreference.action.canceled += OnInputCanceled;
  154. }
  155. foreach ( var inputactionreference in listtrack ) {
  156. inputactionreference.action.Enable();
  157. inputactionreference.action.performed += OnTrackPerformed;
  158. // inputactionreference.action.canceled += OnTrackCanceled;
  159. }
  160. }
  161. public Quaternion controllerRotation { get; private set; }
  162. public Vector3 controllerPosition { get; private set; }
  163. void OnTrackPerformed(InputAction.CallbackContext context) {
  164. //Debug.Log(Time.frameCount+" OnTrackPerformed" + index + " " + context.control.name);
  165. switch ( context.control.name ) {
  166. case "deviceposition":
  167. case "devicePosition":
  168. controllerPosition = context.ReadValue<Vector3>();
  169. break;
  170. case "devicerotation":
  171. case "deviceRotation":
  172. //Debug.Log(Time.frameCount + " OnTrackPerformed" + index + " " + context.control.name+" "+context.ReadValue<Quaternion>());
  173. // GetController(index).inputDataHandShank.rotation = context.ReadValue<Quaternion>();
  174. controllerRotation = context.ReadValue<Quaternion>();
  175. break;
  176. }
  177. }
  178. //private void OnTrackCanceled(InputAction.CallbackContext context) {
  179. // Debug.Log("OnTrackCanceled" + index + " " + context.control.name);
  180. // switch ( context.control.name ) {
  181. // case "devicePosition":
  182. // break;
  183. // case "deviceRotation":
  184. // GetController(index)?.inputDataBase.InputDataAddKey(InputKeyCode.Enter, InputKeyState.UP);
  185. // break;
  186. // }
  187. //}
  188. void updateRotationCallBack() {
  189. Debug.Log(index+" updateRotationCallBack===>");
  190. GetController(index).inputDataHandShank.rotation = controllerRotation;
  191. GetController(index).inputDataHandShank.position = new Vector3( controllerPosition.x, controllerPosition.y+1, controllerPosition.z);
  192. }
  193. private void LateUpdate()
  194. {
  195. //Debug.Log("LateUpdate " + GetController(index).inputDataHandShank.rotation);
  196. if (GetController(index))
  197. {
  198. // GetController(index).inputDeviceHandShankUI.transform.rotation = controllerRotation;
  199. //GetController(index).inputDeviceHandShankUI.model.lineIndicate.transform.rotation = controllerRotation;
  200. //if (GetController(index).inputDeviceUIBase)
  201. //{
  202. // GetController(index).inputDeviceUIBase.BeforTargetDetecterUpdateUI();
  203. //}
  204. //if (GetController(index).inputDataProcessBase != null)
  205. //{
  206. // GetController(index).inputDataProcessBase.OnUpdateAfterUI();
  207. //}
  208. ///没有UI,没必要执行TargetDecter的必要
  209. //if (GetController(index).inputDeviceUIBase != null && GetController(index).inputDeviceTargetDetecterBase != null)
  210. //{
  211. // GetController(index).inputDeviceTargetDetecterBase.OnUpdate();
  212. //}
  213. //if (GetController(index).inputDeviceUIBase)
  214. //{
  215. // GetController(index).inputDeviceUIBase.AfterTargetDetecterUpdateUI();
  216. //}
  217. //GetController(index).inputDeviceHandShankUI.modelK07.transform.rotation = controllerRotation;
  218. //if (GetController(index).inputDataBase != null)
  219. //{
  220. // GetController(index).inputDataBase.OnUpdate();
  221. //}
  222. //if (GetController(index).inputDataGetBase != null)
  223. //{
  224. // GetController(index).inputDataGetBase.OnUpdate();
  225. //}
  226. //GetController(index).inputDataHandShank.rotation = controllerRotation;
  227. //GetController(index).inputDataHandShank.position = controllerPosition;
  228. //if (GetController(index).inputDevicePartStatusBase != null)
  229. //{
  230. // GetController(index).inputDevicePartStatusBase.OnUpdate();
  231. //}
  232. /////如果数据无效或不可获取,则关闭UI并退出
  233. //if (GetController(index).inputDataBase.isVaild == false)
  234. //{
  235. // GetController(index).inputDeviceUIBase.UIDisable();
  236. // DebugMy.Log("inputDataBase.isVaild == false ,Turn Off UI and Return", this);
  237. // return;
  238. //}
  239. //else
  240. //{
  241. // DebugMy.Log("inputDataBase.isVaild == true", this);
  242. // GetController(index).inputDeviceUIBase.UIEnable();
  243. //}
  244. /////一些UI坐标值刷新必须在UI更新前执行完
  245. //if (GetController(index).inputDataProcessBase != null)
  246. //{
  247. // GetController(index).inputDataProcessBase.OnUpdateBeforUI();
  248. //}
  249. //if (GetController(index).inputDeviceUIBase)
  250. //{
  251. // GetController(index).inputDeviceUIBase.BeforTargetDetecterUpdateUI();
  252. //}
  253. //if (GetController(index).inputDataProcessBase != null)
  254. //{
  255. // GetController(index).inputDataProcessBase.OnUpdateAfterUI();
  256. //}
  257. /////没有UI,没必要执行TargetDecter的必要
  258. //if (GetController(index).inputDeviceUIBase != null && GetController(index).inputDeviceTargetDetecterBase != null)
  259. //{
  260. // GetController(index).inputDeviceTargetDetecterBase.OnUpdate();
  261. //}
  262. //if (GetController(index).inputDeviceUIBase)
  263. //{
  264. // GetController(index).inputDeviceUIBase.AfterTargetDetecterUpdateUI();
  265. //} ///一些UI坐标值刷新必须在UI更新前执行完
  266. //if (GetController(index) != null)
  267. //{
  268. // GetController(index).inputDeviceUIBase.model.lineIndicate.DrawLineIndicate() ;
  269. //}
  270. }
  271. }
  272. int xy = 8;
  273. void OnInputPerformed(InputAction.CallbackContext context) {
  274. Debug.Log("OnInput================>" + index + " " + context.control.name);
  275. switch ( context.control.name ) {
  276. case "secondarytouched":
  277. break;
  278. case "secondarybutton":
  279. GetController(index)?.inputDataBase.InputDataAddKey(InputKeyCode.Enter, InputKeyState.DOWN);
  280. break;
  281. case "primarytouched":
  282. break;
  283. case "primarybutton":
  284. break;
  285. case "touchpadtouched":
  286. break;
  287. case "thumbstickclicked":
  288. GetController(index)?.inputDataBase.InputDataAddKey(InputKeyCode.Tp, InputKeyState.DOWN);
  289. break;
  290. case "thumbstick":
  291. GetController(index)?.inputDataHandShank.InputDataAddTouch(true,
  292. new Vector2(xy + (int)(context.ReadValue<Vector2>().x * 10), xy + (int)(context.ReadValue<Vector2>().y * 10)));
  293. break;
  294. case "grip":
  295. break;
  296. case "grippressed":
  297. GetController(index)?.inputDataBase.InputDataAddKey(InputKeyCode.Enter, InputKeyState.DOWN);
  298. break;
  299. case "triggerpressed":
  300. GetController(index)?.inputDataBase.InputDataAddKey(InputKeyCode.Enter, InputKeyState.DOWN);
  301. break;
  302. case "menu":
  303. GetController(index)?.inputDataBase.InputDataAddKey(InputKeyCode.Function, InputKeyState.DOWN);
  304. break;
  305. case "triggerButton":
  306. GetController(index)?.inputDataBase.InputDataAddKey(InputKeyCode.Enter, InputKeyState.DOWN);
  307. break;
  308. case "gripButton":
  309. GetController(index)?.inputDataBase.InputDataAddKey(InputKeyCode.Enter, InputKeyState.DOWN);
  310. break;
  311. }
  312. }
  313. private void OnInputCanceled(InputAction.CallbackContext context) {
  314. Debug.Log("OnInput================>" + index + " " + context.control.name);
  315. switch ( context.control.name ) {
  316. case "secondarytouched":
  317. break;
  318. case "secondarybutton":
  319. GetController(index)?.inputDataBase.InputDataAddKey(InputKeyCode.Enter, InputKeyState.UP);
  320. break;
  321. case "primarytouched":
  322. break;
  323. case "primarybutton":
  324. break;
  325. case "touchpadtouched":
  326. break;
  327. case "thumbstickclicked":
  328. GetController(index)?.inputDataBase.InputDataAddKey(InputKeyCode.Tp, InputKeyState.UP);
  329. break;
  330. case "thumbstick":
  331. GetController(index)?.inputDataHandShank.InputDataAddTouch(true,
  332. new Vector2(xy + (int)(context.ReadValue<Vector2>().x * 10), xy + (int)(context.ReadValue<Vector2>().y * 10)));
  333. break;
  334. case "grip":
  335. break;
  336. case "grippressed":
  337. GetController(index)?.inputDataBase.InputDataAddKey(InputKeyCode.Enter, InputKeyState.UP);
  338. break;
  339. case "triggerpressed":
  340. GetController(index)?.inputDataBase.InputDataAddKey(InputKeyCode.Enter, InputKeyState.UP);
  341. break;
  342. case "menu":
  343. GetController(index)?.inputDataBase.InputDataAddKey(InputKeyCode.Function, InputKeyState.UP);
  344. break;
  345. case "triggerButton":
  346. GetController(index)?.inputDataBase.InputDataAddKey(InputKeyCode.Enter, InputKeyState.UP);
  347. break;
  348. case "gripButton":
  349. GetController(index)?.inputDataBase.InputDataAddKey(InputKeyCode.Enter, InputKeyState.UP);
  350. break;
  351. }
  352. }
  353. public enum ControllerIndex {
  354. Left = 0,
  355. Right = 1
  356. }
  357. InputDeviceHandShankPart GetController(ControllerIndex controllerIndex) {
  358. if ( SC.InputSystem.InputSystem.Instant && SC.InputSystem.InputSystem.Instant.HandShank ) {
  359. if ( ControllerIndex.Left == controllerIndex ) {
  360. return SC.InputSystem.InputSystem.Instant.HandShank.inputDeviceHandShankPartList.Where(e => e.PartType == SC.InputSystem.InputDevicePartType.HandShankOne).FirstOrDefault();
  361. } else if ( ControllerIndex.Right == controllerIndex ) {
  362. return SC.InputSystem.InputSystem.Instant.HandShank.inputDeviceHandShankPartList.Where(e => e.PartType == SC.InputSystem.InputDevicePartType.HandShankTwo).FirstOrDefault();
  363. }
  364. }
  365. return null;
  366. }
  367. }