Is there a way to let a VisualElement event bubble up through the VisualTree without overriding HandleEventBubbleUp or HandleEventTrickleDown methods?

I am looking for a way to detect events happening in VisualElements down the VisualTree without having overriding HandleEventBubbleUp or HandleEventTrickleDown in the VisualElements that trigger the event. I know I can create CustomControls for those VisualElement and override HandleEventBubbleUp or HandleEventTrickleDown but I would prefer not to have to create a CustomControl for all possible VisualElements just to override those methods. Is it possible to do it in another way?

Have you thought about using RegisterCallbacks instead?

You can register a callback during the TrickleDown phase like this:

myVisualElement.RegisterCallback<EventType>(evt =>
{
    Debug.Log("foo");
}, TrickleDown.TrickleDown);
2 Likes

Thank you so much, that did the trick.

For completion only: I was also experimenting with:

[EventInterest(typeof(AttachToPanelEvent))]
protected override void HandleEventTrickleDown(EventBase currentEvent)
{
     base.HandleEventTrickleDown(currentEvent);

    if (currentEvent.GetType() == typeof(AttachToPanelEvent))
    {
        Debug.Log("AttachToPanelEvent");
    }
    else if (currentEvent.GetType() == typeof(DetachFromPanelEvent))
    {
        Debug.Log("DetachFromPanel");
    }
}

This ^ works too but I found your solution to be more elegant. Thank you again for the reply.

1 Like

Was this broken sometime after or there was some intentional change?
I can’t seem to handle the AttachToPanelEvent anywhere in the code except for the specific elements that are being attached.

This thread seems to suggest that it actually trickles down from the parent like most other events, but seems like it’s not.

I even tried the event debugger, and it sees 0 AttachToPanelEvent being registered during runtime.

Not all events bubble down or trickle up. Panel events, for example, don’t.

You can see which ones do and don’t on the docs: Unity - Manual: Panel events