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 Is PickingMode working correctly?

Discussion in 'UI Toolkit' started by Sluggy, Mar 12, 2023.

  1. Sluggy

    Sluggy

    Joined:
    Nov 27, 2012
    Posts:
    840
    I had an issue yesterday where I couldn't get the mouse events to register behind a certain UI element. It was weird because it had been working the day before. Then I came to realize I had reset the USS style that had been controlling it because I decided it would be better to set that via C# code. However, it does not appear to be having any effect.

    To be clear, I can successfully allow the mouse to be ignored by UI elements if I set their style sheet via the UI Builder. But when I set that same value in C# code with something like 'CursorRoot.pickingMode = PickingMode.Ignore;' it appears to have no effect whatsoever and the UI element still intercepts all mouse actions. Is this a bug?
     
  2. aniol21

    aniol21

    Joined:
    May 3, 2023
    Posts:
    5
    I'm having the same issue, it seems like pickingMode is not getting applied when set through code. On the other hand, setting other properties like focusable on the same element work fine through code.
     
  3. griendeau_unity

    griendeau_unity

    Unity Technologies

    Joined:
    Aug 25, 2020
    Posts:
    230
    What Unity version are you on? I cannot reproduce on our latest.
    I tried the following code and my button doesn't receive click events, as expected.

    Code (CSharp):
    1.  
    2. var button = document.rootVisualElement.Q<Button>("Button");
    3. button.pickingMode = PickingMode.Ignore;
    4.  
    5. button.clicked += () => Debug.Log("Clicked");
    6.  
    Note also that pickingMode only applies to the element it's being set on, and not its children.
    If you want to prevent pointer events on children, you can register a callback on the
    PointerDownEvent/PointerMoveEvent/PointerUpEvent
    on
    TrickleDown
    and call
    evt.StopPropagation()
    .
     
  4. Sluggy

    Sluggy

    Joined:
    Nov 27, 2012
    Posts:
    840
    I am stuck on 2022.2.14f due to to various other bugs I currently don't want to deal with in later releases.
     
  5. griendeau_unity

    griendeau_unity

    Unity Technologies

    Joined:
    Aug 25, 2020
    Posts:
    230
    You can report the issue with a small repro project using Help -> Report a Bug... It might be a problem with only older versions, but we'll be able to do a full check that way.
     
  6. ckempke

    ckempke

    Joined:
    Dec 15, 2020
    Posts:
    20
    Did this get reported? I'm having the same problem (well, a variant of it) in 2021.3 LTS. I have a dragged element that has children in it (basically an "item" with a "stack size" imposed on it). I can't stop propagation of the events because I "do" need things under the dragged item to get the clicks. Setting PickingMode=Ignore on the item works if it has no children. Recursively setting PickingMode=ignore on all the children works sometimes, not others, and seems clumsy in any case.

    It also seems that the very existence of children makes this not work (sometimes). The mouse (up, in my case) event doesn't even have to be over one of the children for it to get eaten.

    The above messages imply that you can set PickingMode through USS rather than code, and it'll work. What property is that? I'm not seeing it listed on the USS properties list page.

    [Edited later: It's not USS; it can be set via UIBuilder as an XML attribute on the actual object in UXML, which doesn't help me, since my objects are created dynamically based on the items.]
     
    Last edited: Jul 14, 2023
  7. Sluggy

    Sluggy

    Joined:
    Nov 27, 2012
    Posts:
    840
    It's at the very top under the 'Attributes' heading.

    EDIT: Apparently I can't just link an image on the forums.


     

    Attached Files:

  8. ckempke

    ckempke

    Joined:
    Dec 15, 2020
    Posts:
    20
    Yeah, that's what I found, too. It's an attribute on the object, though, which means the entire object needs to be created from UXML, not just styled with USS.