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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

How to know when a mouse wheel was used by scroll view?

Discussion in 'UI Toolkit' started by skowroshima, Sep 4, 2021.

  1. skowroshima

    skowroshima

    Joined:
    Oct 14, 2018
    Posts:
    75
    By default the mouse wheel controls zoom in my game. I want to be able to know when the mouse wheel was used by the UI, so I can prevent the use of the mouse wheel within the game. Actually checking for movement via the scroller will be too late, as it will be only be updated in the following frame.

    Is there any way of detecting the use of the mouse wheel?
     
  2. manuelgoellnitz

    manuelgoellnitz

    Joined:
    Feb 15, 2017
    Posts:
    366
    You could monitor if the mouse is over an ui element or not, an if so, block the zoom-controls. That is the way I do it.
     
  3. PaoloAm

    PaoloAm

    Joined:
    Jul 15, 2019
    Posts:
    28
    That would be it but the thing is, a month ago (ish) they didn't yet implemented a way to check if the mouse was over an UI element in the easy way as the UGUI, do you know if it's done now?
     
  4. manuelgoellnitz

    manuelgoellnitz

    Joined:
    Feb 15, 2017
    Posts:
    366
    There are "OnMouseEnter" and "OnMouseLeave" Events where you can listen to.

    Another way is via the EventSystem: When you use the normal Eventsystem and check the pointerEventData. When the raycastResult is valid and the distance is 0 then you are probably with your mouse over an UI element.
     
  5. PaoloAm

    PaoloAm

    Joined:
    Jul 15, 2019
    Posts:
    28
    The problem is that if you need to implement the "OnMouseEnter" and "OnMouseLeave" on every single UI element you have it would be a long task and very difficult to maintain. We also use the new EventSystem so it would need to be a tricky implementation. Or have you found a solution?

    The UGUI has a single API for that, very usefull, I hope they implement it soon in the system.
     
  6. rimisaby

    rimisaby

    Joined:
    Nov 21, 2016
    Posts:
    14
    You can use EventSystem.current.IsPointerOverGameObject(). If it returns true, the pointer is over a UIElement.
     
  7. manuelgoellnitz

    manuelgoellnitz

    Joined:
    Feb 15, 2017
    Posts:
    366
    You can use "OnMouseEnter" and "OnMouseLeave" on the root element. This way it then it is triggered for all children as well, aka when the mouse enters any ui element from outside the ui (but you cant have invisible ui-elements that fill the whole screen, otherwise the events are never called).

    Last time i checked "EventSystem.current.IsPointerOverGameObject()" did not work for me.
     
  8. rimisaby

    rimisaby

    Joined:
    Nov 21, 2016
    Posts:
    14
    IsPointOverGameObject works for me with 2021.2.0b10 also for UIToolkit elements.

    I have a MouseEventHandler that handles events for all 3D navigation (like camera movement e.g), where I filter mouse events that were ment for the 2D ui, like

    Code (CSharp):
    1. if (Input.GetMouseButtonDown (0) && !EventSystem.current.IsPointerOverGameObject ())
    2.     HandleMouseDown();
     
    PaoloAm likes this.
  9. PaoloAm

    PaoloAm

    Joined:
    Jul 15, 2019
    Posts:
    28
    It works thanks!