一.监视一个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();
}
本文详细讲解了如何在C#中通过InputSystem处理button类型的事件,包括单个事件绑定与InputSystem接口的使用,以及不同阶段的事件处理方法。

5940

被折叠的 条评论
为什么被折叠?



