Unity Input System 笔记

本文详细讲解了如何在C#中通过InputSystem处理button类型的事件,包括单个事件绑定与InputSystem接口的使用,以及不同阶段的事件处理方法。

一.监视一个ActionType 为button 的:按下,激活,释放

两种方式实现:

下面的InputTest,生成C#类才有的,要有这一步

          1.使用单个事件分别绑定

        InputTest input;
 
        public bool leftControl;  
 
        private void OnEnable()
        {
            input = new InputTest();
             
            input.Gamepay.aweq.performed += Aweq_performed;
            input.Gamepay.aweq.canceled += Aweq_canceled;
            input.Gamepay.aweq.started += Aweq_started; 
            input.Enable();
        }

        private void Aweq_started(InputAction.CallbackContext obj)
        {
            Debug.Log("开始");
            leftControl = obj.ReadValueAsButton();
        }
        private void Aweq_performed(UnityEngine.InputSystem.InputAction.CallbackContext obj)
        {
            Debug.Log("激活");
            leftControl = obj.ReadValueAsButton();
        }
        private void Aweq_canceled(InputAction.CallbackContext obj)
        {
            Debug.Log("释放");
            leftControl = obj.ReadValueAsButton();

        } 

2.实现inputsystem自动给你的接口,然后在接口中做不同类型的判断,获取你想要的数据

        实现接口

    
    public interface IGamepayActions //这个是 inputsystem  自动生成的
    { 
        void OnAweq(InputAction.CallbackContext context);
    }
    


    public class inputcallback : IGamepayActions
    { 
        public void OnAweq(InputAction.CallbackContext context)
        {
            switch (context.phase)
            {
                case InputActionPhase.Disabled:
                    break;
                case InputActionPhase.Waiting:
                    break;
                case InputActionPhase.Started:
                    Debug.Log("按下"+context.ReadValueAsButton());
                    break;
                case InputActionPhase.Performed:
                    Debug.Log("激活" + context.ReadValueAsButton());
                    break;
                case InputActionPhase.Canceled:
                    Debug.Log("释放" + context.ReadValueAsButton());
                    break;
                default:
                    break;
            }
         
        } 
    }

        设置对应回调

        InputTest input; 
        private void OnEnable()
        {
            input = new InputTest(); 
            inputcallback inc = new inputcallback();
            input.Gamepay.SetCallbacks(inc); 
            input.Enable();
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值