Search Unity

MouseDownEvent only registers right mouse click

Discussion in 'UI Toolkit' started by nilsdr, Jun 3, 2019.

  1. nilsdr

    nilsdr

    Joined:
    Oct 24, 2017
    Posts:
    374
    Hi,

    I'm using the new UI Elements framework to create some editor tools. Somehow if I add a button and register an event handler (as per the docs) it only registers a right mouse click.

    Code (CSharp):
    1. Button batchBuildButton = new Button();
    2.         batchBuildButton.RegisterCallback<MouseDownEvent>(evt => {
    3.             Debug.Log("Batch build...");
    4.             foreach(KeyValuePair<ByARConfig, SerializedObject> kvp in configs)
    5.             {
    6.                 buildAndroidPlayer(kvp.Key, true, false);
    7.             }
    8.         });
    9.         batchBuildButton.text = "Batch build Android (prod)";
    Is there something I'm missing? The documentation speaks of buttons having either default actions or event handlers, do I have to prevent the default action somehow?
     
  2. uMathieu

    uMathieu

    Unity Technologies

    Joined:
    Jun 6, 2017
    Posts:
    398
    Button classes uses a Clickable manipulator internally which eats the MouseDown event and prevents its propagation. To have your callback called when the mouse is clicked, try this:

    batchBuildButton.clickable.clicked+= () => {...};

    or pass your delegate straight to the Button's constructor.
     
    Last edited: Aug 14, 2020
    baixiaoxi likes this.
  3. lclemens

    lclemens

    Joined:
    Feb 15, 2020
    Posts:
    761
    That does not work for me. clickable.clickEvent doesn't exit in Button. There is a clickable.clickedWithEventInfo and clickable.clicked , but as far as I can tell, Button eats those as well.
     
    Last edited: Aug 13, 2020
  4. shmafoo

    shmafoo

    Joined:
    Nov 22, 2016
    Posts:
    24
    Just because I ran into this exact same issue right now: Clearing the activator list of the Clickable property fixes the issue with the left mouse button event getting consumed.

    e.g.:
    Code (CSharp):
    1. var button = root.Q<Button>("SomeButton");
    2. button.clickable.activators.Clear();
    3. // Now, if you register a callback for MouseDownEvent, it also works with the left mouse button:
    4. button.RegisterCallback<MouseDownEvent>(e => SomeCallback(e));
    I don't know why they decided to make it that difficult as there seems to just be no way do this in any other way. At least, I was unable to find any way nor an explanation on why it's designed this way or how it would work "the correct way".

    Hope this helps some people out there.
     
  5. Chuck_S

    Chuck_S

    Joined:
    Dec 11, 2018
    Posts:
    15
    @uMathieu - Can you please clarify how this is supposed to be used? I'm doing industrial simulation in Unity and I need a bypass button. The user holds the button down during a specific operation until the automation kicks in, and then they release it.

    I'm trying to make a button panel like they would have in real life, but I can't get the MouseDownEvent and MouseUpEvents to trigger correctly. I want the the MouseDownEvent to write a TRUE and the MouseUpEvent to write a FALSE, and then the user is free to hold the button as long or little as they want while they watch the process go.

    Anything added to the .clicked action only fires once, so I could write TRUE there, or I could write TRUE then immediately FALSE, but neither of those accomplishes my goal. I get exactly the behavior I want when I use the up/down events, but then those only work with the right mouse button.

    I tried @shmafoo 's solution, which does get me the behavior that I need, but it also strips the button animation, so I don't have the visual indication that the button has been clicked.

    Is there a reason the Clickable manipulator needs to eat the MouseDownEvent? Is there any way to disable that or "bubble up" the event or something?
     
  6. unity_nick_

    unity_nick_

    Joined:
    Sep 24, 2021
    Posts:
    5