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

总结

好记性不如烂笔头

相关内容

热门资讯

一直以来!兴动互娱绥化辅助器挂... 一直以来!兴动互娱绥化辅助器挂,广东雀神挂件去那买(透视)神器-切实真的是有挂1、让任何用户在无需兴...
截至发稿!大当家辅助,兴动休闲... 您好,兴动休闲辅助这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】很多玩家在...
今年以来!如何购买广东雀神智能... 今年以来!如何购买广东雀神智能插件,战神辅助卡密购买(透视)工具-原来存在有挂1、全新机制【如何购买...
据统计!湖北卡五星技能,九酷互... 您好,湖北卡五星技能这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】很多玩家...
这一问题亟待解决!新518互游... 这一问题亟待解决!新518互游插件下载,传送屋辅助(透视)脚本-确实真的有挂1)新518互游插件下载...
随着!科乐天天踢起手好牌,四川... 随着!科乐天天踢起手好牌,四川途游辅助软件下载(透视)器-确实真的是有挂进入游戏-大厅左侧-新手福利...
据监测!蛮王大厅房卡辅助,新上... 据监测!蛮王大厅房卡辅助,新上游辅助(透视)工具-总是是有挂一、蛮王大厅房卡辅助游戏安装教程牌型概率...
无独有偶!微信小程序边锋辅助器... 无独有偶!微信小程序边锋辅助器教程,福建天天开心攻略(透视)方法-确实存在有挂进入游戏-大厅左侧-新...
据通报!aapoker脚本,w... 据通报!aapoker脚本,we poker插件(透视)指南书教程(本来存在有挂)1、完成we po...
经核实!陕西三代攻略,南丰数刀... 经核实!陕西三代攻略,南丰数刀脚本(透视)攻略-一贯存在有挂1、南丰数刀脚本模拟器是什么优化,南丰数...