当前位置:   article > 正文

UnityXR的Action 和 Input 封装与使用_unity中xr添加新的action

unity中xr添加新的action

直接通过监听按钮,使用方法跟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);
        }
    }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156

使用 -----监听右手手柄的Trigger按钮

	if (CommonXRInput.GetButtonAction(UnityEngine.XR.CommonUsages.triggerButton, out var triggerButton, CommonXRHand.R)){
        if (triggerButton){
        //TODO
        }
	}
  • 1
  • 2
  • 3
  • 4
  • 5

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);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197

使用—要先继承这个类,也是右手手柄的Trigger

public class ActionTest : CommonXRAction{
        public override void R_TriggerPerformed(InputAction.CallbackContext obj)
        {
            base.R_TriggerPerformed(obj);
            //TODO
        }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

简单介绍,具体的可以把代码拿去自测。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小桥流水78/article/detail/792963
推荐阅读
相关标签
  

闽ICP备14008679号