Perform an input action on a UI element

Hello.
I want to call an Input Action using a UI element directly through the Unity Input System. I encountered a problem because the Controls array will be empty on a mobile device because my Bindings array will be empty. How can I call InputAction through the Unity Input System so that I get performed in my code?

Here is my code:

[SerializeField] private InputActionReference actionReference;

public void OnPointerDown(PointerEventData eventData)
{
    if (actionReference != null && actionReference.action != null)
    {
        var controls = actionReference.action.controls;
        if (controls.Count > 0)
        {
            var control = controls[0];
            var device = control.device;

            using (StateEvent.From(device, out var eventPtr))
            {
                control.WriteValueIntoEvent(1.0f, eventPtr);

                UnityEngine.InputSystem.InputSystem.QueueEvent(eventPtr);
            }
        }
    }
}

You don’t want your UI to call your inputs. You want it to call what the input would be calling.

Thanks for your feedback, however I want to call an Input Action using a button click, for example I want to call the Input Aaction “Jump” through the Unity Input System. Unity has a solution, for example this is On-ScreenButton, but this component is bound to a specific key, which does not suit me

1 Like

You really don’t. There’s no need to pass this button through the input system. Just have the UI Button call whatever is needed to make the player jump.

Trust me, trying to go through the input system is not the correct way to handle this.

Or you can invert the relationship, and have the player controller or whatnot hook into the on-screen buttons.

In any case you don’t gain anything out of going through the input system here.

1 Like