当前位置: 代码网 > it编程>游戏开发>unity > Unity XR Interaction Toolkit设置或监听手柄按键事件(三)

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

2024年08月03日 unity 我要评论
官方文档Input System:链接:安装部分:链接: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
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

二、按键对应名称

在这里插入图片描述

三、代码示例

在这里插入图片描述

在这里插入图片描述

代码如下(示例):


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

总结

好记性不如烂笔头

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com