- Home /
Using Event Trigger from Script to access PointerEnter and PointerExit
I am trying to use Event Trigger to access PointerEnter and PointerExit from script. I am trying to create Tool Tips, but I only want the Tool Tips to display if the Button the mouse hovers over is interactable = false.
I can get this to work by dragging and dropping the correct GameObject into the Inspector, but this method does not know whether the button the mouse is over is interactable or not; therefore it always displays the Tool Tip.
Hopefully this makes sense if not I can try and explain better. Thanks!
Answer by Zankomag · May 25, 2019 at 01:24 AM
You don't need EventTrigger for accessing PointerEnter and PointerExit through script (you can do it via EventTrigger though). All you need is EventSystem on scene and implementing required interfaces:
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class ToolTips : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
Button button;
private void Awake() {
button = GetComponent<Button>();
}
public void OnPointerEnter(PointerEventData eventData) {
if (!button.interactable)
ShowToolTips();
}
public void OnPointerExit(PointerEventData eventData) {
if (!button.interactable)
HideToolTips();
}
}
Also if you need only Interactable feature and don't need Button.onClick, you can just use Selectable component instead of Button
This method does work; however, I decided to simply use the EventTrigger system in the Inspector on each UI Element. I was able to get the ToolTips to not display by disabling Raycast Target through script:
button.GetComponet-Image-().raycastTarget = false;
Answer by OfficerNumberOne · May 25, 2019 at 03:28 AM
@Zankomag, thanks for the help, not sure if its working yet. Using TextMeshPro, can use use TextReference.text = "words" to change the text in game?
Right now I have this (Ive implemented everything else you said):
public TextMeshProUGUI toolTipT;
public void OnPointerEnter(PointerEventData eventData) {
if (Button.interactable) { toolTipT.text = "WORDS"; }
}
Does the toolTipT.text need to be in the Update method?
Sure, you should change Text$$anonymous$$eshPro text through its reference.
No, it doesn't in this case.
Thanks, I had to turn Raycast Target off for certain UI elements I didnt want to mouse over. However, is there a way to differentiate between which button the mouse is over. Cause right now all the inactive buttons display the same message. I could solve this with booleans, but it would be alot of extra code and was wondering if there was an easier way.
Your answer
Follow this Question
Related Questions
Can I set a specific Pointer for the EventTrigger on Buttons for example? 0 Answers
Creating a Tooltip when hovering over a UI Button? 3 Answers
Pointer Down event on a button still registering a hold even when I drag pointer away?? 1 Answer
EventTrigger runtime added callbacks to OnPointerClick/OnPointerUp/OnPointerDown don't work. 1 Answer
Touchscreen Joystick Character Control using PointerEventData 1 Answer