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.
<?xml version="1.0" encoding="utf-8"?>
<engine:UXML
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:engine="UnityEngine.UIElements"
xmlns:editor="UnityEditor.UIElements"
xsi:noNamespaceSchemaLocation="../UIElementsSchema/UIElements.xsd"
>
<engine:Button text="button"/>
<engine:Label text="label" style="background-color:tomato;"/>
</engine:UXML>
public class ClickTest : MonoBehaviour
{
private void Awake()
{
var root = GetComponent<UIDocument>().rootVisualElement;
var button = root.Q<Button>();
button.clicked += () => Debug.Log("Button clicked");
var label = root.Q<Label>();
label.RegisterCallback<PointerDownEvent>(e => Debug.Log("Label clicked"));
}
}
Other test is below.
The test uses PointerDownEvent, but the same is true for MouseDownEvent.
PointerMoveEvent also appears to have a callback called twice.
The callbacks for PointerEnterEvnet, PointerOutEvent were called correctly.
1 Like
Sorry. I overlooked the relevant part of the manual.
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.
system
October 11, 2021, 11:44pm
3
These should help you with what you want.
bool → EventBase.bubbles
bool → EventBase.tricklesDown
Enum → EventBase.propagationPhase
Example:
void UISetup(){
var root = GetComponent<UIDocument>().rootVisualElement;
var label = root.Q<Label>();
label.RegisterCallback<PointerDownEvent>(PointerDown);
}
void PointerDown(PointerDownEvent e){
if(e.propagationPhase == PropagationPhase.AtTarget){
Debug.Log("Label clicked")}
}
}
Here is the full reference/info for EventBase:
https://docs.unity3d.com/2021.2/Documentation/ScriptReference/UIElements.EventBase.html
1 Like
@
Thank you very much!
What you told me is exactly what I want to know.
1 Like
@
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.
void PointerDown(PointerDownEvent e){
Debug.Log("-------------");
Debug.Log(e.bubbles);
Debug.Log(e.tricklesDown);
Log(e.propagationPhase);
}
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?
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.
@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.
Contribute to mutsuyuki/UIToolkitClickTest development by creating an account on GitHub.
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!
@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.
@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.