Is there a general event for button onclick?

I want to add sound for UI button onClicks but I don't think adding audio.Play() to every button onClicks is logical. Is there anything on EventSystem that runs when any button clicked in unity?

Upvotes: 2

Views: 1003

Answers (2)

Christopher
Christopher

Reputation: 9804

Subclassing might be a more efficient way then what you came up with.

//subclass the button
class LoudButton : Button {
    //constructor
    public LoudButton() : base () {
        //Have the constructor add the Event.
        //Optionally, hand the specific sound in as a argument and store it in a property
        base.OnClick += /**/;
    }
}

Upvotes: 2

@zafar suggested a thread and I found a solution like this:

void Update()
{
    if(Input.GetMouseButtonDown(0))
                if (EventSystem.current.currentSelectedGameObject.GetComponent<Button>())
                    soundManager.Play("Tap");
}

Upvotes: 2

Related Questions