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)
EventTrigger trig = elem.gameObject.GetComponent<EventTrigger>();
AddEventTrigger(trig,OnPointerEnter,EventTriggerType.PointerEnter);
AddEventTrigger(trig,OnPointerExit,EventTriggerType.PointerExit);
then
private void AddEventTrigger(EventTrigger evTrig, UnityAction<BaseEventData> action, EventTriggerType triggerType){
EventTrigger.TriggerEvent trigger = new EventTrigger.TriggerEvent();
trigger.AddListener((eventData) => action(eventData));
EventTrigger.Entry entry = new EventTrigger.Entry() { callback = trigger, eventID = triggerType };
evTrig.delegates.Add(entry);
}
and the callback is
private void OnPointerEnter(BaseEventData dataObject){
if(dataObject.selectedObject!=null){
if(dataObject.selectedObject.GetComponent<HeroPortraitGUI>()!=null){
ttp.SetTooltip(dataObject.selectedObject.GetComponent<HeroPortraitGUI>().heroName);
}
}
}
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