Maybe it’s not the best place to ask, but… I am wondering how it would be possible to trigger event with button, if I want to use not “onClick” event but event when mouse pointer enters onto the button?
You mean something like this or?
The proper way is to build your own script to attach to the Button’s GO which implements the IPointerEnterHandler and IPointerExitHandler interfaces. For example:
public class Tooltip : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
And then implement the interface methods using:
public void OnPointerEnter(PointerEventData eventData)
{ }
public void OnPointerExit(PointerEventData eventData)
{ }
EventTrigger should not be used unless you really want to or are not comfortable with coding.
It does have it’s uses. For example if you need to add listeners dynamically at runtime. Or if you need to call methods on another GameObject. It’s also useful if you might frequently change your hookups during iteration.
So the choice between the trigger and interface really comes down to what you want to do with the event.
ok, I made the following class for handing events:
using UnityEngine;
using System.Collections;
public class PointerEventsController : MonoBehaviour,IPointerEnterHandler, IPointerExitHandler {
public void OnPointerEnter(PointerEventData eventData)
{
}
public void OnPointerExit(PointerEventData eventData)
{
}
}
But I receive error just in class declaration:
The type or namespace name `IPointerEnterHandler' could not be found.
Did I missed something here?
P.S. Is this way the best when I want to add helper labels for buttons, which would appear when player puts mouse on the button and disappear when he moves mouse away?
I sorted it out and finally it works (there needed to use UnityEngine.EventSystems
). Here is the full code, which counts and prints out how many times mouse was placed on the button:
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class PointerEventsController : MonoBehaviour,IPointerEnterHandler, IPointerExitHandler {
public int mouseOnCount = 0;
public void OnPointerEnter(PointerEventData eventData)
{
mouseOnCount = mouseOnCount+1;
Debug.Log(mouseOnCount);
}
public void OnPointerExit(PointerEventData eventData)
{
}
}
The code must be saved in file PointerEventsController.cs and attached to button gameObject, like Simon said. There is also no need to call anything from outside, as OnPointerEnter and OnPointerExit methods are triggered automatically when events are happening.
I started to face difficulties with this way, as each time I am using different function calls from different buttons I need to create new classes and adopt to each button individually. Is there a way to do it in more unified way, that I wouldn’t need to copy it to different classes and I could use just one PointerEventsController class which would handle all events for all different buttons?
You could have a single script with all the different methods for handling click events and then use EventTrigger to call the relevant metod on click. But if i is good practice I do not know
Well, the point here is that I need not onClick() events, but “onPointerEnter” events…
I’d disagree with that statement a bit @Kiwasi
If you look at the source for the EventTrigger, all it does is instantiate all interfaces
If you need an event dynamically, then I wou still use a custom script with only those interfaces I wanted.
In which case you want the IPointerClickHandler/OnPointerClick Interface/handler
There are also PointerDown and PointerUp events as well.
You can see them all listed here:
https://bitbucket.org/Unity-Technologies/ui/src/5fc21bb4ecf4b40ff6630057edaa070252909b2e/UnityEngine.UI/EventSystem/EventInterfaces.cs?at=4.6
So I am back to this question and still got confused. Lets say I have 10 buttons and want to add mouse right click events to all of them. I am interested that when player right-click one of buttons, that he would get different functions running. With delegates for Left click I simply use:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
.....
for(int i=1;i<10;i++){
Button but = button_list[i];
if(i==1){
but.onClick.AddListener(delegate {
Function1();
});
}
else if(i==2){
but.onClick.AddListener(delegate {
Function2();
});
}
.....
}
Function1(), Function2() and other functions are completely separate functions, sometimes they are called from different classes. So in that case I am curious if there is a similar way to deal with all these separate functions like it is done through delegates? Or maybe it is possible somehow to use delegates here as well?
You missed this line:
using UnityEngine.EventSystems;
I know late but you can simply implement your own UnityEvent
s like
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class PointerEventsController : MonoBehaviour,IPointerEnterHandler, IPointerExitHandler
{
// Set those in the inspector or via AddListener exactly the same as onClick of a button
public UnityEvent onPointerEnter;
public UnityEvent onPointerExit;
public void OnPointerEnter(PointerEventData eventData)
{
// evtl put some general button fucntionality here
onPointerEnter.Invoke();
}
public void OnPointerExit(PointerEventData eventData)
{
// evtl put some general button fucntionalit here
onPointerExit.Invoke();
}
}
I wish the docs would have useful boilerplate. Here is my all purpose button class:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using UnityEngine.EventSystems;
public class ButtonUI : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerClickHandler, IBeginDragHandler, IDragHandler, IEndDragHandler // required interface when using the OnPointerEnter method.
{
public Button theButton;
private float timeCount;
public void OnPointerEnter(PointerEventData eventData)
{
Debug.Log("The cursor entered the selectable UI element. " + eventData);
}
public void OnPointerClick(PointerEventData eventData)
{
Debug.Log("The cursor clicked the selectable UI element. " + eventData);
}
public void OnPointerExit(PointerEventData eventData)
{
Debug.Log("The cursor exited the selectable UI element. " + eventData);
}
public void OnBeginDrag(PointerEventData data)
{
Debug.Log("OnBeginDrag: " + data.position);
data.pointerDrag = null;
}
public void OnDrag(PointerEventData data)
{
if (data.dragging)
{
timeCount += Time.deltaTime;
if (timeCount > 1.0f)
{
Debug.Log("Dragging:" + data.position);
timeCount = 0.0f;
}
}
}
public void OnEndDrag(PointerEventData data)
{
Debug.Log("OnEndDrag: " + data.position);
}
}
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class PointerEventsController : MonoBehaviour,IPointerEnterHandler, IPointerExitHandler
{
// Set those in the inspector or via AddListener exactly the same as onClick of a button
public UnityEvent onPointerEnter;
public UnityEvent onPointerExit;
public void OnPointerEnter(PointerEventData eventData)
{
// evtl put some general button fucntionality here
onPointerEnter.Invoke();
}
public void OnPointerExit(PointerEventData eventData)
{
// evtl put some general button fucntionalit here
onPointerExit.Invoke();
}
}
[/QUOTE]
Hi, I just made a similar script where I set the eventData and invoke a specific event. Now, I just want to receive these events in an other script. Do I have to setup an EventManager for that?
how can i add the “OnPointerEnter” to an instantiated button?
Thank you so much! This helped give my game the functionality that I was looking for!