Unity XR Interaction Toolkit设置或监听手柄按键事件(三)

提示:文章有错误的地方,还望诸位大神不吝指教!

文章目录


前言

官方文档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



二、按键对应名称

三、代码示例

代码如下(示例):

csharp 复制代码
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;
            }
        }

        /// <summary>
        /// 左手X键
        /// </summary>
        /// <param name="context"></param>
        private void OnMyLeftButton_X_Action(InputAction.CallbackContext context)
        {
            Debug.Log("按下左手X键--------------------");
        }
        /// <summary>
        /// 左手Y键
        /// </summary>
        /// <param name="context"></param>
        private void OnMyLeftButton_Y_Action(InputAction.CallbackContext context)
        {
            Debug.Log("按下左手Y键--------------------");
        }


        /// <summary>
        /// 右手X键
        /// </summary>
        /// <param name="context"></param>
        private void OnMyRightButton_X_Action(InputAction.CallbackContext context)
        {
            Debug.Log("按下右手A键--------------------");
        }
        /// <summary>
        /// 右手Y键
        /// </summary>
        /// <param name="context"></param>
        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
        }
    }
}

总结

好记性不如烂笔头

相关推荐
Magnum Lehar23 分钟前
3d游戏引擎的Utilities模块实现下
c++·算法·游戏引擎
Flamesky33 分钟前
Unity编辑器重新编译代码
unity·重新编译
虾球xz4 小时前
游戏引擎学习第277天:稀疏实体系统
c++·学习·游戏引擎
虾球xz7 小时前
游戏引擎学习第276天:调整身体动画
c++·学习·游戏引擎
虾球xz7 小时前
游戏引擎学习第275天:将旋转和剪切传递给渲染器
c++·学习·游戏引擎
虾球xz12 小时前
游戏引擎学习第268天:合并调试链表与分组
c++·学习·链表·游戏引擎
qq_59821175717 小时前
Unity.UGUI DrawCall合批笔记
笔记·unity·游戏引擎
南玖yy1 天前
C/C++ 内存管理深度解析:从内存分布到实践应用(malloc和new,free和delete的对比与使用,定位 new )
c语言·开发语言·c++·笔记·后端·游戏引擎·课程设计
Tech Synapse1 天前
Unity ML-Agents实战指南:构建多技能游戏AI训练系统
人工智能·游戏·unity
虾球xz1 天前
游戏引擎学习第272天:显式移动转换
c++·学习·游戏引擎