UIElements button MouseDownEvent doesn't appear
I am a newbie to the new UIElements system. Recently I loaded and modified base templates and code examples from docs. Most part works fine, but I have a problem with a handler of a mouse down event. Here code examples:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
using UnityEngine.Events;
using UnityEditor;
public class MyWindow : EditorWindow {
static EditorWindow window;
[MenuItem ("Window/My Window")]
public static void ShowWindow () {
window = EditorWindow.GetWindow(typeof(MyWindow));
VisualTreeAsset uiAsset = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/StorylineEditor/Editors/Experimental/test.uxml");
StyleSheet styles = AssetDatabase.LoadAssetAtPath<StyleSheet>("Assets/StorylineEditor/Editors/Experimental/styles.uss");
VisualElement ui = uiAsset.CloneTree("");
window.rootVisualElement.Add((VisualElement)ui);
window.rootVisualElement.styleSheets.Add(styles);
VisualElement test = UQueryExtensions.Query(window.rootVisualElement, "id").First();
Debug.Log(test);
test.RegisterCallback<MouseUpEvent>(MouseUpHandler);
test.RegisterCallback<MouseDownEvent>(MouseDownHandler);
test.GetFirstAncestorOfType<Box>().RegisterCallback<MouseDownEvent>(MouseDownHandler);
test.HandleEvent(MouseDownEvent.GetPooled());
test.SendEvent(MouseDownEvent.GetPooled());
}
static void MouseUpHandler(MouseUpEvent e)
{
VisualTreeAsset partAsset = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/StorylineEditor/Editors/Experimental/testTemplate.uxml");
VisualElement part = partAsset.CloneTree("");
Debug.Log("MouseUp");
((VisualElement)(e.target)).GetFirstAncestorOfType<Box>().Add(part);
}
static public void MouseDownHandler(MouseDownEvent e)
{
Debug.Log("MouseDown");
}
void OnDisable()
{
}
void OnGUI () {
// Nothing to do here, unless you need to also handle IMGUI stuff.
}
}
Here is my UXML file:
<?xml version="1.0" encoding="utf-8"?>
<UXML
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="UnityEngine.UIElements"
xsi:noNamespaceSchemaLocation="../UIElementsSchema/UIElements.xsd"
xsi:schemaLocation="UnityEngine.UIElements ../UIElementsSchema/UnityEngine.UIElements.xsd">
<Label text="Select something to remove from your suitcase:"/>
<Box>
<Button name="id" class="" text="Procedural test"/>
</Box>
<Box>
<Button name="cancel" text="Cancel" />
<Button name="ok" text="OK" />
</Box>
</UXML>
Speaking about additional template and MouseUpEvent, everything works great.
The main problem is that event handler for $$anonymous$$ouseDownEvent on Button element does not work
Answer by lclemens · Aug 13, 2020 at 04:13 AM
I think the issue is that MouseDownEvent doesn't work for buttons because the button "soaks up" the event and doesn't pass it on like it does for MouseUpEvent.
One option is to make your own custom button type that inherits Button.
Another option is to use a Label instead of a Button, but you would need to re-implement the logic if you still want the "clicked" event functionality.
Your answer
Follow this Question
Related Questions
Unity - Button causes lag on a mobile device 2 Answers
Codeless IAP causing inspector bug 0 Answers
Cursor lock prevents UI interaction 0 Answers
Unity v2018.1.3f1 Button Bug 2 Answers
Buttons work in the Editor but not in the build... WHY! 0 Answers