Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question PointerEnterHandler not firing (trying to get UI element hihglighted)

Discussion in 'UGUI & TextMesh Pro' started by PurpleGoop, Jun 1, 2022.

  1. PurpleGoop

    PurpleGoop

    Joined:
    Apr 30, 2018
    Posts:
    15
    Im creating an interactible computer screen with a custom pointer (by moving an ui element and raycasting from its position). I managed to get that working, and i was able to trigger the pointerDownHandler, pointerUpHandler, pointerClickHandler and the all work perfectly, but for some reason the pointerEnterHandler doesnt seem to do anything, i tried using a script from unity's UI documentation to check if the object is currently highlighted and its not, this is the script i copypasted:

    Code (CSharp):
    1.     //Use the Selectable class as a base class to access the IsHighlighted method
    2.     public class Example : Selectable
    3.     {
    4.         //Use this to check what Events are happening
    5.         BaseEventData m_BaseEvent;
    6.  
    7.         void Update()
    8.         {
    9.             //Check if the GameObject is being highlighted
    10.             if (IsHighlighted())
    11.             {
    12.                 //Output that the GameObject was highlighted, or do something else
    13.                 Debug.Log("Selectable is Highlighted");
    14.             }
    15.         }
    16.     }
    and this is the script im using (made following this YT tutorial, although my implementation is based on direct input from the mouse, instead of how he did it taking look direction as input [link text][1])


    [1]:




    the input functions at the bottom are called by an input manager script, those work fine as i did get the buttons to click, its just the Highlighting that wont work for some reason


    Code (CSharp):
    1.  
    2.     public class ScreenCursor : MonoBehaviour
    3.     {
    4.         [SerializeField] RectTransform cursor;
    5.         [SerializeField] MouseLook mouseLook;
    6.         RectTransform canvas;
    7.         GraphicRaycaster raycaster;
    8.         List<RaycastResult> results;
    9.         List<GameObject> targets;
    10.         Rect rect;
    11.         Vector2 mouseInput;
    12.         bool active = false;
    13.         bool sendMouseDown = false;
    14.         bool sendMouseUp = false;
    15.         bool isMouseDown = false;
    16.  
    17.         void Awake()
    18.         {
    19.             canvas = GetComponent<RectTransform>();
    20.             raycaster = GetComponent<GraphicRaycaster>();
    21.             rect = canvas.rect;
    22.             results = new List<RaycastResult>();
    23.             targets = new List<GameObject>();
    24.         }
    25.  
    26.         void Update()
    27.         {
    28.             GetMouseInput();
    29.             MoveCursor();
    30.             SendInput();
    31.             ResetBools();
    32.         }
    33.  
    34.         void SendInput()
    35.         {
    36.             PointerEventData pointerData = new PointerEventData(EventSystem.current);
    37.             Vector2 centeredCursorPosition = new Vector2(cursor.anchoredPosition.x + rect.xMax,
    38.                                                          cursor.anchoredPosition.y + rect.yMax);
    39.             pointerData.position = centeredCursorPosition;
    40.  
    41.             results.Clear();
    42.          
    43.             raycaster.Raycast(pointerData, results);
    44.  
    45.             foreach(RaycastResult result in results)
    46.             {
    47.                 ExecuteEvents.Execute(result.gameObject, pointerData, ExecuteEvents.pointerEnterHandler);
    48.  
    49.                 if (sendMouseDown)
    50.                 {
    51.                     ExecuteEvents.Execute(result.gameObject, pointerData, ExecuteEvents.pointerDownHandler);
    52.                     print("down");
    53.                 }
    54.                 else if (sendMouseUp)
    55.                 {
    56.                     ExecuteEvents.Execute(result.gameObject, pointerData, ExecuteEvents.pointerUpHandler);
    57.                     ExecuteEvents.Execute(result.gameObject, pointerData, ExecuteEvents.pointerClickHandler);
    58.                     print("up");
    59.                 }
    60.             }
    61.         }
    62.  
    63.         void GetMouseInput()
    64.         {
    65.             mouseInput = new Vector2(mouseLook.mousex, mouseLook.mousey);
    66.         }
    67.  
    68.         void MoveCursor()
    69.         {
    70.             Vector2 newPosition = cursor.anchoredPosition + mouseInput;
    71.  
    72.             if (newPosition.x < rect.xMin)
    73.             {
    74.                 cursor.anchoredPosition = new Vector2(rect.xMin, cursor.anchoredPosition.y);
    75.             }
    76.             else if (newPosition.x > rect.xMax)
    77.             {
    78.                 cursor.anchoredPosition = new Vector2(rect.xMax, cursor.anchoredPosition.y);
    79.             }
    80.             else
    81.             {
    82.               cursor.anchoredPosition += new Vector2(mouseInput.x, 0);
    83.             }
    84.  
    85.             if (newPosition.y < rect.yMin)
    86.             {
    87.                 cursor.anchoredPosition = new Vector2(cursor.anchoredPosition.x, rect.yMin);
    88.             }
    89.             else if (newPosition.y > rect.yMax)
    90.             {
    91.                 cursor.anchoredPosition = new Vector2(cursor.anchoredPosition.x, rect.yMax);
    92.             }
    93.             else
    94.             {
    95.               cursor.anchoredPosition += new Vector2(0, mouseInput.y);
    96.             }
    97.         }
    98.  
    99.         public void OnMouseLeftPressed()
    100.         {
    101.             sendMouseDown = true;
    102.             isMouseDown = true;
    103.         }
    104.  
    105.         public void OnMouseLeftReleased()
    106.         {
    107.             sendMouseUp = true;
    108.             isMouseDown = false;
    109.         }
    110.  
    111.         void ResetBools()
    112.         {
    113.             sendMouseDown = false;
    114.             sendMouseUp = false;
    115.         }
    116.     }
    i spent a couple of days researching before writing this, pls show me how dumb i am and didn't notice the obvious mistake

    PS: tried to post this on unity answers, but something went wrong and its awaiting for moderation, but cant access it again
     
  2. PurpleGoop

    PurpleGoop

    Joined:
    Apr 30, 2018
    Posts:
    15
    Alright i found some sort of solution, after spending hours wandering inside the unity UI documentation page, and found that you can manually add objects to be added to the OnPointEnter event, which is confusing considering the other events work fine without doing this, so i wouldnt consider this solved cause im still trying to understand how and why, this is a good patch fix for now.

    the bit i added is "pointerData.pointerEnter = result.gameObject;" you can find it here https://docs.unity3d.com/Packages/c...ne_EventSystems_PointerEventData_pointerEnter, my question is :
    Why do i have to do this manually while it works without it for the other events?

    EDIT: i forgot to mention that the button would not remove the highlight state by itself, so i also added a cooroutine to wait next frame to execute pointerExitHandler, and it looks like works fine.


    Code (CSharp):
    1.      
    2.         raycaster.Raycast(pointerData, results);
    3.  
    4.         foreach(RaycastResult result in results)
    5.         {
    6.             StartCoroutine(WaitNextFrame(result.gameObject, pointerData));
    7.  
    8.             pointerData.pointerEnter = result.gameObject;
    9.             ExecuteEvents.Execute(result.gameObject, pointerData, ExecuteEvents.pointerEnterHandler);
    10.  
    11.             if (sendMouseDown)
    12.             {
    13.                 ExecuteEvents.Execute(result.gameObject, pointerData, ExecuteEvents.pointerDownHandler);
    14.             }
    15.             else if (sendMouseUp)
    16.             {
    17.                 ExecuteEvents.Execute(result.gameObject, pointerData, ExecuteEvents.pointerUpHandler);
    18.                 ExecuteEvents.Execute(result.gameObject, pointerData, ExecuteEvents.pointerClickHandler);
    19.             }
    20.         }
    21.     }
    22.  
    23.     IEnumerator WaitNextFrame(GameObject button, PointerEventData data)
    24.     {
    25.         yield return null;
    26.         ExecuteEvents.Execute(button, data, ExecuteEvents.pointerExitHandler);
    27.     }
    28.         }
     
    Last edited: Jun 5, 2022