I’m putting together input systems for a primarily gamepad-driven game. When I press a button to get a menu up, the input is still registered by the next menu panel and immediately cancels the panel. Think of the way you use an “interact” button to start dialog with an NPC and press “interact” again to finish the conversation.
My first thought was that I would treat it like UI when I’m making commercial desktop apps, in which you would set a UI event to handled. But that seems to only work with Event.PopEvent. And that doesn’t seem to work with the input axes I set up in Edit → Project Settings → Input.
What’s the best way to check both keyboard and gamepad input for an event and to keep that event from firing again on the newly-enabled menu?
Sure thing. It’s a bit much, since I’m trying to write more reusable systems, so bear with me.
Code Snippets
// called by pause menu button OnClick handler, triggered by "Submit" input on a button
public void Save()
{
Debug.Log("Save Complete");
TextManager.Instance.DisplayModal("Save Complete");
}
//TextManager.cs
public void DisplayModal(string text, int xPercent = 0, int yPercent = 0)
{
modalTextBoxRef = Instantiate(panelModalPrefab, this.transform, false);
modalTextBoxRef.GetComponentInChildren<Text>().text = text;
this.StartListening();
}
the return key triggers the OnClick event of a button
The button calls my GM.Save() method
GM.Save() calls my TextManager.DisplayModal() method
the TextManager.DisplayModal() method instantiates a “Save Complete” prefab that begins listening for input so that the player can dismiss it when done reading.
The Modal dialog panel is immediately dismissed, even though I only pressed return once. I double-checked in the VS debugger, and it is definitely being triggered by Input.GetButtonDown(“Submit”).
The most confusing part of all this is that it only happens once per run. Every subsequent Save works as normal.