Search Unity

can custom a event now?

Discussion in 'UI Toolkit' started by wang37921, Apr 1, 2020.

  1. wang37921

    wang37921

    Joined:
    Aug 1, 2014
    Posts:
    102
    Code (CSharp):
    1.  
    2. public class MyEvent: EventBase<MyEvent>
    3. {}
    4.  
    5. // In MyElement somewhere
    6. using(var evt = MyEvent.GetPooled())
    7. {
    8.     this.SendEvent(evt);
    9. }
    some thing like above code. but in the root element register the custom event, not get the event callback!
     
  2. wang37921

    wang37921

    Joined:
    Aug 1, 2014
    Posts:
    102
    ...any officials..
    debug unity internal code using Rider. i found the key is EventBase.propagation.
    But it is a internal property of EventBase! so user cannt using a custom event now!

    Code (CSharp):
    1.  
    2.     public class CustomEvent : EventBase<CustomEvent>
    3.     {
    4.         protected override void Init()
    5.         {
    6.             base.Init();
    7.             // propagation is internal!
    8.             // this.propagation = EventPropagation.Bubbles;
    9.         }
    10.     }
     
  3. wang37921

    wang37921

    Joined:
    Aug 1, 2014
    Posts:
    102
    using reflection...
    Code (CSharp):
    1.     public class CustomEvent : EventBase<CustomEvent>
    2.     {
    3.         protected override void Init()
    4.         {
    5.             base.Init();
    6. /*
    7.         internal enum EventPropagation
    8.         {
    9.             None = 0,
    10.             Bubbles = 1,
    11.             TricklesDown = 2,
    12.             Cancellable = 4,
    13.         }
    14. 1 is Bubbles...
    15. */
    16.             var prop = typeof(CustomEvent).GetProperty("propagation");
    17.             prop?.SetValue(this, 1);
    18.         }
    19.     }
    now the custom Event works..