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. Dismiss Notice

Question How to replicate functionality from the old EventSystem.IsPointerOverGameObject?

Discussion in 'UI Toolkit' started by Shablo5, Dec 29, 2021.

  1. Shablo5

    Shablo5

    Joined:
    Mar 2, 2015
    Posts:
    40
    I'm trying to replicate this line with the new UI toolkit.

    Code (CSharp):
    1. if (EventSystem.current.IsPointerOverGameObject())
    2.         {
    3.             return;
    4.         }
    Any advice is appreciated.
     
  2. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    700
    It's the same function, you need to set visibility to hidden to have this return false.
     
  3. Shablo5

    Shablo5

    Joined:
    Mar 2, 2015
    Posts:
    40
    I can't set the UI visibility to hidden as I have them persist all the time.

    In UnityUI I could use this and if my persistent UI was in the top left corner, I basically just couldn't click the top left corner and I was happy. But with UI Toolkit it feels like even my limited UI in the top left corner triggers this block of code even if I click in an area that no UI is present.

    Any thoughts?
     
  4. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    700
    In my UI I have 3 panels:

    upload_2021-12-30_0-59-43.png

    The 3D clickable area is covered by the middle panel:

    upload_2021-12-30_1-0-25.png

    It also contains all kind of dialog boxes and popups, and whenever I open one then I toggle its visibility, if I changed the display then the bottom panel would move under the top panel which is not what I want.
     
  5. magnetic_scho

    magnetic_scho

    Joined:
    Feb 2, 2020
    Posts:
    69
    We started with the same approach as you @dlorre, to put all permanent UI into one panel and fill the whole screen with it.

    We had issues with clicks not being received on game objects because of it and therefore, separated all permanent UI elements into their own uxml files. We positioned them with margin set to auto (e.g. your bottom-panel should have margin-top: auto).

    EventSystem.current.IsPointerOverGameObject() works as expected. The only thing that you should keep in mind is, that if you use e.g. IPointerEnterHandler with PolygonCollider2D, the colliders are also treated as UI and IsPointerOverGameObject will return true if you are over a collider. To solve this, we keep track, if the mouse is over a PolygonCollider2D by simply setting a flag to true/false when entering/exiting a collider.
     
  6. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    700
    Sorry, this is not my approach, I have 3 panels not one and I have no issues with clicks because the middle panel is hidden and thus, lets the click go through.

    I apologize if I did not explain this correctly.