Unity XR Interaction Toolkit设置或监听手柄按键事件(三)
创始人
2024-11-27 21:03:46
0

提示:文章有错误的地方,还望诸位大神不吝指教!

文章目录

  • 前言
  • 一、XRI Default Input Actions
      • 1.导入官方案例
      • 2.设置控制器绑定,如手柄、主/辅助按钮、操纵杆等
        • 1.要设置控制器绑定,如左右手 手柄、主/辅助按钮、操纵杆等,请双击:XRI Default Input Actions
        • 2.获取左手XY键事件(右手AB同理)
  • 二、按键对应名称
  • 三、代码示例
  • 总结


前言

官方文档Input System:链接: Input System
安装部分:链接: Unity XR Interaction Toolkit的安装(二)

一、XRI Default Input Actions

1.导入官方案例

需要导入官方案例:Starter Assets,方便学习
在这里插入图片描述

2.设置控制器绑定,如手柄、主/辅助按钮、操纵杆等

1.要设置控制器绑定,如左右手 手柄、主/辅助按钮、操纵杆等,请双击:XRI Default Input Actions

在这里插入图片描述

2.获取左手XY键事件(右手AB同理)

选择顺序 XR Controller–>XR Controller (RightHand)或者XR Controller(LeftHand)–>Usage
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

二、按键对应名称

在这里插入图片描述

三、代码示例

在这里插入图片描述

代码如下(示例):

 using UnityEngine; using UnityEngine.InputSystem;  namespace TWQ {     public class MenuManage : MonoBehaviour     {         public InputActionReference MyLeftButton_X;         public InputActionReference MyLeftButton_Y;          public InputActionReference MyRightButton_X;         public InputActionReference MyRightButton_Y;          private void OnEnable()         {             SetupInteractorEvents();         }         private void OnDisable()         {             TeardownInteractorEvents();         }         void SetupInteractorEvents()         {             //左手             var MyLeftButton_X_Action = GetInputAction(MyLeftButton_X);             if (MyLeftButton_X_Action != null)             {                 MyLeftButton_X_Action.performed += OnMyLeftButton_X_Action;             }             var MyLeftButton_Y_Action = GetInputAction(MyLeftButton_Y);             if (MyLeftButton_Y_Action != null)             {                 MyLeftButton_Y_Action.performed += OnMyLeftButton_Y_Action;             }             //右手             var MyRightButton_X_Action = GetInputAction(MyRightButton_X);             if (MyRightButton_X_Action != null)             {                 MyRightButton_X_Action.performed += OnMyRightButton_X_Action;             }             var MyRightButton_Y_Action = GetInputAction(MyRightButton_Y);             if (MyRightButton_Y_Action != null)             {                 MyRightButton_Y_Action.performed += OnMyRightButton_Y_Action;             }          }         void TeardownInteractorEvents()         {             var MyLeftButton_X_Action = GetInputAction(MyLeftButton_X);             if (MyLeftButton_X_Action != null)             {                 MyLeftButton_X_Action.performed -= OnMyLeftButton_X_Action;             }             var MyLeftButton_Y_Action = GetInputAction(MyLeftButton_Y);             if (MyLeftButton_Y_Action != null)             {                 MyLeftButton_Y_Action.performed -= OnMyLeftButton_Y_Action;             }         }          ///          /// 左手X键         ///          ///          private void OnMyLeftButton_X_Action(InputAction.CallbackContext context)         {             Debug.Log("按下左手X键--------------------");         }         ///          /// 左手Y键         ///          ///          private void OnMyLeftButton_Y_Action(InputAction.CallbackContext context)         {             Debug.Log("按下左手Y键--------------------");         }           ///          /// 右手X键         ///          ///          private void OnMyRightButton_X_Action(InputAction.CallbackContext context)         {             Debug.Log("按下右手A键--------------------");         }         ///          /// 右手Y键         ///          ///          private void OnMyRightButton_Y_Action(InputAction.CallbackContext context)         {             Debug.Log("按下右手B键--------------------");         }          static InputAction GetInputAction(InputActionReference actionReference)         { #pragma warning disable IDE0031 // Use null propagation -- Do not use for UnityEngine.Object types             return actionReference != null ? actionReference.action : null; #pragma warning restore IDE0031         }     } } 

总结

好记性不如烂笔头

相关内容

热门资讯

终于知道!!gg扑克正规,17... 终于知道!!gg扑克正规,17麻将智力众娱输赢规律,安装教程(有挂方法);1、上手简单,内置详细流程...
1分钟辅助挂!畅游麻将有挂吗,... 1分钟辅助挂!畅游麻将有挂吗,长乐斗地主的确是有挂的,教你攻略(有挂攻略)在进入畅游麻将有挂吗辅助挂...
实测揭晓!!wpk微扑克真的有... 实测揭晓!!wpk微扑克真的有辅助插件,天天贵阳麻将插件,扑克教程(有挂详情);1、天天贵阳麻将插件...
两分钟辅助挂!天天贵阳手机麻将... 两分钟辅助挂!天天贵阳手机麻将通用胡牌神器,云南文山麻将翻小鸡一贯真的有挂,透视教程(有挂插件)1、...
五分钟详情!熊猫麻将的隐藏功能... 五分钟详情!熊猫麻将的隐藏功能,多多来了棋牌软件有挂吗(原来一直是有挂);1、操作简单,无需注册,只...
六分钟辅助挂!中至乐平包王小程... 六分钟辅助挂!中至乐平包王小程序技巧,南宁老友十三水本来真的有挂,必备教程(有挂规律);1、构建自己...
我来教教大家!微扑克辅助器是真... 我来教教大家!微扑克辅助器是真的,功夫川麻小程序可以开挂吗,必胜教程(有挂技巧)1、玩家可以在功夫川...
七分钟脚本!南宁老友麻将水鱼摆... 七分钟脚本!南宁老友麻将水鱼摆牌有挂吗,哈灵辅助插件如何下载(其实者的有挂);1、下载好南宁老友麻将...
一分钟快速了解!微扑克可以用a... 一分钟快速了解!微扑克可以用ai辅助,大唐河北麻将有挂吗,新2025版(有挂揭秘)1、用户打开应用后...
三分钟方法!开心武汉花app大... 三分钟方法!开心武汉花app大菠萝,科乐麻将挂件神器通用(原来总是有挂)在进入科乐麻将挂件神器通用辅...