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

Callback through sort order

Discussion in 'UI Toolkit' started by Weidz_, May 12, 2022.

  1. Weidz_

    Weidz_

    Joined:
    Feb 15, 2018
    Posts:
    40
    Hello,
    I wish to use a UIDocument as a overlay to manage dragged visualElements but once I get those on top of the others, the elements behind the dragged visualElements (on another UIDocument) wont fire MouseOver/MouseEnter events, I tried both with/without trickleDown but I can't get it to work.
    Am I missing something ? Is it not possible ?
     
  2. jonathanma_unity

    jonathanma_unity

    Unity Technologies

    Joined:
    Jan 7, 2019
    Posts:
    229
    Hi, this seems to be because the UIDocument on top is receiving the mouse events. You can set the picking-mode to ignore on the overlay and that should let the events pass.
     
  3. Weidz_

    Weidz_

    Joined:
    Feb 15, 2018
    Posts:
    40
    Is there a way I can get both to trigger events ?
    I wish to use the MouseMoveEvent callback on the top layer (dragged elements on the whole screen) but also receive Enter/Leave events on the layer below (actual UI with drop targets).
     
  4. JuliaP_Unity

    JuliaP_Unity

    Unity Technologies

    Joined:
    Mar 26, 2020
    Posts:
    666
    If you set the picking mode to only the overlay (i.e. the empty/transparent base element that expands to cover the whole game view/screen), but keep your actual Visual Elements with picking mode set to position (the default value), those elements will still get mouse events and the elements in other sort orders that are seen on the empty/transparent areas should get input as well.

    Have you tried that?
     
  5. Weidz_

    Weidz_

    Joined:
    Feb 15, 2018
    Posts:
    40
    Here is my setup at the moment :
    (Drag layer has one element that entirely cover the screen)



    When I click on a draggable element it's moved to the drag layer.



    | If you set the picking mode to only the overlay [...] but keep your actual Visual Elements with picking mode set to position
    A this point I can't ignore the drag layer (overlay) as I use its MouseMove event manage the drag. I tried using the event from the draggable element itself but it would bring two major issues : moving too fast allow the mouse to leave the element and no longer register MouseMove events and you could also drag the mouse out of the game window, leaving the element at the border, and re-enter the mouse elsewhere where the element is not, also breaking the drag.



    So right now I end up here where I can drag the element around but the drop target behind won't trigger MouseEnter/MouseLeave semingly because it won't go past the drag layer
     
  6. uBenoitA

    uBenoitA

    Unity Technologies

    Joined:
    Apr 15, 2020
    Posts:
    198
    Not 100% sure it'll work, but set pickingMode to Ignore on the drag layer, then try this

    Code (CSharp):
    1. VisualElement myDraggedElement, dragLayerElement, myDropTarget1, myDropTarget2, dropTarget;
    2. // ...
    3.  
    4. myDraggedElement.RegisterCallback<PointerDownEvent>(e =>
    5. {
    6.     dragLayerElement.CapturePointer(e.pointerId);
    7. });
    8.  
    9. dragLayerElement.RegisterCallback<PointerMoveEvent>(e => Debug.Log("Moving on the drag layer."));
    10.  
    11. myDropTarget1.RegisterCallback<PointerEnterEvent>(e => dropTarget = myDropTarget1);
    12. myDropTarget1.RegisterCallback<PointerLeaveEvent>(e => dropTarget = null);
    13. myDropTarget2.RegisterCallback<PointerEnterEvent>(e => dropTarget = myDropTarget2);
    14. myDropTarget2.RegisterCallback<PointerLeaveEvent>(e => dropTarget = null);
    15.  
    16. dragLayerElement.RegisterCallback<PointerUpEvent>(e =>
    17. {
    18.     dragLayerElement.ReleasePointer(e.pointerId);
    19.     if (dropTarget != null)
    20.         Debug.Log("Dropping " + myDraggedElement + " on " + dropTarget);
    21. });
    If I'm not mistaken, the CapturePointer will ensure that your dragLayer gets all the pointer events while it's capturing the pointer, except the PointerEnter/Leave events which aren't affected by capture.
     
    jonathanma_unity and Weidz_ like this.
  7. Weidz_

    Weidz_

    Joined:
    Feb 15, 2018
    Posts:
    40
    It work ! Thanks a lot.