Search Unity

OnPointerOver - A (sad) tooltip story

Discussion in 'UGUI & TextMesh Pro' started by drHogan, Nov 27, 2014.

  1. drHogan

    drHogan

    Joined:
    Sep 26, 2012
    Posts:
    201
    Hi guys, I posted some days ago about this same problem, then i thought it figured out that it was a known bug, then now someone suggested me i am just misusing the dataObject, so here's my problem again.

    A simple tooltip to appear on mouse over a button (before the user clicks it).
    The code is quite simple, i have a tavern "canvas" from which some pseudo-randomly determined heroes can be available for hiring. Each hero is an image button, when you hover on top of it with the mouse you just see it's name.

    This is how i do it (from the tavern, i do that for each button)

    Code (CSharp):
    1. EventTrigger trig = elem.gameObject.GetComponent<EventTrigger>();
    2. AddEventTrigger(trig,OnPointerEnter,EventTriggerType.PointerEnter);
    3. AddEventTrigger(trig,OnPointerExit,EventTriggerType.PointerExit);
    then

    Code (CSharp):
    1. private void AddEventTrigger(EventTrigger evTrig, UnityAction<BaseEventData> action, EventTriggerType triggerType){
    2.         EventTrigger.TriggerEvent trigger = new EventTrigger.TriggerEvent();
    3.         trigger.AddListener((eventData) => action(eventData));
    4.         EventTrigger.Entry entry = new EventTrigger.Entry() { callback = trigger, eventID = triggerType };
    5.         evTrig.delegates.Add(entry);
    6.     }
    and the callback is
    Code (CSharp):
    1. private void OnPointerEnter(BaseEventData dataObject){
    2.     if(dataObject.selectedObject!=null){
    3.         if(dataObject.selectedObject.GetComponent<HeroPortraitGUI>()!=null){
    4.             ttp.SetTooltip(dataObject.selectedObject.GetComponent<HeroPortraitGUI>().heroName);
    5.          }
    6.     }
    7. }
    It only works if first I click anybutton, and after that when i am inside any other button i get the name from the one clicked. Other than that it works, the tooltip appears and follows only when i am inside one of the portraits.
    I guess i am misusing something but i don't get what. What would be an alternative way to get the gameobject of the portrait i am hovering of top of?

    Best,
    H
     
  2. Immanuel-Scholz

    Immanuel-Scholz

    Joined:
    Jun 8, 2013
    Posts:
    221
    Hm.. I haven't tested the following, but what about this:

    Code (csharp):
    1.  
    2. EventTrigger trig = elem.gameObject.GetComponent<EventTrigger>();
    3. AddEventTrigger(trig,d => OnPointerEnter(d, trig.gameObject),EventTriggerType.PointerEnter);
    4. AddEventTrigger(trig,OnPointerExit,EventTriggerType.PointerExit);
    5.  
    6. ...
    7.  
    8. private void OnPointerEnter(BaseEventData dataObject, GameObject hovered){
    9.     if(hovered != null && hovered.GetComponent<HeroPortraitGUI>() != null)
    10.     ...
    11. }
    12.  
     
    coombsanity likes this.
  3. drHogan

    drHogan

    Joined:
    Sep 26, 2012
    Posts:
    201
    You are awesome, I don't mean a bit awesome like so and so, i mean seriously awesome!

    Thanks man!! It works!

    (Side question, do you know any good tutorial about event managing in unity3d?)

    All the best and really thanks for the hint, saved me several more additional hours of confusion!