赞
踩
UnityXR 手柄按键输入
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.XR; using UnityEngine.UI; public class JackXRUse : MonoBehaviour { private int int_AR, int_BR, int_TR, int_GR, int_MR; private int int_AL, int_BL, int_TL, int_GL, int_ML; public Text Text_AR, Text_BR, Text_TR, Text_GR, Text_MR, Text_JSR_x, Text_JSR_y; public Image Image_JSR; public Text Text_AL, Text_BL, Text_TL, Text_GL, Text_ML, Text_JSL_x, Text_JSL_y; public Image Image_JSL; private bool bool_AR, bool_BR, bool_TR, bool_GR, bool_MR; private bool bool_AR_On, bool_BR_On, bool_TR_On, bool_GR_On, bool_MR_On; private bool bool_AL, bool_BL, bool_TL, bool_GL, bool_ML; private bool bool_AL_On, bool_BL_On, bool_TL_On, bool_GL_On, bool_ML_On; private void Update() { //列出所有输入设备,根据XR节点获取输入设备,必须先获取到输入设备,再获取对应输入设备的输入键值 var rightDevice = new List<InputDevice>(); var leftdevice = new List<InputDevice>(); bool menuValueright, gripValueright, triggerValueright, primaryValueright, secondaryValueright; bool menuValueleft, gripValueleft, triggerValueleft, primaryValueleft, secondaryValueleft; Vector2 joystickright, joystickleft; InputDevices.GetDevicesAtXRNode(XRNode.RightHand, rightDevice); InputDevices.GetDevicesAtXRNode(XRNode.LeftHand, leftdevice); if (rightDevice.Count != 0 && leftdevice.Count != 0) { InputDevice rightcontroller = rightDevice[0]; InputDevice leftcontroller = leftdevice[0]; //右手A键 rightcontroller.TryGetFeatureValue(CommonUsages.primaryButton, out primaryValueright); if(primaryValueright) { bool_AR = true; } else { bool_AR = false; bool_AR_On = true; } //右手B键 rightcontroller.TryGetFeatureValue(CommonUsages.secondaryButton, out secondaryValueright); if (secondaryValueright) { bool_BR = true; } else { bool_BR = false; bool_BR_On = true; } //右手Trigger键 rightcontroller.TryGetFeatureValue(CommonUsages.triggerButton, out triggerValueright); if (triggerValueright) { bool_TR = true; } else { bool_TR = false; bool_TR_On = true; } //右手Grip键 rightcontroller.TryGetFeatureValue(CommonUsages.gripButton, out gripValueright); if (gripValueright) { bool_GR = true; } else { bool_GR = false; bool_GR_On = true; } //右手Menu键 rightcontroller.TryGetFeatureValue(CommonUsages.menuButton, out menuValueright); if (menuValueright) { bool_MR = true; } else { bool_MR = false; bool_MR_On = true; } //右手摇杆 rightcontroller.TryGetFeatureValue(CommonUsages.primary2DAxis, out joystickright); //显示摇杆的Vector2数据 Image_JSR.transform.localPosition = joystickright; Text_JSR_x.text = joystickright.x.ToString(); Text_JSR_y.text = joystickright.y.ToString(); //右手A键按下 if (bool_AR && bool_AR_On) { bool_AR_On = !bool_AR_On; GetHandleRightADown(); } //右手A键按住 if (bool_AR) { GetHandleRightA(); } //右手B键按下 if (bool_BR && bool_BR_On) { bool_BR_On = !bool_BR_On; GetHandleRightBDown(); } //右手B键按住 if (bool_BR) { GetHandleRightB(); } //右手Trigger键按下 if (bool_TR && bool_TR_On) { bool_TR_On = !bool_TR_On; GetHandleRightTDown(); } //右手Trigger键按住 if (bool_TR) { GetHandleRightT(); } //右手Grip键按下 if (bool_GR && bool_GR_On) { bool_GR_On = !bool_GR_On; GetHandleRightGDown(); } //右手Grip键按住 if (bool_GR) { GetHandleRightG(); } //右手Menu键按下 if (bool_MR && bool_MR_On) { bool_MR_On = !bool_MR_On; GetHandleRightMDown(); } //右手Menu键按住 if (bool_MR) { GetHandleRightM(); } //右手JoyStick if (joystickright.x > 0.5f) { } if (joystickright.x < -0.5f) { } if (joystickright.y > 0.5f) { } if (joystickright.y < -0.5f) { } //左手A键 leftcontroller.TryGetFeatureValue(CommonUsages.primaryButton, out primaryValueleft); if (primaryValueleft) { bool_AL = true; } else { bool_AL = false; bool_AL_On = true; } //左手B键 leftcontroller.TryGetFeatureValue(CommonUsages.secondaryButton, out secondaryValueleft); if (secondaryValueleft) { bool_BL = true; } else { bool_BL = false; bool_BL_On = true; } //左手Trigger键 leftcontroller.TryGetFeatureValue(CommonUsages.triggerButton, out triggerValueleft); if (triggerValueleft) { bool_TL = true; } else { bool_TL = false; bool_TL_On = true; } //左手Grip键 leftcontroller.TryGetFeatureValue(CommonUsages.gripButton, out gripValueleft); if (gripValueleft) { bool_GL = true; } else { bool_GL = false; bool_GL_On = true; } //左手Menu键 leftcontroller.TryGetFeatureValue(CommonUsages.menuButton, out menuValueleft); if (menuValueleft) { bool_ML = true; } else { bool_ML = false; bool_ML_On = true; } //左手摇杆 leftcontroller.TryGetFeatureValue(CommonUsages.primary2DAxis, out joystickleft); //显示摇杆的Vector2数据 Image_JSL.transform.localPosition = joystickleft; Text_JSL_x.text = joystickleft.x.ToString(); Text_JSL_y.text = joystickleft.y.ToString(); //左手A键按下 if (bool_AL && bool_AL_On) { bool_AL_On = !bool_AL_On; GetHandleLeftADown(); } //左手A键按住 if (bool_AL) { GetHandleLeftA(); } //左手B键按下 if (bool_BL && bool_BL_On) { bool_BL_On = !bool_BL_On; GetHandleLeftBDown(); } //左手B键按住 if (bool_BL) { GetHandleLeftB(); } //左手Trigger键按下 if (bool_TL && bool_TL_On) { bool_TL_On = !bool_TL_On; GetHandleLeftTDown(); } //左手Trigger键按住 if (bool_TL) { GetHandleLeftT(); } //左手Grip键按下 if (bool_GL && bool_GL_On) { bool_GL_On = !bool_GL_On; GetHandleLeftGDown(); } //左手Grip键按住 if (bool_GL) { GetHandleLeftG(); } //左手Menu键按下 if (bool_ML && bool_ML_On) { bool_ML_On = !bool_ML_On; GetHandleLeftMDown(); } //左手Menu键按住 if (bool_ML) { GetHandleLeftM(); } //左手JoyStick if (joystickleft.x > 0.5f) { } if (joystickleft.x < -0.5f) { } if (joystickleft.y > 0.5f) { } if (joystickleft.y < -0.5f) { } } } //右手A键按下 private void GetHandleRightADown() { int_AR++; Text_AR.text = int_AR.ToString(); } //右手A键按住 private void GetHandleRightA() { int_AR++; Text_AR.text = int_AR.ToString(); } //右手B键按下 private void GetHandleRightBDown() { int_BR++; Text_BR.text = int_BR.ToString(); } //右手B键按住 private void GetHandleRightB() { int_BR++; Text_BR.text = int_BR.ToString(); } //右手T键按下 private void GetHandleRightTDown() { int_TR++; Text_TR.text = int_TR.ToString(); } //右手T键按住 private void GetHandleRightT() { int_TR++; Text_TR.text = int_TR.ToString(); } //右手G键按下 private void GetHandleRightGDown() { int_GR++; Text_GR.text = int_GR.ToString(); } //右手G键按住 private void GetHandleRightG() { int_GR++; Text_GR.text = int_GR.ToString(); } //右手M键按下 private void GetHandleRightMDown() { int_MR++; Text_MR.text = int_MR.ToString(); } //右手M键按住 private void GetHandleRightM() { int_MR++; Text_MR.text = int_MR.ToString(); } //左手A键按下 private void GetHandleLeftADown() { int_AL++; Text_AL.text = int_AL.ToString(); } //左手A键按住 private void GetHandleLeftA() { int_AL++; Text_AL.text = int_AL.ToString(); } //左手B键按下 private void GetHandleLeftBDown() { int_BL++; Text_BL.text = int_BL.ToString(); } //左手B键按住 private void GetHandleLeftB() { int_BL++; Text_BL.text = int_BL.ToString(); } //左手T键按下 private void GetHandleLeftTDown() { int_TL++; Text_TL.text = int_TL.ToString(); } //左手T键按住 private void GetHandleLeftT() { int_TL++; Text_TL.text = int_TL.ToString(); } //左手G键按下 private void GetHandleLeftGDown() { int_GL++; Text_GL.text = int_GL.ToString(); } //左手G键按住 private void GetHandleLeftG() { int_GL++; Text_GL.text = int_GL.ToString(); } //左手M键按下 private void GetHandleLeftMDown() { int_ML++; Text_ML.text = int_ML.ToString(); } //左手M键按住 private void GetHandleLeftM() { int_ML++; Text_ML.text = int_ML.ToString(); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。