- Home /
Ui Button: call function "on pointer enter", all at runtime
So I have the code to create buttons at runtime. Now I'm trying to call a specific function when the mouse pointer enters the button.
For clicks on the button (UnityEngine.UI.Button) it is very simple: button.onClick.AddListener(delegate { // Any code here });
But how does one do this in a simple way for "on pointer enter" events?
Basically I am looking for something like: button.onPointerEnter.AddListener(delegate { // Create tooltip });
Answer by phxvyper · Jun 17, 2018 at 10:48 AM
Over a quick google search, the answer was my second result.
This answer discusses adding an event listener for pointer events.
Thanks but I don't see it. The accepted answer there is not "at runtime". The second answer seems overly complex by creating a whole new raycasting system which I expect my canvas "Graphic Raycaster" already does. And the 3rd answer doesn't really do anything other than test if the pointer is over ANY gameobject.
I was really hoping there was a simple solution to this just like the "onClick", given how it seems so common that you'd want to test point enter/exit events on buttons.
The accepted answer is indeed at runtime. It uses an Event Trigger which is exactly what you need to get PointerEnter events. You can still add to that event at runtime using Unity's Event System.
Your answer
Follow this Question
Related Questions
Issue adding UI Button Unity 5 1 Answer
How do change UI.button onclick states(off, editor and runtime, runtime only) via script? 0 Answers
Problem when trying to select buttons with the keyboard 0 Answers
Easily recolorable Button background textures 1 Answer
How to make World Space Canvas be on top of Overlay Canvas? 0 Answers