首页 > 其他分享 >【Unity XR Input 获取Quest和Pico各个按键状态,按下、抬起、按下中】

【Unity XR Input 获取Quest和Pico各个按键状态,按下、抬起、按下中】

时间:2024-08-03 17:39:55浏览次数:5  
标签:ButtonDispatchModel rightHandController Unity 按下 Pico leftHandController Action 

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR;
using QFramework;
/// <summary>
/// 提供各种输入事件
/// </summary>
public class InputEvent : MonoSingleton<InputEvent>
{
    //*************输入设别**************************
    InputDevice leftHandController;
    InputDevice rightHandController;
    InputDevice headController;

    //**************对外提供公开事件******************
    #region public event

    public Action onLeftTriggerEnter;
    public Action onLeftTriggerDown;
    public Action onLeftTriggerUp;

    public Action onRightTriggerEnter;
    public Action onRightTriggerDown;
    public Action onRightTriggerUp;

    public Action onLeftGripEnter;
    public Action onLeftGripDown;
    public Action onLeftGripUp;

    public Action onRightGripEnter;
    public Action onRightGripDown;
    public Action onRightGripUp;

    public Action onLeftAppButtonEnter;
    public Action onLeftAppButtonDown;
    public Action onLeftAppButtonUp;

    public Action onRightAppButtonEnter;
    public Action onRightAppButtonDown;
    public Action onRightAppButtonUp;

    public Action onLeftJoyStickEnter;
    public Action onLeftJoyStickDown;
    public Action onLeftJoyStickUp;

    public Action onRightJoyStickEnter;
    public Action onRightJoyStickDown;
    public Action onRightJoyStickUp;

    public Action<Vector2> onLeftJoyStickMove;
    public Action<Vector2> onRightJoyStickMove;

    public Action onLeftAXButtonEnter;
    public Action onLeftAXButtonDown;
    public Action onLeftAXButtonUp;

    public Action onLeftBYButtonEnter;
    public Action onLeftBYButtonDown;
    public Action onLeftBYButonUp;

    public Action onRightAXButtonEnter;
    public Action onRightAXButtonDown;
    public Action onRightAXButtonUp;

    public Action onRightBYButtonEnter;
    public Action onRightBYButtonDown;
    public Action onRightBYButtonUp;

    #endregion

    //提供状态字典独立记录各个feature的状态
    Dictionary<string, bool> stateDic;

    private void Init()
    {
        if (!leftHandController.isValid)
        {
            leftHandController = InputDevices.GetDeviceAtXRNode(XRNode.LeftHand);
        }
        if (!rightHandController.isValid)
        {
            rightHandController = InputDevices.GetDeviceAtXRNode(XRNode.RightHand);
        }
        if (!headController.isValid)
        {
            headController = InputDevices.GetDeviceAtXRNode(XRNode.Head);
        }
        stateDic = new Dictionary<string, bool>();

    }

    //*******************事件源的触发**************************
    /// <summary>
    /// 按钮事件源触发模板
    /// </summary>
    /// <param name="device">设备</param>
    /// <param name="usage">功能特征</param>
    /// <param name="btnEnter">开始按下按钮事件</param>
    /// <param name="btnDown">按下按钮事件</param>
    /// <param name="btnUp">抬起按钮事件</param>
    private void ButtonDispatchModel(InputDevice device, InputFeatureUsage<bool> usage, Action btnEnter, Action btnDown, Action btnUp)
    {
        //Debug.Log("usage:" + usage.name);
        //为首次执行的feature添加bool状态 -- 用以判断Enter和Up状态
        string featureKey = device.characteristics + usage.name;
        if (!stateDic.ContainsKey(featureKey))
        {
            stateDic.Add(featureKey, false);
        }

        bool isDown;
        if (device.TryGetFeatureValue(usage, out isDown) && isDown)
        {
            //Debug.Log("device:" + device.characteristics + "usage:" + usage.name);
            if (!stateDic[featureKey])
            {
                stateDic[featureKey] = true;
                btnEnter?.Invoke();
            }
            btnDown?.Invoke();
        }
        else
        {
            if (stateDic[featureKey])
            {
                btnUp?.Invoke();
                stateDic[featureKey] = false;
            }
        }
    }

    /// <summary>
    /// 摇杆事件源触发模板
    /// </summary>
    /// <param name="device">设备</param>
    /// <param name="usage">功能特征</param>
    /// <param name="joyStickMove">移动摇杆事件</param>
    private void JoyStickDispatchModel(InputDevice device, InputFeatureUsage<Vector2> usage, Action<Vector2> joyStickMove)
    {
        Vector2 axis;
        if (device.TryGetFeatureValue(usage, out axis) && !axis.Equals(Vector2.zero))
        {
            if (joyStickMove != null)
                joyStickMove(axis);
        }
    }

    //******************每帧轮询监听事件***********************
    private void Update()
    {
        if (leftHandController.isValid)
        {
            ButtonDispatchModel(leftHandController, CommonUsages.triggerButton, onLeftTriggerEnter, onLeftTriggerDown, onLeftTriggerUp);
            ButtonDispatchModel(leftHandController, CommonUsages.gripButton, onLeftGripEnter, onLeftGripDown, onLeftGripUp);
            ButtonDispatchModel(leftHandController, CommonUsages.primaryButton, onLeftAXButtonEnter, onLeftAXButtonDown, onLeftAXButtonUp);
            ButtonDispatchModel(leftHandController, CommonUsages.secondaryButton, onLeftBYButtonEnter, onLeftBYButtonDown, onLeftBYButonUp);
            ButtonDispatchModel(leftHandController, CommonUsages.primary2DAxisClick, onLeftJoyStickEnter, onLeftJoyStickDown, onLeftJoyStickUp);
            ButtonDispatchModel(leftHandController, CommonUsages.menuButton, onLeftAppButtonEnter, onLeftAppButtonDown, onLeftAppButtonUp);
            //JoyStickDispatchModel(leftHandController, CommonUsages.primary2DAxis, onLeftJoyStickMove);
        }
        else
        {
            Init();
        }
        if (rightHandController.isValid)
        {
            ButtonDispatchModel(rightHandController, CommonUsages.triggerButton, onRightTriggerEnter, onRightTriggerDown, onRightTriggerUp);
            ButtonDispatchModel(rightHandController, CommonUsages.gripButton, onRightGripEnter, onRightGripDown, onRightGripUp);
            ButtonDispatchModel(rightHandController, CommonUsages.primaryButton, onRightAXButtonEnter, onRightAXButtonDown, onRightAXButtonUp);
            ButtonDispatchModel(rightHandController, CommonUsages.secondaryButton, onRightBYButtonEnter, onRightBYButtonDown, onRightBYButtonUp);
            ButtonDispatchModel(rightHandController, CommonUsages.primary2DAxisClick, onRightJoyStickEnter, onRightJoyStickDown, onRightJoyStickUp);
            ButtonDispatchModel(rightHandController, CommonUsages.menuButton, onRightAppButtonEnter, onRightAppButtonDown, onRightAppButtonUp);
            //JoyStickDispatchModel(rightHandController, CommonUsages.primary2DAxis, onRightJoyStickMove);
        }
        else
        {
            Init();
        }
    }
}

使用

void Awake()
{
 InputEvent.Instance.onRightBYButtonUp += RightBYButtonDown;
}
 void RightBYButtonDown()
 {
     Debug.Log("右手柄B键按下中......");
     Time.timeScale = 0;
     IsPaused = true;
     AudioManager.Instance.PauseAll();
     //PopUpManager.Instance.ShowPauseMenu();
 }

方法二:按下B键

 private void Update()
 {
     if (InputDevices.GetDeviceAtXRNode(XRNode.RightHand).TryGetFeatureValue(UnityEngine.XR.CommonUsages.secondaryButton, out triggerValue) && triggerValue)
     {
         RightBYButtonDown();
     }
 }

 

标签:ButtonDispatchModel,rightHandController,Unity,按下,Pico,leftHandController,Action,
From: https://www.cnblogs.com/WalkingSnail/p/18340763

相关文章

  • LogCat连接安卓手机拉取日志到本地(Unity开发版)
    unity开发游戏的时候经常会碰到安卓手机真机报错/崩溃,定位问题需要拉取安卓手机上的日志到电脑上来查看。1.unity安装的时候,勾选安卓模块(sdk这些记得勾选安装)2.打开对应安卓模块个目录下的adb目录,当前我的安装目录为C:\ProgramFiles\Unity\Hub\Editor\2021.3.32f1\Editor\D......
  • 【unity小技巧】unity性能优化以及如何进行性能测试
    文章目录前言GPU性能优化打包素材CPU性能优化代码执行优化性能测试Vector2.Distance和sqrMagnitude哪个好?动画切换优化shader属性优化URP渲染器资产优化对象池优化删除没必要的空函数图片、音乐音效、贴图等素材压缩ScriptableObject优化参数参考完结前言功能的......
  • C# & Unity 面向对象补全计划 之 接口
    本文仅作学习笔记与交流,不作任何商业用途,作者能力有限,如有不足还请斧正本系列旨在通过补全学习之后,给出任意类图都能实现并做到逻辑上严丝合缝1.接口在C#中,接口(interface)是一种定义了一组方法、属性和事件的类型接口只包含成员的声明,而不包含任何实现,实现接口的类必须......
  • Unity中调试Scroll View,一个Scroll View可以加载不同的图片
    1.所有的图片宽度要相同(最好)2.锚点设置usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;usingUnityEngine.UI;publicclassScrollImageScale:MonoBehaviour{publicImageimage;publicGameObjectcontent;privatef......
  • 【Unity UI】Ultimate Clean GUI Pack: 打造专业级2D界面的终极工具包
    在Unity游戏开发中,用户界面(UI)是玩家体验的重要组成部分。一个美观、直观且响应迅速的UI能够极大地提升玩家的游戏体验。"UltimateCleanGUIPack"是一个专为Unity设计的2DGUI资源包,提供了一整套现代化且风格统一的界面元素,帮助你快速打造出专业级别的用户界面。一、资源......
  • 【Unity源码】Auto Chess: 自走棋策略游戏开发框架
    在UnityAssetStore上,一款名为"AutoChess"的资源包为开发者提供了一个完整的框架,以便快速构建和部署自己的自走棋游戏。自走棋是一种结合了策略、卡牌和棋盘游戏元素的流行游戏类型,而这个资源包让开发者能够轻松地将这一概念实现在Unity项目中。资源包亮点全面的......
  • Unity引擎字符串内存布局
      Unity引擎的字符串有三种存储方式:堆:分配在堆上内嵌:一个栈上的内存数据。默认25字节,可以放长度最多24的字符串。这个长度定义为STACK_LENGTH. 外部  重点主要是前两种,这是一种优化方法,对于非常短的字符串,可以直接使用栈数据而不需要再次内存分配。C++伪代......
  • anki Windows 按下z键,使用有道api发音选中英文文本
     <scripttype="text/javascript"> //播放句子的函数 functionplaySentence(sentence){ //构造有道词典的在线朗读URL varyoudaoUrl="https://dict.youdao.com/dictvoice?audio="+encodeURIComponent(sentence); //创建音频元素并播放 varaudio=newAu......
  • Unity 摄像机跟随人物、人物走到地图边缘摄像机停止平移、手指长按屏幕摄像机平移
    1.摄像机跟随人物首先,你需要一个脚本来控制摄像机跟随人物。这个脚本应该附加到你的摄像机对象上。CameraFollow.csusingUnityEngine;publicclassCameraFollow:MonoBehaviour{publicTransformtarget;//指向你的人物对象publicfloatsmoothSpeed......
  • unity报错CommandWithNoStdoutInvokationFailure: Unable to start ADB server.
    这个错误提示表明Unity无法启动ADB(AndroidDebugBridge)服务器,这通常是因为AndroidSDK没有安装或者配置不正确。以下是一些解决这个问题的步骤:确认AndroidSDK的安装:确保你已经安装了AndroidSDK。可以通过AndroidStudio来安装SDK,或者从Android开发者网站下载。配置U......