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

Sending custom event from child to parent

Discussion in 'UI Toolkit' started by echekanskiy, Sep 27, 2020.

  1. echekanskiy

    echekanskiy

    Joined:
    Aug 16, 2020
    Posts:
    16
    First of all, it that possible to create custom event and use it in Register/Unregister callback?
    And second question, how to send event from child visual element to parent visual element?

    What I am trying to create - some generic editor window that accepts content VisualElement as a content and add it to Window.rootVisualElement. I want my button located in VisualElement send some CloseEditorWindowEvent and add callback for this event to Window.rootVisualElement in order to intercept it and close actual editor window. I want to do this in order to encapsulate content from knowing anything about where its located.

    Thanks in advance.
     
  2. echekanskiy

    echekanskiy

    Joined:
    Aug 16, 2020
    Posts:
    16
    I was not able to route my custom message until this reflection magic added.
    Code (CSharp):
    1.     public class CloseWindowEvent : EventBase<CloseWindowEvent>
    2.     {
    3.         public CloseWindowEvent()
    4.         {
    5.  
    6.         }
    7.  
    8.         protected override void Init()
    9.         {
    10.             base.Init();
    11.             PropertyInfo propagation = typeof(EventBase).GetProperty("propagation", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
    12.             propagation.GetSetMethod(true).Invoke(this, new object[] { 1 | 2 | 4 });
    13.         }
    14.     }
    Internal unity code checks for propagation flag(which is 0 by default) and do nothing in case of 0. Not sure how to set it without reflection, since property is internal. Maybe Unity guys can help with this.
     
    nvidialover likes this.
  3. echekanskiy

    echekanskiy

    Joined:
    Aug 16, 2020
    Posts:
    16
    Also, another solution without reflection:
    Code (CSharp):
    1.                             var closeWindowEvent = ChangeEvent<WhateverUniqueType>.GetPooled();
    2.                             closeWindowEvent.target = item;
    3.                            
    4.                             item.SendEvent(closeWindowEvent);
    Ensure to replace WhateverUniqueType to something unique to your event type. Use ChangeEvent<WhateverUniqueType> in callbacks.
     
    nvidialover and MostHated like this.
  4. uMathieu

    uMathieu

    Unity Technologies

    Joined:
    Jun 6, 2017
    Posts:
    384
    The EventBase class has the tricklesDown and bubbles protected properties that are accessible proxies to the propatagion internal property.
     
  5. echekanskiy

    echekanskiy

    Joined:
    Aug 16, 2020
    Posts:
    16
    But how to set them without reflection? None of that properties has setters, that is why I used reflection.
     
  6. uMathieu

    uMathieu

    Unity Technologies

    Joined:
    Jun 6, 2017
    Posts:
    384
    Oh you're right, the setters were introduced in 2020.2. If you're on 2020.1 you can install the com.unity.ui package, otherwise I guess you're stuck with reflection.
     
    echekanskiy likes this.
  7. echekanskiy

    echekanskiy

    Joined:
    Aug 16, 2020
    Posts:
    16
    Ha, I am on 2019.4. Anyway, thanks for replies, thats make sense now.