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

总结

好记性不如烂笔头

相关内容

热门资讯

详情透视!hhpoker作必弊... 详情透视!hhpoker作必弊实战视频,wepoker破解版内购,教材教程(果真有挂)-哔哩哔哩1、...
必备透视!hhpoker辅助软... 必备透视!hhpoker辅助软件下载,微信小程序微乐房间怎么开挂,方针教程(有挂规律)-哔哩哔哩1、...
一直以来!桂麻圈辅助器使用方法... 一直以来!桂麻圈辅助器使用方法(辅助)好像是有辅助软件(有挂分析)-哔哩哔哩1)桂麻圈辅助器使用方法...
实测必看!!琼崖辅助软件,we... 实测必看!!琼崖辅助软件,wepoker的辅助器,要领教程(有挂解密)-哔哩哔哩1)琼崖辅助软件免费...
解迷透视!约局吧德州透视,微信... 解迷透视!约局吧德州透视,微信小程序微乐辅助免费,绝活儿教程(详细教程)-哔哩哔哩1、首先打开约局吧...
科普攻略!多乐跑如何增加胜率,... 科普攻略!多乐跑如何增加胜率,aapoker插件下载,教程书教程(揭秘有挂)-哔哩哔哩多乐跑如何增加...
解谜透视!aapoker透视插... 解谜透视!aapoker透视插件,德州局透视,指南教程(有挂教程)-哔哩哔哩1、用户打开应用后不用登...
解谜透视!hhpoker辅助,... 解谜透视!hhpoker辅助,微信小程序游戏辅助,教材教程(有挂秘笈)-哔哩哔哩1、起透看视 hhp...
值得注意的是!淘宝买顺欣茶楼辅... 值得注意的是!淘宝买顺欣茶楼辅助软件靠谱吗(辅助)切实真的是有辅助修改器(有挂教程)-哔哩哔哩1、淘...
玩家必看攻略!冰球突破辅助软件... 玩家必看攻略!冰球突破辅助软件,哈糖大菠萝破解器,攻略教程(详细教程)-哔哩哔哩1、首先打开哈糖大菠...