Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to use OnPointerEnter event?

Discussion in 'UGUI & TextMesh Pro' started by chanfort, Jan 30, 2015.

  1. chanfort

    chanfort

    Joined:
    Dec 15, 2013
    Posts:
    641
    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?
     
    DragonCoder likes this.
  2. Ramsdal

    Ramsdal

    Joined:
    Oct 18, 2013
    Posts:
    251
  3. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,688
    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:
    Code (CSharp):
    1. public class Tooltip : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
    And then implement the interface methods using:
    Code (CSharp):
    1.     public void OnPointerEnter(PointerEventData eventData)
    2.     {  }
    3.  
    4.     public void OnPointerExit(PointerEventData eventData)
    5.     {    }
    EventTrigger should not be used unless you really want to or are not comfortable with coding.
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    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.
     
  5. chanfort

    chanfort

    Joined:
    Dec 15, 2013
    Posts:
    641
    ok, I made the following class for handing events:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PointerEventsController : MonoBehaviour,IPointerEnterHandler, IPointerExitHandler {
    5.  
    6. public void OnPointerEnter(PointerEventData eventData)
    7.     {
    8.  
    9.     }
    10.     public void OnPointerExit(PointerEventData eventData)
    11.     {
    12.  
    13.     }
    14.  
    15. }
    16.  
    But I receive error just in class declaration:
    Code (CSharp):
    1. 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?
     
  6. chanfort

    chanfort

    Joined:
    Dec 15, 2013
    Posts:
    641
    I sorted it out and finally it works (there needed to use
    Code (CSharp):
    1. UnityEngine.EventSystems
    ). Here is the full code, which counts and prints out how many times mouse was placed on the button:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.EventSystems;
    4.  
    5. public class PointerEventsController : MonoBehaviour,IPointerEnterHandler, IPointerExitHandler {
    6.  
    7.     public int mouseOnCount = 0;
    8.  
    9.     public void OnPointerEnter(PointerEventData eventData)
    10.     {
    11.         mouseOnCount = mouseOnCount+1;
    12.         Debug.Log(mouseOnCount);
    13.     }
    14.     public void OnPointerExit(PointerEventData eventData)
    15.     {
    16.    
    17.     }
    18.    
    19. }
    20.  
    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.
     
    niqle_dev and LucyRocks like this.
  7. chanfort

    chanfort

    Joined:
    Dec 15, 2013
    Posts:
    641
    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?
     
  8. Ramsdal

    Ramsdal

    Joined:
    Oct 18, 2013
    Posts:
    251
    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 ;)
     
  9. chanfort

    chanfort

    Joined:
    Dec 15, 2013
    Posts:
    641
    Well, the point here is that I need not onClick() events, but "onPointerEnter" events...
     
    DragonCoder likes this.
  10. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,688
    I'd disagree with that statement a bit @BoredMormon
    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.
     
    Kiwasi likes this.
  11. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,688
  12. chanfort

    chanfort

    Joined:
    Dec 15, 2013
    Posts:
    641
    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:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine.UI;
    5.  
    6.  
    7. .....
    8. for(int i=1;i<10;i++){
    9.     Button but = button_list[i];
    10.     if(i==1){
    11.         but.onClick.AddListener(delegate {
    12.             Function1();
    13.         });
    14.     }
    15.     else if(i==2){
    16.         but.onClick.AddListener(delegate {
    17.             Function2();
    18.         });
    19.     }
    20.     .....
    21. }
    22.  
    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?
     
  13. s3519064

    s3519064

    Joined:
    Aug 25, 2017
    Posts:
    3
    You missed this line:
    using UnityEngine.EventSystems;
     
  14. derHugo

    derHugo

    Joined:
    Nov 1, 2017
    Posts:
    11
    I know late but you can simply implement your own `UnityEvent`s like

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.EventSystems;
    4.  
    5. public class PointerEventsController : MonoBehaviour,IPointerEnterHandler, IPointerExitHandler
    6. {
    7.     // Set those in the inspector or via AddListener exactly the same as onClick of a button
    8.     public UnityEvent onPointerEnter;
    9.     public UnityEvent onPointerExit;
    10.  
    11.     public void OnPointerEnter(PointerEventData eventData)
    12.     {
    13.         // evtl put some general button fucntionality here
    14.          
    15.         onPointerEnter.Invoke();
    16.     }
    17.  
    18.     public void OnPointerExit(PointerEventData eventData)
    19.     {
    20.         // evtl put some general button fucntionalit here
    21.      
    22.         onPointerExit.Invoke();
    23.     }  
    24. }
     
    DragonCoder likes this.
  15. tomachinz

    tomachinz

    Joined:
    Mar 2, 2019
    Posts:
    15
    I wish the docs would have useful boilerplate. Here is my all purpose button class:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.Events;
    6. using UnityEngine.EventSystems;
    7.  
    8. public class ButtonUI : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerClickHandler, IBeginDragHandler, IDragHandler, IEndDragHandler // required interface when using the OnPointerEnter method.
    9.  
    10. {
    11.     public Button theButton;
    12.     private float timeCount;
    13.  
    14.     public void OnPointerEnter(PointerEventData eventData)
    15.     {
    16.         Debug.Log("The cursor entered the selectable UI element. " + eventData);
    17.     }
    18.     public void OnPointerClick(PointerEventData eventData)
    19.     {
    20.         Debug.Log("The cursor clicked the selectable UI element. " + eventData);
    21.     }
    22.     public void OnPointerExit(PointerEventData eventData)
    23.     {
    24.         Debug.Log("The cursor exited the selectable UI element. " + eventData);
    25.     }
    26.     public void OnBeginDrag(PointerEventData data)
    27.     {
    28.         Debug.Log("OnBeginDrag: " + data.position);
    29.         data.pointerDrag = null;
    30.     }
    31.     public void OnDrag(PointerEventData data)
    32.     {
    33.         if (data.dragging)
    34.         {
    35.             timeCount += Time.deltaTime;
    36.             if (timeCount > 1.0f)
    37.             {
    38.                 Debug.Log("Dragging:" + data.position);
    39.                 timeCount = 0.0f;
    40.             }
    41.         }
    42.     }
    43.     public void OnEndDrag(PointerEventData data)
    44.     {
    45.         Debug.Log("OnEndDrag: " + data.position);
    46.     }
    47. }
     
    willianjohns and NeatWolf like this.
  16. beyondtheline

    beyondtheline

    Joined:
    Feb 1, 2017
    Posts:
    2
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.EventSystems;
    4.  
    5. public class PointerEventsController : MonoBehaviour,IPointerEnterHandler, IPointerExitHandler
    6. {
    7.     // Set those in the inspector or via AddListener exactly the same as onClick of a button
    8.     public UnityEvent onPointerEnter;
    9.     public UnityEvent onPointerExit;
    10.  
    11.     public void OnPointerEnter(PointerEventData eventData)
    12.     {
    13.         // evtl put some general button fucntionality here
    14.        
    15.         onPointerEnter.Invoke();
    16.     }
    17.  
    18.     public void OnPointerExit(PointerEventData eventData)
    19.     {
    20.         // evtl put some general button fucntionalit here
    21.    
    22.         onPointerExit.Invoke();
    23.     }  
    24. }
    [/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?
     
  17. houssemyui

    houssemyui

    Joined:
    Sep 4, 2020
    Posts:
    3
    how can i add the "OnPointerEnter" to an instantiated button?
     
  18. niqle_dev

    niqle_dev

    Joined:
    Nov 30, 2021
    Posts:
    2
    Thank you so much! This helped give my game the functionality that I was looking for!