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

总结

好记性不如烂笔头

相关内容

热门资讯

我来教教大家!(德州wepow... 我来教教大家!(德州wepower)外挂软件透明挂辅助机器人!(fishpoker大菠萝)可靠技巧(...
3分钟理解!aapoker辅助... 3分钟理解!aapoker辅助器是真的吗(透明挂黑科技)外挂透明挂辅助ai代打(2021已更新)(哔...
五分钟科普!werplan外卦... 五分钟科普!werplan外卦神器,wepokerplus脚本(透视)规律教程(有挂总结)亲,关键说...
发现玩家!德州poker有外挂... 发现玩家!德州poker有外挂,德扑之星ai代打,可靠教程(有人有挂)-哔哩哔哩;德扑之星ai代打是...
两分钟俱乐部!wepoke算法... 您好:wepoke算法这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌...
每日必看!(WPK漏洞)外挂辅... 每日必看!(WPK漏洞)外挂辅助透视透视!(好运大菠萝)细节揭秘(2026已更新)(哔哩哔哩);好运...
一分钟体悟!aapoker能控... 一分钟体悟!aapoker能控制牌吗,wepoker透视脚本苹果版(透视)详细教程(有挂方法)1、w...
一分钟了解!wpk ai会不会... 一分钟了解!wpk ai会不会被封,德州之星可能有外挂,力荐教程(了解有挂)-哔哩哔哩;小薇(透视辅...
9分钟掌握!we poke有挂... 9分钟掌握!we poke有挂吗(透明挂黑科技)外挂透明挂辅助下载(2023已更新)(哔哩哔哩);一...
黑科技教程!(微扑克app)外... 黑科技教程!(微扑克app)外挂辅助神器!(WepokE)安装教程(2021已更新)(哔哩哔哩);一...