1
\$\begingroup\$

As title, I know Button can be triggered onclick by button.onClick.Invoke();

But how to simulate onclick event of Selectable?

Thanks for the help.

\$\endgroup\$
3
  • \$\begingroup\$ Are you trying to select the object as with EventSystem.SetSelectedGameObject? \$\endgroup\$
    – DMGregory
    Commented Oct 20, 2020 at 6:30
  • \$\begingroup\$ @DMGregory I am select the object, but cannot submit/click it. \$\endgroup\$
    – qwerty
    Commented Oct 20, 2020 at 14:53
  • 1
    \$\begingroup\$ A Selectable can be Selected on click. If it has another reaction to being clicked, that might be implemented by a more specific derived class. Can you tell us more about what this specific object is, and how you've set it up in the Unity Inspector? \$\endgroup\$
    – DMGregory
    Commented Oct 20, 2020 at 15:12

2 Answers 2

3
\$\begingroup\$

As you know:

button.onClick.Invoke();

Acts like the user has clicked the button, invoking callbacks, but there's no visual effect on the button itself.

Do you also want the Selectable to be highlighted as if you'd moused-over it and clicked on it? I.e. a "simulated" click?

If so, the following code works for at least Buttons:

public static void SimulateClick(Button button)
{
    ExecuteEvents.Execute(button.gameObject, new BaseEventData(EventSystem.current), ExecuteEvents.submitHandler);
}

From my (limited) understanding, this uses the EventSystem to create a new "Submit" event on the Button object, as if the user had clicked on it.

I tried it for a Toggle too, and it seems to works fine (Unity 2019.4).

I'm not sure if this is the best way to do it, but it seems legitimate since it's making use of the EventSystem which is designed to manage this kind of interaction.

\$\endgroup\$
1
\$\begingroup\$

You can actually create your own click event by overriding the OnPointerDown(PointerEventData eventData) method. This works perfectly well for normal Mouse Input but sadly not directly for controller input. My code using this method is this:

public override void OnPointerDown(PointerEventData eventData)
{
    base.OnPointerDown(eventData);

    if(!IsActive() || !IsInteractable())
    {
        return;
    }

    //Do stuff here i.e. invoke event or method
}

You can even add the a on click event assignable in the inspector (Like the one on a button element). If you'd like to do that make sure to implement using static UnityEngine.UI.Button; and make a SerializeField private ButtonClickedEvent m_OnClick = new ButtonClickedEvent(); Then you can assign your method in the inspector and call m_OnClick.Invoke(); in the above Method.

This approach is actually pretty similar to the general "Button" UI element, so using this or simply a button will probably not make much of a difference. Nevertheless, this works for me!

I also found out how to add an onClick event that can also be triggered by a controller (by the whole event system):

If you click on the points of a component in the inspector, you can sometimes "edit" the script, even if it is a Unity pre-built component like "Button". This helped me to understand the structure of a Button and helped me to find the solution.

You can implement the interface ISubmitHandler (and others like ICancelHandler , ...) to implement an OnSubmit method which is called every time the submit action, referenced in the eventsystem, is triggered. You can use these to get this working with controller and keyboard!

Hope that helps, cheers! :)

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .