Page 1 of 3

Adding sound effect to response menu

Posted: Sat Jan 11, 2020 8:45 am
by lostmushroom
Hey guys. Is there a way to add a sound effect to response menu choices? I'm making a text game and I'd like to have audio feedback whenever you click a menu choice. Just wondering if there's a way to do this for all choices in game, without having to add it into the Sequencer of every choice node.

Thanks :)

Re: Adding sound effect to response menu

Posted: Sat Jan 11, 2020 10:14 am
by Tony Li
Hi,

Add an Audio Source to the main dialogue UI GameObject. It can be on any GameObject that stays active after you click the response button. The main dialogue UI GameObject is a handy one to choose. Untick the Audio Source's Play On Awake and Loop.

Inspect the response buttons' OnClick() events. Configure them to call the Audio Source's Play() or PlayOneShot() method. If you select Play(), assign an audio clip to the Audio Source. If you select PlayOneShot(), assign an audio clip to the OnClick() event.

Re: Adding sound effect to response menu

Posted: Sat Jan 11, 2020 11:04 am
by lostmushroom
It works perfectly :) Thanks!

Re: Adding sound effect to response menu

Posted: Sat Jan 11, 2020 12:56 pm
by Tony Li
Glad to help! :)

Re: Adding sound effect to response menu

Posted: Sun Jul 26, 2020 2:54 pm
by karakori
Hope it's okay to bump this, wasn't sure if I should create a new topic or not.

Along similar lines, is it possible to add a sound effect when switching between response options? i.e.

Image

I didn't see a field for it on any of the response menu panel game objects - would it have to be done via custom code?

Re: Adding sound effect to response menu

Posted: Sun Jul 26, 2020 5:23 pm
by Tony Li
Hi,

Add an Event Trigger component to each UI Button. Add a Select() event, and configure it to play a sound on an Audio Source. You can put an Audio Source on the Button itself. Untick Play On Awake, and assign the sound you want to play when that Button is switched to.

Re: Adding sound effect to response menu

Posted: Sun Jul 26, 2020 6:29 pm
by karakori
Once again, thank you Tony!

Re: Adding sound effect to response menu

Posted: Sun Jul 26, 2020 8:33 pm
by Tony Li
Glad to help! You can also try assigning it to Deselect instead of Select if you like that effect better.

Re: Adding sound effect to response menu

Posted: Wed Jul 29, 2020 4:12 pm
by alfa995
Sorry to randomly show up but I was trying to do that and had a problem. If I set the sound playing on Select(), it plays as soon as the response menu opens (since it auto-focuses and selects a button) but if I set it on Deselect(), then it plays after selecting a response, since it disables and thus deselects all the buttons.
Is there a way around that? I think it'd also look nicer if the selected button remained selected after pressing it, and just disable navigation and further presses. Is there a way to do that that works with mouse and keyboard?

Re: Adding sound effect to response menu

Posted: Wed Jul 29, 2020 5:08 pm
by Tony Li
Hi,

The solution above is decent -- and, more importantly, relatively easy to implement since it doesn't require any scripting. But it does have the behaviors that you mentioned.

If you were only using mouse, you could use the PointerEnter() event to play the sound, which won't play as soon as the response menu opens. But it will only play when you mouse over a button, not when you navigate to it using keyboard/joystick.

To play sounds for keyboard/joystick navigation, too, you'd want to point the Select() event to a custom script, typically on the response menu GameObject. The script would have two methods:
  • PlayNavigationSound: Plays the navigation sound unless a bool variable has been set true. Point the Select() event to this method.
  • OnEnable: (Invoked when the response menu is opened.) Sets the bool variable true for one frame, then sets it false.
alfa995 wrote: Wed Jul 29, 2020 4:12 pmI think it'd also look nicer if the selected button remained selected after pressing it, and just disable navigation and further presses. Is there a way to do that that works with mouse and keyboard?
How would you want this to work? Normally, as soon as you click a response button, the response menu disappears.