簡體   English   中英

按鈕 onclick 是否有一般事件?

[英]Is there a general event for button onclick?

我想為 UI 按鈕 onClicks 添加聲音,但我不認為將 audio.Play() 添加到每個按鈕 onClicks 是合乎邏輯的。 當統一單擊任何按鈕時,EventSystem 上是否有任何東西運行?

@zafar 建議了一個線程,我找到了這樣的解決方案:

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

子類化可能是比你想出的更有效的方法。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM