Adding sound effect to response menu

Announcements, support questions, and discussion for the Dialogue System.
lostmushroom
Posts: 195
Joined: Mon Jul 01, 2019 1:21 pm

Adding sound effect to response menu

Post 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 :)
User avatar
Tony Li
Posts: 22098
Joined: Thu Jul 18, 2013 1:27 pm

Re: Adding sound effect to response menu

Post 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.
lostmushroom
Posts: 195
Joined: Mon Jul 01, 2019 1:21 pm

Re: Adding sound effect to response menu

Post by lostmushroom »

It works perfectly :) Thanks!
User avatar
Tony Li
Posts: 22098
Joined: Thu Jul 18, 2013 1:27 pm

Re: Adding sound effect to response menu

Post by Tony Li »

Glad to help! :)
karakori
Posts: 11
Joined: Sat Jul 25, 2020 3:36 pm

Re: Adding sound effect to response menu

Post 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?
User avatar
Tony Li
Posts: 22098
Joined: Thu Jul 18, 2013 1:27 pm

Re: Adding sound effect to response menu

Post 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.
karakori
Posts: 11
Joined: Sat Jul 25, 2020 3:36 pm

Re: Adding sound effect to response menu

Post by karakori »

Once again, thank you Tony!
User avatar
Tony Li
Posts: 22098
Joined: Thu Jul 18, 2013 1:27 pm

Re: Adding sound effect to response menu

Post by Tony Li »

Glad to help! You can also try assigning it to Deselect instead of Select if you like that effect better.
alfa995
Posts: 30
Joined: Mon Dec 03, 2018 11:31 pm

Re: Adding sound effect to response menu

Post 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?
User avatar
Tony Li
Posts: 22098
Joined: Thu Jul 18, 2013 1:27 pm

Re: Adding sound effect to response menu

Post 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.
Post Reply