Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

One mouse down fires the event twice.

Discussion in 'UI Toolkit' started by mutsuyuki, Oct 11, 2021.

  1. mutsuyuki

    mutsuyuki

    Joined:
    Apr 16, 2015
    Posts:
    19
    When I set mouse event for some element that is not a Button(like Label), callback will be called twice.

    The following is what I tried.
    I placed a Button and a Label, and clicked each once.
    Please see that the callback is called twice for the label.


    Screenshot from 2021-10-11 12-38-32.png


    Code (UXML):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <engine:UXML
    4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5.     xmlns:engine="UnityEngine.UIElements"
    6.     xmlns:editor="UnityEditor.UIElements"
    7.     xsi:noNamespaceSchemaLocation="../UIElementsSchema/UIElements.xsd"
    8. >
    9.  
    10.     <engine:Button text="button"/>
    11.     <engine:Label text="label" style="background-color:tomato;"/>
    12.  
    13. </engine:UXML>
    14.  
    Code (CSharp):
    1.  
    2. public class ClickTest : MonoBehaviour
    3. {
    4.     private void Awake()
    5.     {
    6.         var root = GetComponent<UIDocument>().rootVisualElement;
    7.  
    8.         var button = root.Q<Button>();
    9.         button.clicked += () => Debug.Log("Button clicked");
    10.  
    11.         var label = root.Q<Label>();
    12.         label.RegisterCallback<PointerDownEvent>(e => Debug.Log("Label clicked"));
    13.     }
    14. }
    15.  

    Other test is below.
    1. The test uses PointerDownEvent, but the same is true for MouseDownEvent.
    2. PointerMoveEvent also appears to have a callback called twice.
    3. The callbacks for PointerEnterEvnet, PointerOutEvent were called correctly.
     
    Nexer8 likes this.
  2. mutsuyuki

    mutsuyuki

    Joined:
    Apr 16, 2015
    Posts:
    19
    Sorry. I overlooked the relevant part of the manual.

    https://docs.unity3d.com/2021.2/Documentation/Manual/UIE-Events-Handling.html

    Manual says
    Each element along the propagation path (except the target) can receive an event twice:
    • Once during the trickle-down phase.
    • Once during the bubble-up phase.

    Now, I still want to know how to detect that current event is in trickle-down or bubble-up.
     
  3. Deleted User

    Deleted User

    Guest

    These should help you with what you want.

    bool -> EventBase.bubbles
    bool -> EventBase.tricklesDown
    Enum -> EventBase.propagationPhase

    Example:
    Code (CSharp):
    1. void UISetup(){
    2.     var root = GetComponent<UIDocument>().rootVisualElement;
    3.     var label = root.Q<Label>();
    4.     label.RegisterCallback<PointerDownEvent>(PointerDown);
    5. }
    6.  
    7. void PointerDown(PointerDownEvent e){
    8.     if(e.propagationPhase == PropagationPhase.AtTarget){
    9.         Debug.Log("Label clicked")}
    10.     }
    11. }
    Here is the full reference/info for EventBase:
    https://docs.unity3d.com/2021.2/Documentation/ScriptReference/UIElements.EventBase.html
     
    Nexer8 likes this.
  4. mutsuyuki

    mutsuyuki

    Joined:
    Apr 16, 2015
    Posts:
    19
    @RyanJEC
    Thank you very much!
    What you told me is exactly what I want to know.
     
    Deleted User likes this.
  5. mutsuyuki

    mutsuyuki

    Joined:
    Apr 16, 2015
    Posts:
    19
    @RyanJEC

    I am sorry for the delay, but I have tested the code you gave me.
    As a result, "Label Clicked" was displayed twice again.
    I run the following code to check the content of the data.
    Code (CSharp):
    1.  
    2. void PointerDown(PointerDownEvent e){
    3.                 Debug.Log("-------------");
    4.                 Debug.Log(e.bubbles);
    5.                 Debug.Log(e.tricklesDown);
    6.                 Log(e.propagationPhase);
    7. }
    8.  
    And I got the result as below.

    -------------
    True
    True
    AtTarget
    -------------
    True
    True
    AtTarget

    it seems to contain the same data both times.
    Is there anything you can tell me about the cause?
     
  6. mcoted3d

    mcoted3d

    Unity Technologies

    Joined:
    Feb 3, 2016
    Posts:
    1,007
    I can't reproduce this behavior here. Can you share more information about the Unity/UI Toolkit versions you are using?

    Also, if you have a project to share, that could help as well.
     
  7. mutsuyuki

    mutsuyuki

    Joined:
    Apr 16, 2015
    Posts:
    19
    @mcoted3d
    Thank you for your reply.

    I'm using uitoolkit included in Unity 2021.2.0b15.
    And my OS is Ubuntu 20.04.

    Here is manifest.json
    ---------------------
    ...
    "com.unity.modules.ui": "1.0.0",
    "com.unity.modules.uielements": "1.0.0",
    ...
    ---------------------

    I have created a repository that can reproduce this behavior.
    This is the very small unity project that I used when I took the first screenshot I posted.
    https://github.com/mutsuyuki/UIToolkitClickTest.git
     
  8. mcoted3d

    mcoted3d

    Unity Technologies

    Joined:
    Feb 3, 2016
    Posts:
    1,007
    Thanks for the info. I suspect this is a Linux input issue, I'm not properly setup to test this at this time, but we'll try to in the near future. Thanks for reporting!
     
  9. mutsuyuki

    mutsuyuki

    Joined:
    Apr 16, 2015
    Posts:
    19
    @mcoted3d

    As you expected, I run same project on Windows and did not have this problem.
    I usually using Unity on Linux. So I am looking forward to this behavior will fix.
     
  10. mcoted3d

    mcoted3d

    Unity Technologies

    Joined:
    Feb 3, 2016
    Posts:
    1,007
    @mutsuyuki Can you please open a bug report (Help > Report a Bug...), this will allow the QA team to properly assess the issue and send it to the right team, duplicated events like so may be caused by the Linux platform layer. Please paste the bug number in this thread when you are done.
     
  11. mutsuyuki

    mutsuyuki

    Joined:
    Apr 16, 2015
    Posts:
    19
    mcoted3d likes this.