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

总结

好记性不如烂笔头

相关内容

热门资讯

揭秘攻略!透视小程序开挂辅助器... 揭秘攻略!透视小程序开挂辅助器,新天道辅助软件可以样,力荐教程(有人有挂);详细新天道辅助软件可以样...
必看攻略“随意玩聚乐部辅助”普... 必看攻略“随意玩聚乐部辅助”普及开挂作弊辅助app(固有有挂);随意玩聚乐部辅助是一款益智类棋牌手游...
技巧知识分享!中至辅助器,心悦... 技巧知识分享!中至辅助器,心悦填大坑辅助,透视教程(今日头条);1、很好的心悦填大坑辅助工具软件,可...
总算了解!九游辅助破解版,悠闲... 总算了解!九游辅助破解版,悠闲卡五星辅助,德州论坛(有挂方式);2. 七法小法:双重施法,震撼全场总...
今日科普!微乐四川亲友圈辅助器... 今日科普!微乐四川亲友圈辅助器,边锋斗地主插件辅助脚本,分享教程(有挂秘籍)是一款可以让一直输的玩家...
推荐十款“衢州都莱破解器”介绍... 推荐十款“衢州都莱破解器”介绍开挂作弊辅助下载(总是真的有挂);衢州都莱破解器中的10万兆豆可能无法...
玩家必看秘籍!欢乐达人正版脚本... 玩家必看秘籍!欢乐达人正版脚本,菠萝神辅助器app,AI教程(有挂透视)是一款可以让一直输的玩家,快...
必看攻略!乐酷副厅外挂辅助,三... 必看攻略!乐酷副厅外挂辅助,三加一辅助,线上教程(有挂解惑);一、乐酷副厅外挂辅助软件透明挂的定义与...
重大消息!微信小程序修改器,微... 重大消息!微信小程序修改器,微信大厅辅助软件,2025新版(有挂方法);1、【微信大厅辅助软件辅助软...
来一盘“广东雀神智能插件安装可... 来一盘“广东雀神智能插件安装可测试”推荐开挂作弊辅助下载(起初真的是有挂);亲,有的,ai轻松简单,...