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

总结

好记性不如烂笔头

相关内容

热门资讯

透视黑科技!wepoker钻石... 透视黑科技!wepoker钻石怎么看底牌(透视)从来有挂(详细辅助系统教程);1分钟了解详细教程(微...
透视脚本!wepoker免费永... 透视脚本!wepoker免费永久脚本(透视)详细辅助透明挂教程(一直真的有挂)1、在wepoker免...
透视科技!aapoker透视怎... 透视科技!aapoker透视怎么用,德普软件(详细辅助必胜教程)aapoker透视怎么用软件透明挂微...
透视规律(wePOKE)wep... 透视规律(wePOKE)wepower让系统发好牌(透视)详细辅助分享教程关于wepower让系统发...
透视最新!aapoker能控制... 1、透视最新!aapoker能控制牌吗(透视)原来有挂(详细辅助大神讲解);详细教程。2、aapok...
透视辅助!aapoker辅助是... 透视辅助!aapoker辅助是真的吗,werplan怎么透视(详细辅助介绍教程)1、下载好aapok...
透视实锤!wpk辅助器安装(透... 透视实锤!wpk辅助器安装(透视)详细辅助微扑克教程(一直有挂)1、全新机制【wpk辅助器安装软件透...
透视透视(WePoKer)wp... 透视透视(WePoKer)wpk发牌算法(透视)详细辅助介绍教程;支持2-10人实时对战,虚拟庄家发...
透视工具!拱趴大菠萝辅助神器(... 透视工具!拱趴大菠萝辅助神器(透视)果然真的有挂(详细辅助细节方法);支持2-10人实时对战,虚拟庄...
透视透视!智星德州菠萝插件官网... 透视透视!智星德州菠萝插件官网,智星德州插件2024最新版(详细辅助黑科技教程);1、实时智星德州菠...