On Click () on a button without the mouse

Hi there,
is possible to call OnClick on a button without the mouse input? I do not want to use the mouse. Instead the mouse cursor I want to use my own point on the screen. I would like to call OnClick using left mouse button and using button of a gamepad.
Thanks.

You can trigger a UnityEvent at any time by doing something like this:

yourButton.onClick.Invoke( );

Assuming you are using Unity’s built-in widget navigation and selection, you may also want to look at adding an EventTrigger component and then an OnSubmit event in that EventTrigger. I think OnSubmit is intended for handling the gamepad/keyboard equivalent of clicking, but I’m really just guessing. I haven’t worked w/ gamepad/keyboard UI input yet personally.

1 Like

Base on casimps1 answer, next sample I created for VR to click with Bluetooth joystick.

  1. Attach this class to a button
  2. Add event trigger to a button
  3. On pointer enter call Activate()
  4. On pointer exit call Deactivate();
  5. Enter key name you want to use.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class HomeButton1 : MonoBehaviour {
    public string key;
    private bool active;
    private SettingsJson sj;

    void Start () {
        active = false;
        sj = GameObject.Find ("Settings").GetComponent<SettingsJson> ();
        b = GetComponent<Button>();
    }

    void Update () {
        if (active == true && Input.GetKeyDown (key)) {
            b.onClick.Invoke ();
        }
    }

    public void Activate(){
        active = true;
    }

    public void Deactivate(){
        active = false;
    }
}