如何查看是否执行了 PlayerInput 操作? Unity,新的输入系统
How to see if PlayerInput action was performed? Unity, New Input System
我正在使用新的 inptu 系统,我想要一种方法来查看输入操作是否已执行(仅一次,如 GetButtonDown)。
如果我使用“.ReadValue()”,if 语句 returns 不仅一次为真,而且只要按住“inventoryAction 按钮”就一直为真。
private void Awake()
{
playerInput = GetComponent<PlayerInput>();
inventoryAction = playerInput.actions["Inventory"];
}
private void Update()
{
if (inventoryAction.ReadValue<float>() == 1)
{
Debug.Log("inventory key was pressed");
}
}
如何(使用新的输入系统)在 if 语句中只获取“已执行”部分?新输入法有GetButtonDown之类的吗?
从版本 1.1.1 开始,动作有一个 WasPressedThisFrame
方法,基本上可以做到这一点。
文档(上面链接)包括以下代码片段:
var fire = playerInput.actions["fire"];
if (fire.WasPressedThisFrame() && fire.IsPressed())
StartFiring();
else if (fire.WasReleasedThisFrame())
StopFiring();
如代码示例所示,还有 WasReleasedThisFrame
和 IsPressed
,它们本质上分别是 GetButtonUp
和 GetButton
。
请注意,有一些特殊的注意事项使它们的行为不同于 GetButtonX
调用,尤其是在按钮释放和按下阈值有很大不同的情况下,或者在一个帧中多次按下和释放按钮的情况下。
This method is different from simply reading the action's current float
value and comparing it to the press threshold and is also different from comparing the current actuation of activeControl
to it. This is because the current level of actuation might have already fallen below the press threshold but might not yet have reached the release threshold.
This method works with any type of action, not just buttons.
Also note that because this operates on the results of EvaluateMagnitude()
, it works with many kind of controls, not just buttons. For example, if an action is bound to a StickControl
, the control will be considered "pressed" once the magnitude of the Vector2
of the control has crossed the press threshold.
Finally, note that custom button press points of controls (see pressPoint
) are respected and will take precedence over defaultButtonPressPoint
.
1.1.1 WasPressedThisFrame docs:
This method is different from WasPerformedThisFrame()
in that it is
not bound to phase. Instead, if the action's level of actuation (that
is, the level of magnitude -- see EvaluateMagnitude()
-- of the
control(s) bound to the action) crossed the press threshold (see
defaultButtonPressPoint
) at any point in the frame, this method will
return true
. It will do so even if there is an interaction on the
action that has not yet performed the action in response to the press.
This method works with any type of action, not just buttons.
Also note that because this operates on the results of
EvaluateMagnitude()
, it works with many kind of controls, not just
buttons. For example, if an action is bound to a StickControl
, the
control will be considered "pressed" once the magnitude of the Vector2
of the control has crossed the press threshold.
Finally, note that custom button press points of controls (see
pressPoint
) are respected and will take precedence over
defaultButtonPressPoint
.
请务必阅读您的版本的文档以获取更多信息。
我正在使用新的 inptu 系统,我想要一种方法来查看输入操作是否已执行(仅一次,如 GetButtonDown)。 如果我使用“.ReadValue()”,if 语句 returns 不仅一次为真,而且只要按住“inventoryAction 按钮”就一直为真。
private void Awake()
{
playerInput = GetComponent<PlayerInput>();
inventoryAction = playerInput.actions["Inventory"];
}
private void Update()
{
if (inventoryAction.ReadValue<float>() == 1)
{
Debug.Log("inventory key was pressed");
}
}
如何(使用新的输入系统)在 if 语句中只获取“已执行”部分?新输入法有GetButtonDown之类的吗?
从版本 1.1.1 开始,动作有一个 WasPressedThisFrame
方法,基本上可以做到这一点。
文档(上面链接)包括以下代码片段:
var fire = playerInput.actions["fire"]; if (fire.WasPressedThisFrame() && fire.IsPressed()) StartFiring(); else if (fire.WasReleasedThisFrame()) StopFiring();
如代码示例所示,还有 WasReleasedThisFrame
和 IsPressed
,它们本质上分别是 GetButtonUp
和 GetButton
。
请注意,有一些特殊的注意事项使它们的行为不同于 GetButtonX
调用,尤其是在按钮释放和按下阈值有很大不同的情况下,或者在一个帧中多次按下和释放按钮的情况下。
This method is different from simply reading the action's current
float
value and comparing it to the press threshold and is also different from comparing the current actuation ofactiveControl
to it. This is because the current level of actuation might have already fallen below the press threshold but might not yet have reached the release threshold.This method works with any type of action, not just buttons.
Also note that because this operates on the results of
EvaluateMagnitude()
, it works with many kind of controls, not just buttons. For example, if an action is bound to aStickControl
, the control will be considered "pressed" once the magnitude of theVector2
of the control has crossed the press threshold.Finally, note that custom button press points of controls (see
pressPoint
) are respected and will take precedence overdefaultButtonPressPoint
.
1.1.1 WasPressedThisFrame docs:
This method is different from
WasPerformedThisFrame()
in that it is not bound to phase. Instead, if the action's level of actuation (that is, the level of magnitude -- seeEvaluateMagnitude()
-- of the control(s) bound to the action) crossed the press threshold (seedefaultButtonPressPoint
) at any point in the frame, this method will returntrue
. It will do so even if there is an interaction on the action that has not yet performed the action in response to the press.This method works with any type of action, not just buttons.
Also note that because this operates on the results of
EvaluateMagnitude()
, it works with many kind of controls, not just buttons. For example, if an action is bound to aStickControl
, the control will be considered "pressed" once the magnitude of theVector2
of the control has crossed the press threshold.Finally, note that custom button press points of controls (see
pressPoint
) are respected and will take precedence overdefaultButtonPressPoint
.
请务必阅读您的版本的文档以获取更多信息。