- Home /
Gamepad press and release in Input Action - UI Buttons
Hi!
I am currently implementing the UI for my game and I'm hoping to keep it cross platform. One thing I'm missing when using gamepads is the pressed and released states for UI Buttons.
I am using the Input Actions system. My hope is that when a button is selected, I would hold the gamepad button down and the UI button should enter the pressed state. Once the gamepad button is released the submit event should be activated, resulting in the button's action being activated.
Currently the submit event is activated when the gamepad button is pressed (not released)
I have tried adding an Interaction for the Press type and that the behaviour to Release Only. But this resulted in the submit event never being fired.
Could anyone point me in the right direction?
Thanks, Joe
Answer by jhubbard0221 · Jul 17, 2020 at 05:32 PM
It looks like you'll want to use the .performed and the .canceled functions. These allow the pressing and releasing of the buttons to have their own functions.
Here's an example using a character controller component:
public class PlayerController : MonoBehaviour
{
{
PlayerControls controls;
public float movementSpeed = 7f;
}
void Awake()
{
controls.Gameplay.Run.performed += ctx => Run();
controls.Gameplay.Run.canceled += ctx => Walk();
}
void Run()
{
movementSpeed = 14f;
Debug.Log("Runnin");
}
void Walk()
{
movementSpeed = 7f;
Debug.Log("Walkin");
}
void OnEnable()
{
controls.Gameplay.Enable();
}
void OnDisable()
{
controls.Gameplay.Disable();
}
}
This script would allow you go faster with the button pressed and revert back to original speed once in is released. Anything can be put in the .performed or .canceled functions, this is just my example. To you should be able to put your Submit event in the .canceled function.
I'm wondering more specifically if this is possible through the Input Action system and the ui button component that is supplied with the engine. The button component subscribes to the Submit event, and i suspect that should be able to configure the Input Actions system to only send that event on gamepad button release
Your answer
Follow this Question
Related Questions
Find out if any Button on any Gamepad has been pressed and which one 6 Answers
Help with a button 1 Answer
Creating a menu for gamepad? 1 Answer
Unity Editor swallows gamepad input 0 Answers