0
\$\begingroup\$

I'm new to Unity and C# and have been struggling to figure out how to solve this. What I'm looking for is a way to make text display whenever a button is selected. (ex. Player selects a difficulty option, and text pops up describing what mechanics the difficulty will affect) I can get this to work with mouse input with no issue, with the following code attached to the button.

void Update()    {
        if (this.IsHighlighted() == true)
        {
            gameObject.transform.GetChild(1).gameObject.SetActive(true);
        }
        else
        {
            gameObject.transform.GetChild(1).gameObject.SetActive(false);
        }
}

The aforementioned description text is set up as a child to the button, and will appear/disappear depending on whether it's highlighted or not. The problem is that isHighlighted only occurs when the mouse is hovered over the button, and does not take effect when selected via directional inputs from a keyboard/controller, which is what I'm looking for. I've done some research and noticed there is an OnSelect method, but I can't figure out how to implement it. Any advice would be greatly appreciated.

\$\endgroup\$
0
\$\begingroup\$

Edit: I added the OnSelect version.

Note that selecting and highlighting / hovering are two different things. When you hover over your button with mouse, OnPointerEnter will be triggered (if using Color Tint transition, Highlighted color will be also activated), and when you exit the Button, OnPointerExit will be triggered. But when you click a Button or navigate to it with keyboard, it is selected and when you click elsewhere or navigate away from your Button, it gets deselected.

So I think you might have to think about your highlighting / hovering if you want to make it work for different navigation styles. When using mouse one doesn't expect he/she has to click a button to see a tooltip.

To detect Button select with ISelectHandler (this should work for at least keyboard and mouse):

public class ButtonTest : MonoBehaviour, ISelectHandler
{
    public void OnSelect(BaseEventData eventData)
    {
        Debug.Log("selected");
    }
}

And seems like you also might need deselect, if that is the case, then you can also implement IDeselectHandler:

public class ButtonTest : MonoBehaviour, ISelectHandler, IDeselectHandler
{
    public void OnSelect(BaseEventData eventData)
    {
        Debug.Log("selected");
    }
    
    public void OnDeselect(BaseEventData eventData)
    {
        Debug.Log("deselected");
    }
}

If you want to detect cursor hovering over your Button use IPointerEnterHandler / IPointerExitHandler:

public class ButtonTest : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
    public void OnPointerEnter(PointerEventData eventData)
    {
        Debug.Log("pointer enter");
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        Debug.Log("pointer exit");
    }
}

You will also have to add a using statement for UnityEngine.EventSystems for these to work.

If you want to detect Button select without using ISelectHandler and IDeselectHandler, you could also do this in your Update:

void Update()
{
    if (EventSystem.current.currentSelectedGameObject == this.gameObject)
    {
        Debug.Log("selected");
    }
}
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ This helped a lot, thank you! For some reason I thought I might have needed to do something really complicated with the EventSystem itself. When I set up the OnSelect and OnDeselect methods, the Debug messages were going through, but the text I wanted to pop up still wasn't showing, despite the same command that worked before being in the new methods. It took me a bit to realize that Update was detecting the buttons as not highlighted, and automatically deactivating the text. This might have been the root of the problem the whole time. Thanks again for the help! \$\endgroup\$ – FEZ_R Nov 18 at 1:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.