当前位置:   article > 正文

Unity XR手柄按键管理模块_unity xr手柄各个按键怎么获取

unity xr手柄各个按键怎么获取

    unity XR里面手柄的API,我使用时候发现,只能返回是否按了的返回值,Trigger(扳机键)按住是一直返回true,我想要的抬起、按下没有对应的参数,我设计的管理模块对这些API进行了整理,能够获取按钮的抬起、按下的返回值。

     设计了一个字典,初始化将需要使用到的按钮注册到字典当中,在UPdate中获取手柄信息,调用TryGetFeatureValue,获取每个按钮的按下的返回值。这里按下的意思是只要你按了就返回true,不区分按下、抬起、长按等,遍历字典里的按钮信息,pressed只要按了这个按钮既为true,第一次按下的时候就是buttondown为true(类似HTC),后续长按返回false。lastButtonState是上一次pressed的返回值,不相等的时候就是抬起的时候buttonup=true。

      这个类可以设计成单例模式,调用Getfunction,参数就是手柄按钮的类型Inputstate枚举。

下面是相关代码,仅供参考。


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR;

public enum Inputstate
{
    menuL,
    menuR,
    /// <summary>
    /// 左手柄扳机按键
    /// </summary>
    triggerL,
    triggerR,
    gripL,
    gripR,
    primaryL,
    /// <summary>
    /// 右手柄A
    /// </summary>
    primaryR,
    /// <summary>
    /// 左手柄B键
    /// </summary>
    secondaryL,
    secondaryR,
}
public class function
{
    public bool pressed;
    public bool buttondown;
    public bool buttonup;
    public bool downonce;
    public bool lastButtonState;
}

public class MRInput : MonoBehaviour
{
    public bool menuValue_Ldown;
    public bool menuValue_Lup;
    /// <summary>
    /// 按下左手柄A键
    /// </summary>
    public bool primaryValue_L;
    /// <summary>
    /// 按下右手柄A键
    /// </summary>
    public bool primaryValue_R;
    /// <summary>
    /// 按下左手柄B键
    /// </summary>
    public bool secondaryValue_L;
    /// <summary>
    /// 按下右手柄B键
    /// </summary>
    public bool secondaryValue_R;
    /// <summary>
    /// 按下左手柄Trigger按键
    /// </summary>
    public bool triggerValue_L;
    /// <summary>
    /// 按下右手柄Trigger按键
    /// </summary>
    public bool triggerValue_R;
    public bool lastButtonState = false;
    Dictionary<Inputstate, function> Dicfunction = new Dictionary<Inputstate, function>();
    function menu_L = new function();
    function menu_R = new function();
    function trigger_L = new function();
    function trigger_R = new function();
    function grip_L = new function();
    function grip_R = new function();
    function primary_L = new function();
    function primary_R = new function();
    function secondary_L = new function();
    function secondary_R = new function();
    private void Awake()
    {
        注册按钮

        Dicfunction.Add(Inputstate.menuL, menu_L);
        Dicfunction.Add(Inputstate.menuR, menu_R);
        Dicfunction.Add(Inputstate.triggerL, trigger_L);
        Dicfunction.Add(Inputstate.triggerR, trigger_R);

        Dicfunction.Add(Inputstate.primaryL, primary_L);
        Dicfunction.Add(Inputstate.primaryR, primary_R);
        Dicfunction.Add(Inputstate.secondaryL, secondary_L);
        Dicfunction.Add(Inputstate.secondaryR, secondary_R);

        Dicfunction.Add(Inputstate.gripL, grip_L);
        Dicfunction.Add(Inputstate.gripR, grip_R);
    }

    // Update is called once per frame
    void Update()
    {
        //列出所有输入设备,根据XR节点获取输入设备,必须先获取到输入设备,再获取对应输入设备的输入键值
        var rightDevice = new List<InputDevice>();

        var leftdevice = new List<InputDevice>();

        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];
            leftcontroller.TryGetFeatureValue(CommonUsages.menuButton, out menu_L.pressed);
            rightcontroller.TryGetFeatureValue(CommonUsages.menuButton, out menu_R.pressed);

            leftcontroller.TryGetFeatureValue(CommonUsages.primaryButton, out primary_L.pressed);
            rightcontroller.TryGetFeatureValue(CommonUsages.primaryButton, out primary_R.pressed);

            leftcontroller.TryGetFeatureValue(CommonUsages.secondaryButton, out secondary_L.pressed);
            rightcontroller.TryGetFeatureValue(CommonUsages.secondaryButton, out secondary_R.pressed);

            leftcontroller.TryGetFeatureValue(CommonUsages.triggerButton, out trigger_L.pressed);
            rightcontroller.TryGetFeatureValue(CommonUsages.triggerButton, out trigger_R.pressed);

            leftcontroller.TryGetFeatureValue(CommonUsages.gripButton, out grip_L.pressed);
            rightcontroller.TryGetFeatureValue(CommonUsages.gripButton, out grip_R.pressed);
        }
        foreach(var it in Dicfunction)
        {
            if(it.Value.pressed)
            {
                if(it.Value.downonce)
                {
                    it.Value.buttondown = true;
                    it.Value.downonce = false;
                }
                else
                    it.Value.buttondown = false;
            }
            if (it.Value.lastButtonState && !it.Value.pressed)
            {
                it.Value.buttonup = true;
                it.Value.buttondown = false;
                it.Value.downonce = true;
            }
            else
            {
                it.Value.buttonup = false;
            }
            it.Value.lastButtonState = it.Value.pressed;
        }

    }
    public function Getfunction(Inputstate inputstate)
    {
        function temp = new function();
        Dicfunction.TryGetValue(inputstate, out temp);
        return temp;
    }
}
 

        本人测试时候发现有点延迟,我不知道是因为头盔(国产头盔)的问题,还是我代码逻辑有问题,如果有小伙伴发现了,欢迎指正探讨,尚未完善、请勿转载谢谢,害怕误人子弟!

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

闽ICP备14008679号