123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494 |
- using NibiruAxis;
- using SC.InputSystem;
- using SC.InputSystem.InputDeviceHandShank;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using UnityEngine;
- using UnityEngine.InputSystem;
- using UnityEngine.XR;
- using WCQ.CommonV1_20200325;
- public class ControllerInputFromAction : MonoBehaviour
- {
- public ControllerIndex index = ControllerIndex.Left;
- public List<InputActionReference> listinput;
- public List<InputActionReference> listtrack;
- public Transform Controller
- {
- get {
- return GetController(index)?.inputDeviceHandShankUI.transform;
- }
- }
- private void Start() {
- SC.InputSystem.InputSystem.ISInitializeCallBack += InputSystemVaildCallBack;
- }
- private void OnDestroy() {
- SC.InputSystem.InputSystem.ISInitializeCallBack -= InputSystemVaildCallBack;
- }
- public Transform right;
- private void Update()
- {
- //GetController(index)?.gameObject.SetActive(true);
- //if (index == ControllerIndex.Left)
- // InputDataHandShank.HSData.HS0Connect = true;
- //else
- // InputDataHandShank.HSData.HS1Connect = true;
- //if (GetController(index))
- //{
- // GetController(index).inputDataHandShank.blueToothName = "K07";
- // GetController(index).inputDataHandShank.handShankType = HandShankType.K07;
- // GetController(index).inputDataHandShank.isVaild = true;
- //}
- //Debug.Log("LateUpdate " + GetController(index).inputDataHandShank.rotation);
- }
- void InputSystemVaildCallBack()
- {
- Invoke("DelayInvoke", 1);
- }
- void DelayInvoke() {
- Debug.Log("InputSystem Initialize ===" + index + " ");
- /*
- var HandedControllers = new List<UnityEngine.XR.InputDevice>();
- var desiredCharacteristics = (index == ControllerIndex.Left ? UnityEngine.XR.InputDeviceCharacteristics.Left : UnityEngine.XR.InputDeviceCharacteristics.Right) | UnityEngine.XR.InputDeviceCharacteristics.Controller;
- UnityEngine.XR.InputDevices.GetDevicesWithCharacteristics(desiredCharacteristics, HandedControllers);
- Debug.Log(HandedControllers.Count + " ===========");
- foreach (var device in HandedControllers)
- {
- Debug.Log(string.Format("Device name '{0}' has characteristics '{1}'", device.name, device.characteristics.ToString()));
- }
- if (HandedControllers.Count == 1)
- {
- onDeviceConnected(HandedControllers[0]);
- }
- if (Application.platform != RuntimePlatform.Android)
- {
- onDeviceConnected(new UnityEngine.XR.InputDevice());
- }
- InputDevices.deviceConnected += onDeviceConnected;
- InputDevices.deviceDisconnected += onDeviceDisConnected;
-
- */
- if(index== ControllerIndex.Left)
- {
- Debug.Log("Left onDeviceDisConnected===>" + index);
- InputDataHandShank.HSData.HS0Connect = false;
- GetController(ControllerIndex.Left)?.gameObject.SetActive(false);
- GetController(index).inputDataHandShank.blueToothName = "K07";
- GetController(index).inputDataHandShank.handShankType = HandShankType.K07;
- GetController(index).inputDataHandShank.isVaild = false;
- GetController(index).inputDataGetHandShank.inputDataGetHandShank3DOF.updateRotationCallBack -= updateRotationCallBack;
- }
- else
- {
- Debug.Log("Right onDeviceConnected===>" + index);
- InputDataHandShank.HSData.HS1Connect = true;
- GetController(ControllerIndex.Right)?.gameObject.SetActive(true);
- GetController(index).inputDataHandShank.blueToothName = "K07";
- GetController(index).inputDataHandShank.handShankType = HandShankType.K07;
- GetController(index).inputDataHandShank.isVaild = true;
- GetController(index).inputDataGetHandShank.inputDataGetHandShank3DOF.updateRotationCallBack += updateRotationCallBack;
- }
- Invoke("DelayInvoke", 1);
- }
- private void onDeviceDisConnected(UnityEngine.XR.InputDevice obj) {
- if (Application.platform == RuntimePlatform.Android)
- {
- if (obj.characteristics.ToString().Contains(InputDeviceCharacteristics.Left.ToString()))
- {
- Debug.Log("Left onDeviceDisConnected===>" + index);
- InputDataHandShank.HSData.HS0Connect = false;
- GetController(ControllerIndex.Left)?.gameObject.SetActive(false);
- GetController(index).inputDataHandShank.blueToothName = "K07";
- GetController(index).inputDataHandShank.handShankType = HandShankType.K07;
- GetController(index).inputDataHandShank.isVaild = false;
- GetController(index).inputDataGetHandShank.inputDataGetHandShank3DOF.updateRotationCallBack -= updateRotationCallBack;
- }
- if (obj.characteristics.ToString().Contains(InputDeviceCharacteristics.Right.ToString()))
- {
- Debug.Log("Right onDeviceDisConnected===>" + index);
- InputDataHandShank.HSData.HS1Connect = false;
- GetController(ControllerIndex.Right)?.gameObject.SetActive(false);
- GetController(index).inputDataHandShank.blueToothName = "K07";
- GetController(index).inputDataHandShank.handShankType = HandShankType.K07;
- GetController(index).inputDataHandShank.isVaild = false;
- GetController(index).inputDataGetHandShank.inputDataGetHandShank3DOF.updateRotationCallBack -= updateRotationCallBack;
- }
- }
- else {
- GetController(index)?.gameObject.SetActive(false);
- if (index == ControllerIndex.Left)
- InputDataHandShank.HSData.HS0Connect = false;
- else
- InputDataHandShank.HSData.HS1Connect = false;
- GetController(index).inputDataHandShank.blueToothName = "K07";
- GetController(index).inputDataHandShank.handShankType = HandShankType.K07;
- GetController(index).inputDataHandShank.isVaild = false;
- GetController(index).inputDataGetHandShank.inputDataGetHandShank3DOF.updateRotationCallBack -= updateRotationCallBack;
- }
- foreach ( var inputactionreference in listinput ) {
- inputactionreference.action.Disable();
- inputactionreference.action.performed -= OnInputPerformed;
- inputactionreference.action.canceled -= OnInputCanceled;
- }
- foreach ( var inputactionreference in listtrack ) {
- inputactionreference.action.Disable();
- inputactionreference.action.performed -= OnTrackPerformed;
- //inputactionreference.action.canceled -= OnTrackCanceled;
- }
- }
- private void onDeviceConnected(UnityEngine.XR.InputDevice obj)
- {
- if (Application.platform == RuntimePlatform.Android)
- {
- if (obj.characteristics.ToString().Contains(InputDeviceCharacteristics.Left.ToString()))
- {
- Debug.Log("Left onDeviceConnected===>" + index);
- InputDataHandShank.HSData.HS0Connect = true;
- GetController(ControllerIndex.Left)?.gameObject.SetActive(true);
- GetController(index).inputDataHandShank.blueToothName = "K07";
- GetController(index).inputDataHandShank.handShankType = HandShankType.K07;
- GetController(index).inputDataHandShank.isVaild = true;
- GetController(index).inputDataGetHandShank.inputDataGetHandShank3DOF.updateRotationCallBack += updateRotationCallBack;
- }
- if (obj.characteristics.ToString().Contains(InputDeviceCharacteristics.Right.ToString()))
- {
- Debug.Log("Right onDeviceConnected===>" + index);
- InputDataHandShank.HSData.HS1Connect = true;
- GetController(ControllerIndex.Right)?.gameObject.SetActive(true);
- GetController(index).inputDataHandShank.blueToothName = "K07";
- GetController(index).inputDataHandShank.handShankType = HandShankType.K07;
- GetController(index).inputDataHandShank.isVaild = true;
- GetController(index).inputDataGetHandShank.inputDataGetHandShank3DOF.updateRotationCallBack += updateRotationCallBack;
- }
- }
- else
- {
- GetController(index)?.gameObject.SetActive(true);
- if (index == ControllerIndex.Left)
- InputDataHandShank.HSData.HS0Connect = true;
- else
- InputDataHandShank.HSData.HS1Connect = true;
- GetController(index).inputDataHandShank.blueToothName = "K07";
- GetController(index).inputDataHandShank.handShankType = HandShankType.K07;
- GetController(index).inputDataHandShank.isVaild = true;
- GetController(index).inputDataGetHandShank.inputDataGetHandShank3DOF.updateRotationCallBack += updateRotationCallBack;
- }
- foreach ( var inputactionreference in listinput ) {
- inputactionreference.action.Enable();
- inputactionreference.action.performed += OnInputPerformed;
- inputactionreference.action.canceled += OnInputCanceled;
- }
- foreach ( var inputactionreference in listtrack ) {
- inputactionreference.action.Enable();
- inputactionreference.action.performed += OnTrackPerformed;
- // inputactionreference.action.canceled += OnTrackCanceled;
- }
- }
- public Quaternion controllerRotation { get; private set; }
- public Vector3 controllerPosition { get; private set; }
- void OnTrackPerformed(InputAction.CallbackContext context) {
- //Debug.Log(Time.frameCount+" OnTrackPerformed" + index + " " + context.control.name);
- switch ( context.control.name ) {
- case "deviceposition":
- case "devicePosition":
- controllerPosition = context.ReadValue<Vector3>();
- break;
- case "devicerotation":
- case "deviceRotation":
- //Debug.Log(Time.frameCount + " OnTrackPerformed" + index + " " + context.control.name+" "+context.ReadValue<Quaternion>());
- // GetController(index).inputDataHandShank.rotation = context.ReadValue<Quaternion>();
- controllerRotation = context.ReadValue<Quaternion>();
- break;
- }
- }
- //private void OnTrackCanceled(InputAction.CallbackContext context) {
- // Debug.Log("OnTrackCanceled" + index + " " + context.control.name);
- // switch ( context.control.name ) {
- // case "devicePosition":
- // break;
- // case "deviceRotation":
- // GetController(index)?.inputDataBase.InputDataAddKey(InputKeyCode.Enter, InputKeyState.UP);
- // break;
- // }
- //}
- void updateRotationCallBack() {
- // Debug.Log(index+" updateRotationCallBack===>");
- // GetController(index).inputDataHandShank.rotation = controllerRotation;
- // GetController(index).inputDataHandShank.position = controllerPosition;
- if (index == ControllerIndex.Right)
- {
- // Debug.Log(NxrPlayerCtrl.Instance.mTransform.eulerAngles);
- // Debug.Log(NxrPlayerCtrl.Instance.mTransform.position);
- GetController(index).inputDataHandShank.rotation = NxrPlayerCtrl.Instance.mTransform.rotation;
- GetController(index).inputDataHandShank.position = NxrPlayerCtrl.Instance.mTransform.position;
- }
- }
- private void LateUpdate()
- {
- return;
- //Debug.Log("LateUpdate " + GetController(index).inputDataHandShank.rotation);
- if (GetController(index))
- {
- // GetController(index).inputDeviceHandShankUI.transform.rotation = controllerRotation;
- //GetController(index).inputDeviceHandShankUI.model.lineIndicate.transform.rotation = controllerRotation;
- //if (GetController(index).inputDeviceUIBase)
- //{
- // GetController(index).inputDeviceUIBase.BeforTargetDetecterUpdateUI();
- //}
- //if (GetController(index).inputDataProcessBase != null)
- //{
- // GetController(index).inputDataProcessBase.OnUpdateAfterUI();
- //}
- ///没有UI,没必要执行TargetDecter的必要
- //if (GetController(index).inputDeviceUIBase != null && GetController(index).inputDeviceTargetDetecterBase != null)
- //{
- // GetController(index).inputDeviceTargetDetecterBase.OnUpdate();
- //}
- //if (GetController(index).inputDeviceUIBase)
- //{
- // GetController(index).inputDeviceUIBase.AfterTargetDetecterUpdateUI();
- //}
- //GetController(index).inputDeviceHandShankUI.modelK07.transform.rotation = controllerRotation;
- //if (GetController(index).inputDataBase != null)
- //{
- // GetController(index).inputDataBase.OnUpdate();
- //}
- //if (GetController(index).inputDataGetBase != null)
- //{
- // GetController(index).inputDataGetBase.OnUpdate();
- //}
- //GetController(index).inputDataHandShank.rotation = controllerRotation;
- //GetController(index).inputDataHandShank.position = controllerPosition;
- //if (GetController(index).inputDevicePartStatusBase != null)
- //{
- // GetController(index).inputDevicePartStatusBase.OnUpdate();
- //}
- /////如果数据无效或不可获取,则关闭UI并退出
- //if (GetController(index).inputDataBase.isVaild == false)
- //{
- // GetController(index).inputDeviceUIBase.UIDisable();
- // DebugMy.Log("inputDataBase.isVaild == false ,Turn Off UI and Return", this);
- // return;
- //}
- //else
- //{
- // DebugMy.Log("inputDataBase.isVaild == true", this);
- // GetController(index).inputDeviceUIBase.UIEnable();
- //}
- /////一些UI坐标值刷新必须在UI更新前执行完
- //if (GetController(index).inputDataProcessBase != null)
- //{
- // GetController(index).inputDataProcessBase.OnUpdateBeforUI();
- //}
- //if (GetController(index).inputDeviceUIBase)
- //{
- // GetController(index).inputDeviceUIBase.BeforTargetDetecterUpdateUI();
- //}
- //if (GetController(index).inputDataProcessBase != null)
- //{
- // GetController(index).inputDataProcessBase.OnUpdateAfterUI();
- //}
- /////没有UI,没必要执行TargetDecter的必要
- //if (GetController(index).inputDeviceUIBase != null && GetController(index).inputDeviceTargetDetecterBase != null)
- //{
- // GetController(index).inputDeviceTargetDetecterBase.OnUpdate();
- //}
- //if (GetController(index).inputDeviceUIBase)
- //{
- // GetController(index).inputDeviceUIBase.AfterTargetDetecterUpdateUI();
- //} ///一些UI坐标值刷新必须在UI更新前执行完
- //if (GetController(index) != null)
- //{
- // GetController(index).inputDeviceUIBase.model.lineIndicate.DrawLineIndicate() ;
- //}
- }
- }
- int xy = 8;
- void OnInputPerformed(InputAction.CallbackContext context) {
- //Debug.Log("OnInputPerformed" + index + " " + context.control.name);
- switch ( context.control.name ) {
- case "secondarytouched":
- break;
- case "secondarybutton":
- GetController(index)?.inputDataBase.InputDataAddKey(InputKeyCode.Enter, InputKeyState.DOWN);
- break;
- case "primarytouched":
- break;
- case "primarybutton":
- break;
- case "touchpadtouched":
- break;
- case "thumbstickclicked":
- GetController(index)?.inputDataBase.InputDataAddKey(InputKeyCode.Tp, InputKeyState.DOWN);
- break;
- case "thumbstick":
- GetController(index)?.inputDataHandShank.InputDataAddTouch(true,
- new Vector2(xy + (int)(context.ReadValue<Vector2>().x * 10), xy + (int)(context.ReadValue<Vector2>().y * 10)));
- break;
- case "grip":
- break;
- case "grippressed":
- GetController(index)?.inputDataBase.InputDataAddKey(InputKeyCode.Enter, InputKeyState.DOWN);
- break;
- case "trigger":
- break;
- case "triggerpressed":
- GetController(index)?.inputDataBase.InputDataAddKey(InputKeyCode.Enter, InputKeyState.DOWN);
- break;
- case "menu":
- GetController(index)?.inputDataBase.InputDataAddKey(InputKeyCode.Function, InputKeyState.DOWN);
- break;
- case "triggerButton":
- GetController(index)?.inputDataBase.InputDataAddKey(InputKeyCode.Enter, InputKeyState.DOWN);
- break;
- case "gripButton":
- GetController(index)?.inputDataBase.InputDataAddKey(InputKeyCode.Enter, InputKeyState.DOWN);
- break;
- }
- }
- private void OnInputCanceled(InputAction.CallbackContext context) {
- // Debug.Log("OnInputCanceled" + index + " " + context.control.name);
- switch ( context.control.name ) {
- case "secondarytouched":
- break;
- case "secondarybutton":
- GetController(index)?.inputDataBase.InputDataAddKey(InputKeyCode.Enter, InputKeyState.UP);
- break;
- case "primarytouched":
- break;
- case "primarybutton":
- break;
- case "touchpadtouched":
- break;
- case "thumbstickclicked":
- GetController(index)?.inputDataBase.InputDataAddKey(InputKeyCode.Tp, InputKeyState.UP);
- break;
- case "thumbstick":
- GetController(index)?.inputDataHandShank.InputDataAddTouch(true,
- new Vector2(xy + (int)(context.ReadValue<Vector2>().x * 10), xy + (int)(context.ReadValue<Vector2>().y * 10)));
- break;
- case "grip":
- break;
- case "grippressed":
- GetController(index)?.inputDataBase.InputDataAddKey(InputKeyCode.Enter, InputKeyState.UP);
- break;
- case "trigger":
- break;
- case "triggerpressed":
- GetController(index)?.inputDataBase.InputDataAddKey(InputKeyCode.Enter, InputKeyState.UP);
- break;
- case "menu":
- GetController(index)?.inputDataBase.InputDataAddKey(InputKeyCode.Function, InputKeyState.UP);
- break;
- case "triggerButton":
- GetController(index)?.inputDataBase.InputDataAddKey(InputKeyCode.Enter, InputKeyState.UP);
- break;
- case "gripButton":
- GetController(index)?.inputDataBase.InputDataAddKey(InputKeyCode.Enter, InputKeyState.UP);
- break;
- }
- }
- public enum ControllerIndex {
- Left = 0,
- Right = 1
- }
- InputDeviceHandShankPart GetController(ControllerIndex controllerIndex) {
- if ( SC.InputSystem.InputSystem.Instant && SC.InputSystem.InputSystem.Instant.HandShank ) {
- if ( ControllerIndex.Left == controllerIndex ) {
- return SC.InputSystem.InputSystem.Instant.HandShank.inputDeviceHandShankPartList.Where(e => e.PartType == SC.InputSystem.InputDevicePartType.HandShankOne).FirstOrDefault();
- } else if ( ControllerIndex.Right == controllerIndex ) {
- return SC.InputSystem.InputSystem.Instant.HandShank.inputDeviceHandShankPartList.Where(e => e.PartType == SC.InputSystem.InputDevicePartType.HandShankTwo).FirstOrDefault();
- }
- }
- return null;
- }
- }
|