Adding sound effect to response menu

Announcements, support questions, and discussion for the Dialogue System.
alfa995
Posts: 30
Joined: Mon Dec 03, 2018 11:31 pm

Re: Adding sound effect to response menu

Post by alfa995 »

Yeah dealing with that behavior does require the extra script so it's not as simple but it worked, thank you! :D

For the response menu, if the hide animation is not instant you can see all options get deselected, including the one you picked. In some games the button remains selected/highlighted etc as the menu disappears, just a minor cosmetic thing I tried to replicate, but it'd probably be complicated.
User avatar
Tony Li
Posts: 21679
Joined: Thu Jul 18, 2013 1:27 pm

Re: Adding sound effect to response menu

Post by Tony Li »

The Standard Dialogue UI makes the buttons non-clickable so the player can't accidentally click the response button a second time while the menu is hiding. It does this by settings the UI Buttons' interactable properties false.

Here's a patch that makes StandardUIMenuPanel.MakeButtonsNonclickable() virtual so you can override it:

DS_MenuPanelPatch_2020-07-29.unitypackage

This change will also be in the next release (2.2.9).

You could make a subclass that overrides two methods:

Override ShowResponses() to set the menu panel's CanvasGroup.interactable true and then call the base method.

Override MakeButtonsNonclickable() to set CanvasGroup.interactable false.

Example:

Code: Select all

public class CustomMenuPanel : StandardUIMenuPanel
{
    private CanvasGroup canvasGroup;
    public virtual override Awake()
    {
        base.Awake();
        canvasGroup = GetComponent<CanvasGroup>(); // Assumes menu has canvas group.
    }
    
    public override void ShowResponses(Subtitle subtitle, Response[] responses, Transform target)
    {
        canvasGroup.interactable = true;
        base.ShowResponses();
    }
    
    public override void MakeButtonsNonclickable()
    {
        canvasGroup.interactable = false;
    }
}
alfa995
Posts: 30
Joined: Mon Dec 03, 2018 11:31 pm

Re: Adding sound effect to response menu

Post by alfa995 »

Yeah making buttons non-clickable is more important, I just wish Unity had a way to do it without disabling/deselecting the button. I tried the script but the problem is the same, setting the canvas group to non-interactable deselects and disables all the buttons. I also thought of enabling an empty canvas that blocks raycasts but I imagine that wouldn't work with keyboard navigation.

Don't worry again it's just a minor thing, trying to get it to work would probably be more effort than it's worth. Plus that's not what the thread was originally about so I shouldn't change the topic. Thanks again though!
User avatar
Tony Li
Posts: 21679
Joined: Thu Jul 18, 2013 1:27 pm

Re: Adding sound effect to response menu

Post by Tony Li »

I don't mind bouncing around a few ideas. It might be helpful to others in the future, too.

You could do the following:

1. While hiding the menu, disable the Canvas's GraphicRaycaster component. Rough example:

Code: Select all

var canvas = DialogueManager.instance.GetComponentInChildren<Canvas>();
canvas.GetComponent<GraphicRaycaster>().enabled = false;
This will prevent mouse clicks.

2. Then set the buttons' Navigation to None. Rough example, assuming you're using instantiated response buttons:

Code: Select all

var noNav = new Navigation();
noNav = Navigation.Mode.None;
foreach (var go in menuPanel.instantiatedButtons)
{
    go.GetComponent<Button>().navigation = noNav;
}
The StandardUIMenuPanel sets explicit navigation whenever it sets up a new menu, so you don't need to worry about setting navigation back from None.
alfa995
Posts: 30
Joined: Mon Dec 03, 2018 11:31 pm

Re: Adding sound effect to response menu

Post by alfa995 »

Still some issues.

In mouse mode:
- The button doesn't remain selected after clicking, although it does prevent multiple clicks.

In keyboard mode:
- Navigation is correctly blocked after pressing a button, but still allows multiple presses while the menu hides. (Can be fixed with a "clickAllowed" bool in each button though so no big deal)
- But the bigger issue is.. like in mouse mode the chosen button doesn't remain selected, though instead, for some reason, it selects the first response.

Maybe it's a problem with settings or Unity UI or my own setup, but even if I can prevent multiple clicks and navigation in both modes, I can't get the chosen option to remain in its Selected state after clicking, which is the main thing I was trying to achieve. Even forcing it back to a Selected state afterwards might not work, since if the event triggers again, it would play the selected sound after clicking.
User avatar
Tony Li
Posts: 21679
Joined: Thu Jul 18, 2013 1:27 pm

Re: Adding sound effect to response menu

Post by Tony Li »

alfa995 wrote: Thu Jul 30, 2020 4:20 pmIn mouse mode:
- The button doesn't remain selected after clicking, although it does prevent multiple clicks.
Try unticking the menu panel's 'Select Previous On Disable' checkbox. I'm not sure what's going on in your case, but when closing the menu that checkbox will definitely move the selection to whatever was selected before the menu.

This is a hack, but when a response button is clicked, what if you set the selected button's disabled state (or normal state, depending on what it's transitioning to) to the same color or sprite as the selected state. So the button will technically be in the disabled/normal state, but it will look like it's selected.
alfa995
Posts: 30
Joined: Mon Dec 03, 2018 11:31 pm

Re: Adding sound effect to response menu

Post by alfa995 »

Tried it but still doesn't work.
Also the problem is not just the color of the button itself, for my setup I set text colors/animation when a button is selected and set it back to normal when it's deselected using event triggers. So if at any point clicking deselects the chosen response, even if I try to force it back it will probably play the sound again and restart the animation which might look weird, definitely not worth the hacky solution.
User avatar
Tony Li
Posts: 21679
Joined: Thu Jul 18, 2013 1:27 pm

Re: Adding sound effect to response menu

Post by Tony Li »

Hi,

If possible, you might want to get this to work in a simple scene with a basic UI, not a full blown Dialogue System dialogue UI. Once it works the way you want, apply the same setup to the dialogue UI. If it doesn't work there, let me know and I can take a look at how it interacts with the StandardUIMenuPanel code.
alfa995
Posts: 30
Joined: Mon Dec 03, 2018 11:31 pm

Re: Adding sound effect to response menu

Post by alfa995 »

I'm not even sure how I'd get it to work with regular UI. From what I've seen checking around, seems the most straightforward way is with a Toggle Group instead of regular buttons and trigger the click behavior when OnValueChanged returns true, but no idea how much of the system would need to be reworked just for that.
User avatar
Tony Li
Posts: 21679
Joined: Thu Jul 18, 2013 1:27 pm

Re: Adding sound effect to response menu

Post by Tony Li »

I think it would just require overriding the StandardUIMenuPanel's ShowResponses() method. I'll give it a try this weekend and let you know how it goes.
Post Reply