- Home /
Buttons with Event System with different Submit keys
Hello All
I have 3 buttons on my screen UI, each button has a different event systems, and each event system has a different Submit button key press
So if i press Key1, it presses the Button on the left Key2, it presses the Button in the middle Key3, it presses the button on the right.
i'm not using a mouse, or touch screen. The only thing that works is the first random button that gets highlighted, the other buttons wont respond to its Submit button key press.
Is it possible to highlight all 3 buttons at the same time. so if i press either key1,2 or 3, it knows what button to click.
Any help would be extremely grateful
Answer by MortonPrice · Mar 12, 2019 at 08:16 AM
Found the answer in other Question
For any one wondering, attaching the below script worked a treat
using UnityEngine;
using UnityEngine.UI;
[RequireComponent(typeof(Button))]
public class KeyButton : MonoBehaviour {
public KeyCode key;
public Button button {get; private set;}
Graphic targetGraphic;
Color normalColor;
void Awake() {
button = GetComponent<Button>();
button.interactable = false;
targetGraphic = GetComponent<Graphic>();
ColorBlock cb = button.colors;
cb.disabledColor = cb.normalColor;
button.colors = cb;
}
void Start() {
button.targetGraphic = null;
Up();
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(key)) {
Down();
} else if (Input.GetKeyUp(key)) {
Up();
}
}
void Up() {
StartColorTween(button.colors.normalColor, false);
}
void Down() {
StartColorTween(button.colors.pressedColor, false);
button.onClick.Invoke();
}
void StartColorTween(Color targetColor, bool instant) {
if (targetGraphic == null)
return;
targetGraphic.CrossFadeColor(targetColor, instant ? 0f : button.colors.fadeDuration, true, true);
}
void OnApplicationFocus(bool focus) {
Up();
}
public void LogOnClick() {
Debug.Log ("LogOnClick() - " + GetComponentInChildren<Text>().text);
}
}
Thank you @Huacanacha for answering this 4 years ago