Search Unity

MouseDownEvent only registers right mouse click

Discussion in 'UIElements' started by nilsdr, Jun 3, 2019.

  1. nilsdr

    nilsdr

    Joined:
    Oct 24, 2017
    Posts:
    193
    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:
    28
    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.clickEvent += () => {...};

    or pass your delegate straight to the Button's constructor.
     
    baixiaoxi likes this.