Using keybind y and n for choosing a yes no response

Announcements, support questions, and discussion for the Dialogue System.
vinchenamigo
Posts: 10
Joined: Tue May 13, 2025 4:09 am

Using keybind y and n for choosing a yes no response

Post by vinchenamigo »

I would like to know if there's any way to set the response menu input to the keyboard button, Y and N?
it was for a menu for buy clue, so the response of the PC was Yes or No, i need it because the game must be able to be played fully using keyboard, thanks
Attachments
Screenshot 2025-05-13 at 15.14.24.png
Screenshot 2025-05-13 at 15.14.24.png (76.22 KiB) Viewed 12056 times
User avatar
Tony Li
Posts: 23161
Joined: Thu Jul 18, 2013 1:27 pm

Re: Using keybind y and n for choosing a yes no response

Post by Tony Li »

Hi,

On your dialogue UI's StandardUIMenuPanel component, if you tick Autonumber you can keybind number keys (1, 2, 3, etc.) to the response menu buttons.

Keybinding Y and N will require a little scripting, however. Here are two ways you could do it by making a subclass of StandardUIMenuPanel and overriding the SetResponseButton() method.

1. This first approach always sets the first button's keybind to Y and the second to N. (This assumes you've ticked Autonumber.)

Code: Select all

protected override void SetResponseButton(StandardUIResponseButton button, Response response, Transform target, int buttonNumber)
{
    base.SetResponseButton(button, response, target, buttonNumber);
    var keyTrigger = button.GetComponent<UIButtonKeyTrigger>();
    if (keyTrigger != null)
    {
        if (buttonNumber == 0) keyTrigger.key = KeyCode.Y;
        else if (buttonNumber == 1) keyTrigger.key = KeyCode.N;
    }
}
2. Or add a custom field named "Keybind" to your response dialogue entries. Set it to "Y" to keybind Y or "N" to keybind N to the button.

Code: Select all

protected override void SetResponseButton(StandardUIResponseButton button, Response response, Transform target, int buttonNumber)
{
    base.SetResponseButton(button, response, target, buttonNumber);
    var keybind = Field.LookupValue(response.destinationEntry.fields, "Keybind");
    bool bindToY = string.Equals(keybind, "Y", System.StringComparison.OrdinalIgnoreCase);
    bool bindToN = string.Equals(keybind, "N", System.StringComparison.OrdinalIgnoreCase);
    if (bindToY || bindToN)
    {
        var keyTrigger = button.GetComponent<UIButtonKeyTrigger>() ?? button.gameObject.AddComponent<UIButtonKeyTrigger>();
        keyTrigger.key = bindToY ? KeyCode.Y : KeyCode.N;
    }
}
(Note: I typed this code directly into the reply here. There might be a typo or two.)
vinchenamigo
Posts: 10
Joined: Tue May 13, 2025 4:09 am

Re: Using keybind y and n for choosing a yes no response

Post by vinchenamigo »

Hello, is it correct that the one file that changed was UnityUIResponseMenuControls.cs? i have applied the second way, but it didnt work
vinchenamigo
Posts: 10
Joined: Tue May 13, 2025 4:09 am

Re: Using keybind y and n for choosing a yes no response

Post by vinchenamigo »

heres the function if you forgotten

Code: Select all

        private void SetResponseButton(UnityUIResponseButton button, Response response, Transform target, int buttonNumber)
        {
            if (button != null)
            {
                button.visible = true;
                button.clickable = response.enabled;
                button.target = target;
                if (response != null) button.SetFormattedText(response.formattedText);
                button.response = response;
                var keybind = Field.LookupValue(response.destinationEntry.fields, "Keybind");
                bool bindToY = string.Equals(keybind, "Y", System.StringComparison.OrdinalIgnoreCase);
                bool bindToN = string.Equals(keybind, "N", System.StringComparison.OrdinalIgnoreCase);
                if (bindToY || bindToN)
                {
                    var keyTrigger = button.GetComponent<UIButtonKeyTrigger>() ?? button.gameObject.AddComponent<UIButtonKeyTrigger>();
                    keyTrigger.key = bindToY ? KeyCode.Y : KeyCode.N;
                }
                // Auto-number:
                if (autonumber.enabled)
                {
                    button.Text = string.Format(autonumber.format, buttonNumber + 1, button.Text);
                    var keyTrigger = button.GetComponent<UIButtonKeyTrigger>();
                    if (autonumber.regularNumberHotkeys)
                    {
                        if (keyTrigger == null) keyTrigger = button.gameObject.AddComponent<UIButtonKeyTrigger>();
                        keyTrigger.key = (KeyCode)((int)KeyCode.Alpha1 + buttonNumber);
                    }
                    if (autonumber.numpadHotkeys)
                    {
                        if (autonumber.regularNumberHotkeys || keyTrigger == null) keyTrigger = button.gameObject.AddComponent<UIButtonKeyTrigger>();
                        keyTrigger.key = (KeyCode)((int)KeyCode.Keypad1 + buttonNumber);
                    }
                }
            }
        }
either way, thanks Tony!
User avatar
Tony Li
Posts: 23161
Joined: Thu Jul 18, 2013 1:27 pm

Re: Using keybind y and n for choosing a yes no response

Post by Tony Li »

Hi,

Don't use UnityUIDialogueUI. It's deprecated. Please use StandardDialogueUI. (See Dialogue UIs.)

If you're going with option #2, make a subclass of StandardUIMenuPanel. (Don't directly edit StandardUIMenuPanel.) Use the override method I suggested above. In your dialogue UI, you can replace StandardUIMenuPanel with the subclass in-place and retain the UI element assignments. See: How To: Replace Script with Subclass and Keep Field Assignments
vinchenamigo
Posts: 10
Joined: Tue May 13, 2025 4:09 am

Re: Using keybind y and n for choosing a yes no response

Post by vinchenamigo »

Hi tony,

Thank you so much for the insights! I have applied it to the wrapped subclass as follows:

Code: Select all

    public class StandardUIMenuPanel : DialogueSystem.StandardUIMenuPanel
    {
        protected void SetResponseButton(StandardUIResponseButton button, Response response, Transform target, int buttonNumber)
        {
            Debug.Log("called");
            base.SetResponseButton(button, response, target, buttonNumber);
            var keybind = Field.LookupValue(response.destinationEntry.fields, "Keybind");
            bool bindToY = string.Equals(keybind, "Y", System.StringComparison.OrdinalIgnoreCase);
            bool bindToN = string.Equals(keybind, "N", System.StringComparison.OrdinalIgnoreCase);
            if (bindToY || bindToN)
            {
                var keyTrigger = button.GetComponent<UIButtonKeyTrigger>() ?? button.gameObject.AddComponent<UIButtonKeyTrigger>();
                keyTrigger.key = bindToY ? KeyCode.Y : KeyCode.N;
            }
        }
    }
the StandardUIMenuPanel in the inspector's script already set to the subclass at the first hand, but its still not called, i tried to debug logged it but the its not logged
vinchenamigo
Posts: 10
Joined: Tue May 13, 2025 4:09 am

Re: Using keybind y and n for choosing a yes no response

Post by vinchenamigo »

i am not using override because it get error:

Code: Select all

'StandardUIMenuPanel.SetResponseButton(StandardUIResponseButton, Response, Transform, int)': no suitable method found to override
User avatar
Tony Li
Posts: 23161
Joined: Thu Jul 18, 2013 1:27 pm

Re: Using keybind y and n for choosing a yes no response

Post by Tony Li »

Hi,

Here's a complete version:

Code: Select all

using UnityEngine;
using PixelCrushers.DialogueSystem;

public class YesNoMenuPanel : StandardUIMenuPanel
{
    protected override void SetResponseButton(StandardUIResponseButton button, Response response, Transform target, int buttonNumber)
    {
        base.SetResponseButton(button, response, target, buttonNumber);
        var keybind = Field.LookupValue(response.destinationEntry.fields, "Keybind");
        bool bindToY = string.Equals(keybind, "Y", System.StringComparison.OrdinalIgnoreCase);
        bool bindToN = string.Equals(keybind, "N", System.StringComparison.OrdinalIgnoreCase);
        if (bindToY || bindToN)
        {
            var keyTrigger = button.GetComponent<UIButtonKeyTrigger>() ?? button.gameObject.AddComponent<UIButtonKeyTrigger>();
            keyTrigger.key = bindToY ? KeyCode.Y : KeyCode.N;
        }
    }
}
vinchenamigo
Posts: 10
Joined: Tue May 13, 2025 4:09 am

Re: Using keybind y and n for choosing a yes no response

Post by vinchenamigo »

Thank you soo much Tony, it worked!

One more thing, is there any way that the response menu starts with none selected? like at my case if player mistakenly pressed space(to continue the dialog) it probably chosed yes
Attachments
Screenshot 2025-05-16 at 10.11.01.png
Screenshot 2025-05-16 at 10.11.01.png (700.49 KiB) Viewed 6919 times
vinchenamigo
Posts: 10
Joined: Tue May 13, 2025 4:09 am

Re: Using keybind y and n for choosing a yes no response

Post by vinchenamigo »

i have tried this, but it disables the yes only for few seconds, then it was focused again:

Code: Select all

    protected override void ShowResponsesNow(Subtitle subtitle, Response[] responses, Transform target)
    {
        base.ShowResponsesNow(subtitle, responses, target);
        
        if (eventSystem != null)
        {
            eventSystem.SetSelectedGameObject(null);
        }
    }
Post Reply