- Home /
Better Code/Event System for Menu
I am working on a menu that is meant to have buttons that show quotation marks when you select them (like Silent Hill).
Scene View and Heirarchy
Event Trigger (used for both buttons)
I created event triggers inside the "BTN_" objects that change the alpha of the "Quote1" child object from 0 to 1 when the mouse is hovered over and back to 0 when the mouse isn't over the button. Is there a way to make a better event system that also reacts to "wasd" and arrow keys? I'm preferably aiming to organize the menu and triggers in C#, but using components in the inspector is fine.
Packages Used - TextMesh Pro
EDIT: The parent system of my button is:
BTN_ (the button) NewGameBtn (the text) Quote1 (the quotation marks)
Answer by WarmedxMints · Mar 01, 2019 at 02:58 AM
You could make a class that inherits from EventTrigger to deal with it. Like so..
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class ButtonController : EventTrigger
{
private string _buttonText;
private Text _buttonTextBox;
private void Start()
{
_buttonTextBox = GetComponentInChildren<Text>();
_buttonText = _buttonTextBox.text;
}
public override void OnPointerEnter(PointerEventData eventData)
{
UpdateButtonText(true);
}
public override void OnPointerExit(PointerEventData eventData)
{
UpdateButtonText(false);
}
public override void OnSelect(BaseEventData eventData)
{
UpdateButtonText(true);
}
public override void OnDeselect(BaseEventData eventData)
{
UpdateButtonText(false);
}
private void UpdateButtonText(bool selected)
{
_buttonTextBox.text = selected ? "\"" + _buttonText + "\"" : _buttonText;
}
}
If you remove the event trigger component and your quote child object, you can just put the above script on the button.
I removed the quotes and the trigger component. I put the code on the button and if I move my mouse over the button or click it, nothing happens. I forgot to mention that the "NewGameBtn" object that's inside the main "BTN_" object is the text itself.
Is it better to use this code for the normal Unity UI Text?
I am also getting two NullReferenceException errors that point to lines 14 and 42
14:"_buttonText = _buttonTextBox.text
"
42: this function - "_buttonTextBox.text = selected ...
" inside the UpdateButtonText bool.
That code is for the Unity UI text. Create a new standard button and try it. All you need to do with that code is change the text component of a normal button to your font and shade of red. If you don't have a text component on the button, the code will not work as it is looking for one.
It works for the mouse, but what about using the arrow keys and wasd?
Your answer
![](https://koobas.hobune.stream/wayback/20220612192712im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
IDropHandler OnDrop() only being called on parent of dragged item. 0 Answers
How to seperate/decouple UI and player code 0 Answers
Adding a function to a UI 4.6 Button called on another game object in c# 0 Answers
Can't change UI Button Listeners 1 Answer
How to vertical drag button inside horizontal scrollrect 2 Answers