简体   繁体   中英

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?

@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");
}

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 += /**/;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM