赞
踩
直接通过监听按钮,使用方法跟Input.getkey差不多
直接上代码
using System.Collections.Generic; using UnityEngine; using UnityEngine.XR; public enum CommonXRHand { L, R, Def = R,//默认手 } /// <summary> /// XR通用按钮监听 ,Editor XR Device Simulator模拟不可用 /// </summary> public class CommonXRInput { static UnityEngine.XR.InputDevice deviceHand_L; static UnityEngine.XR.InputDevice deviceHand_R; static int last_frame_count = -1; static Dictionary<CommonXRHand, Dictionary<string, bool>> buttonStateCache = new Dictionary<CommonXRHand, Dictionary<string, bool>>() { { CommonXRHand.L, new Dictionary<string, bool>() }, { CommonXRHand.R, new Dictionary<string, bool>() } }; static Dictionary<CommonXRHand, Dictionary<string, bool>> buttonStateCache2 = new Dictionary<CommonXRHand, Dictionary<string, bool>>() { { CommonXRHand.L, new Dictionary<string, bool>() }, { CommonXRHand.R, new Dictionary<string, bool>() } }; private static void CheckDevice(CommonXRHand hand = CommonXRHand.Def) { if (hand == CommonXRHand.L) { if (deviceHand_L == null || string.IsNullOrEmpty(deviceHand_L.name)) deviceHand_L = UnityEngine.XR.InputDevices.GetDeviceAtXRNode(UnityEngine.XR.XRNode.LeftHand); } else { if (deviceHand_R == null || string.IsNullOrEmpty(deviceHand_R.name)) deviceHand_R = UnityEngine.XR.InputDevices.GetDeviceAtXRNode(UnityEngine.XR.XRNode.RightHand); } } public static bool GetButtonAction(InputFeatureUsage<bool> usage, out bool actionVal, CommonXRHand hand = CommonXRHand.Def) { if (last_frame_count == -1) { last_frame_count = Time.frameCount; } if (last_frame_count != Time.frameCount) { foreach (var cache2 in buttonStateCache2) { foreach (var buttonStates in cache2.Value) { buttonStateCache[cache2.Key][buttonStates.Key] = buttonStates.Value; } } last_frame_count = Time.frameCount; } string name = usage.name; if (!buttonStateCache[hand].ContainsKey(name)) { buttonStateCache[hand][name] = false; } CheckDevice(hand); bool state; if (hand == CommonXRHand.L) { deviceHand_L.TryGetFeatureValue(usage, out state); } else { deviceHand_R.TryGetFeatureValue(usage, out state); } if (buttonStateCache[hand][name] != state) { buttonStateCache2[hand][name] = state; actionVal = state; return true; } else { actionVal = false; return false; } } public static bool GetButtonValue(InputFeatureUsage<bool> usage, out bool buttonVal, CommonXRHand hand = CommonXRHand.Def) { CheckDevice(hand); if (hand == CommonXRHand.L) { return deviceHand_L.TryGetFeatureValue(usage, out buttonVal); } else { return deviceHand_R.TryGetFeatureValue(usage, out buttonVal); } } public static bool GetButtonValue(InputFeatureUsage<float> usage, out float buttonVal, CommonXRHand hand = CommonXRHand.Def) { CheckDevice(hand); if (hand == CommonXRHand.L) { return deviceHand_L.TryGetFeatureValue(usage, out buttonVal); } else { return deviceHand_R.TryGetFeatureValue(usage, out buttonVal); } } public static bool GetButtonValue(InputFeatureUsage<Vector2> usage, out Vector2 buttonVal, CommonXRHand hand = CommonXRHand.Def) { CheckDevice(hand); if (hand == CommonXRHand.L) { return deviceHand_L.TryGetFeatureValue(usage, out buttonVal); } else { return deviceHand_R.TryGetFeatureValue(usage, out buttonVal); } } public static bool GetButtonValue(InputFeatureUsage<Vector3> usage, out Vector3 buttonVal, CommonXRHand hand = CommonXRHand.Def) { CheckDevice(hand); if (hand == CommonXRHand.L) { return deviceHand_L.TryGetFeatureValue(usage, out buttonVal); } else { return deviceHand_R.TryGetFeatureValue(usage, out buttonVal); } } public static bool GetButtonValue(InputFeatureUsage<Quaternion> usage, out Quaternion buttonVal, CommonXRHand hand = CommonXRHand.Def) { CheckDevice(hand); if (hand == CommonXRHand.L) { return deviceHand_L.TryGetFeatureValue(usage, out buttonVal); } else { return deviceHand_R.TryGetFeatureValue(usage, out buttonVal); } } }
使用 -----监听右手手柄的Trigger按钮
if (CommonXRInput.GetButtonAction(UnityEngine.XR.CommonUsages.triggerButton, out var triggerButton, CommonXRHand.R)){
if (triggerButton){
//TODO
}
}
UnityXR的Action事件响应,使用这个的前提是对UnityXR的Action事件响应机制有一定的了解。 这个是被改过的对应下面代码的Assets文件。常用的都写了,没有自补。
https://download.csdn.net/download/weixin_44347839/88632367?ydreferer=aHR0cHM6Ly9tcC5jc2RuLm5ldC9tcF9kb3dubG9hZC9tYW5hZ2UvZG93bmxvYWQvVXBEZXRhaWxlZA%3D%3D
using System; using System.Collections; using System.Collections.Generic; using System.Reflection; using UnityEngine; using UnityEngine.InputSystem; /// <summary> /// XR通用按钮调用Action ,要获取指定的InputActionAsset,Editor 模拟可用 /// </summary> public class CommonXRAction : MonoBehaviour { public InputActionAsset actionAsset; private InputAction RightGrip_Action; private InputAction LeftGrip_Action; private InputAction RightTrigger_Action; private InputAction LeftTrigger_Action; private InputAction Menu_Action; private InputAction RighA_Action; private InputAction LeftX_Action; private InputAction RighB_Action; private InputAction LeftY_Action; public virtual void Start() { RightGrip_Action = actionAsset.FindAction("XRI RightHand Interaction/Activate"); RightGrip_Action.performed += R_GripPerformed; RightGrip_Action.started += R_GripStarted; RightGrip_Action.canceled += R_GripCanceled; LeftGrip_Action = actionAsset.FindAction("XRI LeftHand Interaction/Activate"); LeftGrip_Action.performed += L_GripPerformed; LeftGrip_Action.started += L_GripStarted; LeftGrip_Action.canceled += L_GripCanceled; LeftX_Action = actionAsset.FindAction("XRI LeftHand Interaction/Btn_X"); LeftX_Action.performed += XPerformed; LeftX_Action.started += Xstarted; LeftX_Action.canceled += Xcanceled; LeftY_Action = actionAsset.FindAction("XRI LeftHand Interaction/Btn_Y"); LeftY_Action.performed += YPerformed; LeftY_Action.started += Ystarted; LeftY_Action.canceled += Ycanceled; RighA_Action = actionAsset.FindAction("XRI RightHand Interaction/Btn_A"); RighA_Action.performed += APerformed; RighA_Action.started += Astarted; RighA_Action.canceled += Acanceled; RighB_Action = actionAsset.FindAction("XRI RightHand Interaction/Btn_B"); RighB_Action.performed += BPerformed; RighB_Action.started += Bstarted; RighB_Action.canceled += Bcanceled; RightTrigger_Action = actionAsset.FindAction("XRI RightHand Interaction/Select"); RightTrigger_Action.performed += R_TriggerPerformed; RightTrigger_Action.started += R_Triggerstarted; RightTrigger_Action.canceled += R_Triggercanceled; LeftTrigger_Action = actionAsset.FindAction("XRI LeftHand Interaction/Select"); LeftTrigger_Action.performed += L_TriggerPerformed; LeftTrigger_Action.started += L_Triggerstarted; LeftTrigger_Action.canceled += L_Triggercanceled; Menu_Action = actionAsset.FindAction("XRI LeftHand Interaction/Btn_Menu"); Menu_Action.performed += MenuPerformed; Menu_Action.started += Menustarted; Menu_Action.canceled += Menucanceled; } public virtual void Menucanceled(InputAction.CallbackContext obj) { Debug.Log(MethodBase.GetCurrentMethod().Name); } public virtual void MenuPerformed(InputAction.CallbackContext obj) { Debug.Log(MethodBase.GetCurrentMethod().Name); } public virtual void Menustarted(InputAction.CallbackContext obj) { Debug.Log(MethodBase.GetCurrentMethod().Name); } public virtual void L_Triggercanceled(InputAction.CallbackContext obj) { Debug.Log(MethodBase.GetCurrentMethod().Name); } public virtual void L_Triggerstarted(InputAction.CallbackContext obj) { Debug.Log(MethodBase.GetCurrentMethod().Name); } public virtual void L_TriggerPerformed(InputAction.CallbackContext obj) { Debug.Log(MethodBase.GetCurrentMethod().Name); } public virtual void R_Triggercanceled(InputAction.CallbackContext obj) { Debug.Log(MethodBase.GetCurrentMethod().Name); } public virtual void R_Triggerstarted(InputAction.CallbackContext obj) { Debug.Log(MethodBase.GetCurrentMethod().Name); } public virtual void R_TriggerPerformed(InputAction.CallbackContext obj) { Debug.Log(MethodBase.GetCurrentMethod().Name); } public virtual void Bcanceled(InputAction.CallbackContext obj) { Debug.Log(MethodBase.GetCurrentMethod().Name); } public virtual void Bstarted(InputAction.CallbackContext obj) { Debug.Log(MethodBase.GetCurrentMethod().Name); } public virtual void BPerformed(InputAction.CallbackContext obj) { Debug.Log(MethodBase.GetCurrentMethod().Name); } public virtual void Acanceled(InputAction.CallbackContext obj) { Debug.Log(MethodBase.GetCurrentMethod().Name); } public virtual void Astarted(InputAction.CallbackContext obj) { Debug.Log(MethodBase.GetCurrentMethod().Name); } public virtual void APerformed(InputAction.CallbackContext obj) { Debug.Log(MethodBase.GetCurrentMethod().Name); } public virtual void Ycanceled(InputAction.CallbackContext obj) { Debug.Log(MethodBase.GetCurrentMethod().Name); } public virtual void Ystarted(InputAction.CallbackContext obj) { Debug.Log(MethodBase.GetCurrentMethod().Name); } public virtual void YPerformed(InputAction.CallbackContext obj) { Debug.Log(MethodBase.GetCurrentMethod().Name); } public virtual void Xcanceled(InputAction.CallbackContext obj) { Debug.Log(MethodBase.GetCurrentMethod().Name); } public virtual void Xstarted(InputAction.CallbackContext obj) { Debug.Log(MethodBase.GetCurrentMethod().Name); } public virtual void XPerformed(InputAction.CallbackContext obj) { Debug.Log(MethodBase.GetCurrentMethod().Name); } public virtual void R_GripPerformed(InputAction.CallbackContext obj) { Debug.Log(MethodBase.GetCurrentMethod().Name); } public virtual void R_GripStarted(InputAction.CallbackContext obj) { Debug.Log(MethodBase.GetCurrentMethod().Name); } public virtual void R_GripCanceled(InputAction.CallbackContext obj) { Debug.Log(MethodBase.GetCurrentMethod().Name); } public virtual void L_GripCanceled(InputAction.CallbackContext obj) { Debug.Log(MethodBase.GetCurrentMethod().Name); } public virtual void L_GripStarted(InputAction.CallbackContext obj) { Debug.Log(MethodBase.GetCurrentMethod().Name); } public virtual void L_GripPerformed(InputAction.CallbackContext obj) { Debug.Log(MethodBase.GetCurrentMethod().Name); } }
使用—要先继承这个类,也是右手手柄的Trigger
public class ActionTest : CommonXRAction{
public override void R_TriggerPerformed(InputAction.CallbackContext obj)
{
base.R_TriggerPerformed(obj);
//TODO
}
}
简单介绍,具体的可以把代码拿去自测。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。