Page 1 of 2
Using keybind y and n for choosing a yes no response
Posted: Tue May 13, 2025 4:16 am
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
Re: Using keybind y and n for choosing a yes no response
Posted: Tue May 13, 2025 8:38 am
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.)
Re: Using keybind y and n for choosing a yes no response
Posted: Wed May 14, 2025 5:34 am
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
Re: Using keybind y and n for choosing a yes no response
Posted: Wed May 14, 2025 5:36 am
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!
Re: Using keybind y and n for choosing a yes no response
Posted: Wed May 14, 2025 7:55 am
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
Re: Using keybind y and n for choosing a yes no response
Posted: Thu May 15, 2025 5:53 am
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
Re: Using keybind y and n for choosing a yes no response
Posted: Thu May 15, 2025 6:09 am
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
Re: Using keybind y and n for choosing a yes no response
Posted: Thu May 15, 2025 8:05 am
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;
}
}
}
Re: Using keybind y and n for choosing a yes no response
Posted: Thu May 15, 2025 11:14 pm
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
Re: Using keybind y and n for choosing a yes no response
Posted: Fri May 16, 2025 4:22 am
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);
}
}