Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Trickles Down and Bubbles Up for custom events

Discussion in 'UI Toolkit' started by LadyCookie, Nov 19, 2020.

  1. LadyCookie

    LadyCookie

    Joined:
    Oct 4, 2018
    Posts:
    4
    Hello!
    I'm trying to send a custom Event within a UI element but I want to register the callback on the visualelement root.
    My custom Event definition :
    upload_2020-11-19_9-51-7.png
    The callback management :
    upload_2020-11-19_9-51-54.png
    upload_2020-11-19_9-52-14.png

    And now how I send the event :
    upload_2020-11-19_9-52-53.png

    With some test I've noticed that the bubbles field is set up to false but I don't know how to set it to true.
    Is there a way to bubble up (and trickle down) custom events without recoding the entire process?

    Thanks you
     

    Attached Files:

  2. hkvik

    hkvik

    Joined:
    May 4, 2020
    Posts:
    1
    I am also having the same issue, would love to know the proper way to to this.

    I have been able to solve this with reflections but would like to avoid that if possible
    Code (CSharp):
    1. public static class UIElementsUtility
    2.     {
    3.         public static void SetPropagation(this EventBase e, bool bubbleUp, bool trickleDown, bool cancellable)
    4.         {
    5.             typeof(EventBase)
    6.                 .GetProperty("propagation", BindingFlags.NonPublic | BindingFlags.Instance)
    7.                 ?.SetValue(e, (bubbleUp ? 1 : 0) | (trickleDown ? 2 : 0) | (cancellable ? 4 : 0));
    8.         }
    9.     }
     
    Last edited: Nov 19, 2020
  3. uBenoitA

    uBenoitA

    Unity Technologies

    Joined:
    Apr 15, 2020
    Posts:
    198
    Unfortunately the
    propagation
    property and the
    EventPropagation
    enum are internal at the moment. For now I'd say you have three choices:

    1. Your custom event could derive from an existing base event that has the right propagation values, for example
    public class CustomEvent : MouseEventBase<CustomEvent> { }


    2. You can checkout our package locally, then modify what you need to be public. Alternatively, you can grant your code access to all UIElementsModule internals by adding your assembly to the list of
    [assembly:InternalsVisibleTo("Blablah")]
    in com.unity.ui/Core/AssemblyInfo.cs.

    3. You can use reflection, as hkvik suggested. To make it more convenient for you to use in the future, you could create a base event type, say
    CustomEventBase<T> where T : CustomEventBase<T>
    with your own version of a public virtual
    propagation
    property that you could then use in your code. You could make that property use the reflection bit from above, but keep it safely wrapped there, and then define
    CustomEvent : CustomEventBase<CustomEvent>
    as you need.
     
  4. tonycoculuzzi

    tonycoculuzzi

    Joined:
    Jun 2, 2011
    Posts:
    301
    just checking, has this been addressed as of 2020.2?

    I'm running into a lot of issues implementing custom bubbling+trickling events and I'm wondering if this may be why.
     
  5. uMathieu

    uMathieu

    Unity Technologies

    Joined:
    Jun 6, 2017
    Posts:
    384
    EventBase has bubbles and tricklesDown protected properties that will let you control this from your custom event type.
     
  6. tonycoculuzzi

    tonycoculuzzi

    Joined:
    Jun 2, 2011
    Posts:
    301
    I tried setting both to true but it still appeared to not be working. I ended up working around this, so I'm not sure if it was my own fault or a bug, unfortunately. I'll let you know if I come across the same issue again though!
     
  7. fipsy

    fipsy

    Joined:
    Sep 3, 2016
    Posts:
    13
    For anyone wondering how to correctly set the bubbles and tricklesDown protected properties inside their custom event:

    Code (CSharp):
    1. public class MyCustomEvent : EventBase<MyCustomEvent>
    2.         {
    3.             protected override void Init()
    4.             {
    5.                 base.Init();
    6.                 bubbles = true;
    7.                 tricklesDown = true;
    8.             }
    9.         }
     
    KillHour and Denis-535 like this.