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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more..
    Dismiss Notice
  3. Dismiss Notice

Question Registering an change event with a custom visual element.

Discussion in 'UI Toolkit' started by WizByteGames, Jul 1, 2020.

  1. WizByteGames

    WizByteGames

    Joined:
    Mar 28, 2013
    Posts:
    70
    So I'm working on creating a visual element that has a list of of elements. Each element will have 2 child elements one as a popup field and one as a float field. I have a few questions that I need answered so I can get it to work properly.

    1. If I register events for both the popup and and a float field what type of change event would I have to register for the main visual element.

    2. Is there a event that can tell if the main visual elements child count has changed.

    I can't find much info when it comes to creating our own visual elements. The only thing I could find was creating basic visual elements, and that helps a bit but not for any advance visual elements or components.
     
  2. uDamian

    uDamian

    Unity Technologies

    Joined:
    Dec 11, 2017
    Posts:
    1,203
    Depends what you want to know. Keep in mind that change events propagate up the hierarchy, so you can just register for the ChangeEvent<float> on the main visual element and catch all the changes in the child FloatField.

    There is no such event. You have AttachToPanel event that you can register on the child and know when the child has been added to the parent. And if you're adding elements via UXML, you can register for the GeometryChangeEvent on the parent to know when all children have been fully created (this event also fires every time the element changes size/shape so you might want unregister from it after the first trigger).

    There's a bit more info here:
    https://forum.unity.com/threads/ui-builder-and-custom-elements.785129/#post-5225297
     
  3. WizByteGames

    WizByteGames

    Joined:
    Mar 28, 2013
    Posts:
    70
    The info you linked is basically what all my custom visual elements is based on. I thought there would have been more advanced examples around. But the info is still useful.

    Also AttachToPanel wasn't somthing I had thought of but it could be something worth trying. Thing is from the sound of it it sounds like it would only work for adding child elements and not removing them.

    I do have one more question that maybe would help with more advance use of the RegistarCallback function.

    Is there a way to create your own event type that you could pass into the RegistarCallback function?
     
  4. uDamian

    uDamian

    Unity Technologies

    Joined:
    Dec 11, 2017
    Posts:
    1,203
    There is the opposite "DetachFromPanelEvent" for that. :)

    Yes, you can. It just needs to inherit from and implement EventBase<T>. Follow the inheritance of ChangeEvent<T> as a clue. But you then also have to be the one that sends this custom event. I don't have a full example handy but here's a function that sends an artificial ChangeEvent<T> to an element:
    Code (CSharp):
    1. void DispatchChangeEvent<TValueType>(BaseField<TValueType> field)
    2. {
    3.     var e = ChangeEvent<TValueType>.GetPooled(field.value, field.value);
    4.     e.target = field;
    5.     field.SendEvent(e);
    6. }
     
  5. WizByteGames

    WizByteGames

    Joined:
    Mar 28, 2013
    Posts:
    70
    Ah OK thanks for the info. Now I have to go fix my custom Visual Elements so they can work properly. I was kind of using a hacky workaround that only worked in certain situations.