IPointerEnterHandler and IPointerExitHandler for panel images tooltip management

HI Everybody,

I am dealing with the GUI of our game since a couple of months. We have a very large and working GUI now, but i would like to optimize it too.

The thing I am dealing with is the OnPointerEnter/Exit for the tooltips. So far i used the “ignorant” approach, that is i attached an EventTrigger to the things i wanted to have the tooltip, i was then retrieving it from the main panel script, attacking the callback from there and showing the tooltip.

SimonDarksideJ mentioned in a few different occasions that the EvenTrigger, by implementing all the events it’s an overkill in such cases, i decide therefore to go for the IPointerEnterHandler /Exit implementation, but I couldn’t find any example to get a first rough direction.

I don’t know if this approach is correct, but i wrote a little script that i then attach to the images that have to throw the tooltip. The thing is, as the tooltips will be all dynamically generated, i would like to have the control over it from the main panel script (so with a simple switch over the component hovered i can launch the tooltip script with the correct settings).

If this makes sense ( i think it does but couldn’t find any example), how do I retrieve it and use it from the main script? I mean ok, i get the TooltipSourceHandler component from the item, but how am i supposed to use it in practice? I feel a bit lost :smile: any suggestion is welcome! Cheers,

H

public class TooltipSourceHandler : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler {
   
    #region IPointerEnterHandler implementation
    public void OnPointerEnter (PointerEventData eventData)
    {
        Debug.Log("ENTER");
        throw new System.NotImplementedException ();
    }
    #endregion

    #region IPointerExitHandler implementation
    public void OnPointerExit (PointerEventData eventData)
    {
        Debug.Log("EXIT");
        throw new System.NotImplementedException ();
    }
    #endregion

    // Use this for initialization
    void Start () {
   
    }
   
    // Update is called once per frame
    void Update () {
   
    }
}

Yup that implementation is fine (without the thrownew System.NotImplementedException(); as they would cause it to crash :smile:)

There is a tooltip example in the UI Essentials which takes another approach but needs a lot of extending.

Probably breaking all the rules (Sh, don’t tell my publisher) but here is the tooltip example I show in Ch 6 of my UI book.

using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class Tooltip : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
    private bool m_tooltipDisplayed = false;
    public RectTransform TooltipItem;
    private Vector3 m_tooltipOffset;

    public void OnPointerEnter(PointerEventData eventData)
    {
        m_tooltipDisplayed = true;
        TooltipItem.transform.position = transform.position + m_tooltipOffset;
        TooltipItem.gameObject.SetActive(m_tooltipDisplayed);
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        m_tooltipDisplayed = false;
        TooltipItem.gameObject.SetActive(m_tooltipDisplayed);
    }

    void Start()
    {
        //Offset the tooltip above the target GameObject
        m_tooltipOffset = new Vector3(0, TooltipItem.sizeDelta.y , 0);
        //Deactivate the tooltip so that it is only shown when you want it to
        TooltipItem.gameObject.SetActive(m_tooltipDisplayed);
    }
}

You can then attach this to any object in the game, link your tooltip object to the “ToolTipItem” property of the script and hay presto, one tooltip.

There are lots of ways you can expand on this by having dynamic text coming from the object. Enhanced positioning for the tooltip (keep it in screen if you are near the edge) and so on.

Mmm yes, it makes sense indeed.

(yep, the exception was a leftover of the refactoring of mono)

For some weird reason i was thinking on retrieving the script from the object, keep the script really minimal, and try some more black magic from the main script (something in the style of adding events to an event trigger), but this solution from inside the implementing script might work cleanly as well!

Thanks man!