- Home /
Using keyboard to controll UI buttons
Hey guys, so as you may already know I want to be able to use wasd or arrow keys to move between buttons but with my code I just can't seem to be able to do more than invoke only one button.
""
As you can see in my code below I've used the images for each button as a marker and am using those to move but then I'm calling the actual button in order to invoke the onClick(); once they are in their correct... position?.. I'm basically saying if image[2] then invoke this buttons onClick(); but it doesn't. Not fully at least, the first if gets called correctly but none of the others do, they all invoke the same button and I can not, for the love of me, understand why.
""
mostly because I've only been coding for a week and a half so I'm quite new to this. Anyway I could really use some help here. What am I missing? The thing is if I comment out the first if then the second if is called and we are back inside that god forsaken loop where all the buttons call the same onClick();
""
I am THIS close to ripping my hair out, please stop me.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class ButtonManager : MonoBehaviour {
//References for each menu
//Pause//
public CanvasGroup canvaspause;
//WinScreen//
//Array
public Image[] image;
public Button[] buttons;
//Button selection
private int selected;
//Color selection
Color normal;
Color highlighted;
//Color Pressed;
//Bool
private bool Paused = false;
// Use this for initialization
void Start () {
normal = new Color32(0xEC, 0xEC, 0xEC, 0xFF);
highlighted = new Color32(0xFF, 0xFF, 0xFF, 0xFF);
//Pressed = new Color32(0x85, 0x85, 0x85, 0xFF);
}
public void MoveToNextButton()
{
image[selected].color = normal;
selected++;
if (selected > 2)
{
selected -= 1;
}
image[selected].color = highlighted;
}
public void MoveToPreviousButton()
{
image[selected].color = normal;
selected--;
if (selected < 0)
{
selected += 1;
}
image[selected].color = highlighted;
}
// Update is called once per frame
void Update () {
if(canvaspause.alpha >= 1f)
{
Paused = true;
}
if(Paused && Input.GetKeyDown(KeyCode.S))
{
MoveToNextButton();
}
else if (Paused && Input.GetKeyDown(KeyCode.W))
{
MoveToPreviousButton();
}
if(Paused && Input.GetKeyUp(KeyCode.E))
{
if (image[0])
{
buttons[0].onClick.Invoke();
}
else if (image[1])
{
buttons[1].onClick.Invoke();
}
else if (image[2])
{
buttons[2].onClick.Invoke();
}
}
}
}
Answer by Arkos25 · May 09, 2018 at 12:22 PM
Okay Guys!
Found the answer to this question so for anyone that is struggling you can find my solution to this problem here. I'll mark the changes I did from the script above. I did two things differently on my code below, one, I created a second variable in order to keep track of my buttons and two I added this line
""
buttons[selected].FindSelectable(Vector2.down); >> (inside MoveToNextButton(); ) ""
and
buttons[selected].FindSelectable(Vector2.Up); >> (inside MoveToPreviousButton();)
""
These were then called by pressing either S or W inside the update.
""
//Button selection
**__private int selected;__**
**__private int color;__**
//Color selection
Color normal;
Color highlighted;
//Color Pressed;
//Bool
private bool Paused = false;
// Use this for initialization
void Start () {
selected = 0;
color = 0;
normal = new Color32(0xEC, 0xEC, 0xEC, 0xFF);
highlighted = new Color32(0xFF, 0xFF, 0xFF, 0xFF);
//Pressed = new Color32(0x85, 0x85, 0x85, 0xFF);
}
public void MoveToNextButton()
{
image[color].color = normal;
**buttons[selected].FindSelectable(Vector2.down);**
**selected++;**
**color++;**
if **(**selected > 2 && color > 2**)**
{
color -= 1;
selected -= 1;
}
image[color].color = highlighted;
}
public void MoveToPreviousButton()
{
image[color].color = normal;
****buttons[selected].FindSelectable(Vector2.up);****
selected--;
color--;
if ****(selected < 0 && color < 0)****
{
color += 1;
selected += 1;
}
image[color].color = highlighted;
}
// Update is called once per frame
void Update () {
if(canvaspause.alpha >= 1f)
{
Paused = true;
}
if(Paused && Input.GetKeyDown(KeyCode.S))
{
MoveToNextButton();
}
else if (Paused && Input.GetKeyDown(KeyCode.W))
{
MoveToPreviousButton();
}
if(Paused && Input.GetKeyUp(KeyCode.E))
{
if **(**selected == 0**)**
{
buttons[0].onClick.Invoke();
}
else if (**selected == 1**)
{
buttons[1].onClick.Invoke();
}
else if (**selected == 2**)
{
buttons[2].onClick.Invoke();
}
}
}
""
Sorry of the shitty explanation so if you didn't understand, write the code yourselves and try it out. Learn by doing I guess :D