1
\$\begingroup\$

I have a scene, where a panel containing 24 buttons. I need to show some piece of text when hover a mouse cursor over the button. For example, for detecting clicks, I'm using onClick event trigger, but I can't find anything related to something like a onHover event. I have Googled and read the docs, but could not find anything. Can anybody help me with that?

Code:

var button = panel.GetChild(0).GetComponent<Button>();
\$\endgroup\$
1
\$\begingroup\$

For example, for detecting clicks, I'm using onClick event trigger, but I can't find anything related to something like a onHover event

Due to that piece of your question, I assume that what you want to find out is how to detect mouse hovering over UI Buttons in Unity (version above 4.6). To do that, you do the following:

  1. Select the desired UI Button
  2. Scroll down the inspector and click the "Add Component" button

  3. In the list of components that will appear, select "Event" and then "Event Trigger"

  4. You will notice that a new "Event Trigger" area appears in the inspector for the button
  5. There, click on the "Add new even type" button

  6. A list of even types will pop-up. Choose "PointerEnter"

  7. Next, on the "PointerEnter" sub-area that will appear within the "Event Trigger" area in the inspector (when your button is selected, of course), click on '+' to add a new item to the list of events

  8. from here on, you just do the same as if you were playing with OnClick events in the inspector, i.e. you just select the object containing the function you want to run when the button is hovered. Then, you select that function.

If you have a lot of buttons and want to automate the process, you can achieve that via script: for each of your buttons you do something like the following (I have not been able to test this myself, unfortunately):

    EventTrigger.Entry eventtype = new EventTrigger.Entry();
    eventtype.eventID = EventTriggerType.PointerEnter;
    eventtype.callback.AddListener((eventData) => { YourDesiredFunction(); });

    GameObject newbutton;
    newbutton.AddComponent<Button>();
    newbutton.AddComponent<EventTrigger>();
    newbutton.GetComponent<EventTrigger>().triggers.Add(eventtype);
\$\endgroup\$
0
\$\begingroup\$

You can also use IPointerEnterHandler and IPointerExitHandler to detect when the mouse cursor is hovering over a button.

Below is some sample code from the Unity documentation for IPointerEnterHandler

//Attach this script to the GameObject you would like to have mouse hovering detected on
//This script outputs a message to the Console when the mouse pointer is currently detected hovering over the GameObject and also when the pointer leaves.

using UnityEngine;
using UnityEngine.EventSystems;

public class Example : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
    //Detect if the Cursor starts to pass over the GameObject
    public void OnPointerEnter(PointerEventData pointerEventData)
    {
        //Output to console the GameObject's name and the following message
        Debug.Log("Cursor Entering " + name + " GameObject");
    }

    //Detect when Cursor leaves the GameObject
    public void OnPointerExit(PointerEventData pointerEventData)
    {
        //Output the following message with the GameObject's name
        Debug.Log("Cursor Exiting " + name + " GameObject");
    }
}
\$\endgroup\$

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.