Search Unity

Popup fields and callbacks not working?

Discussion in 'UI Toolkit' started by rocarpen, Jun 27, 2019.

  1. rocarpen

    rocarpen

    Joined:
    Jun 27, 2019
    Posts:
    4
    I'm having problems with RegisterCallback on PopupFields instantiated by PropertyFields.

    Context:
    • Custom editor (not a drawer or window)
    • PropertyField created in UXML or C# (have tried both)
    • Want to show/hide fields when value of PopupField changes (see GIF below)
    • Polling with Schedule is a working fallback for my use case, but I'd prefer an event-driven approach.
    Symptoms:
    • Originally was unable to get reference to the PopupField (I believe I've resolved this though)
      • Solved: Wait until next frame to get PropertyField / PopupField and RegisterCallback
        • Per this post from antoine-unity: "...some specialied elements like Listview and PropertyField may create their hierarchy after one frame."
        • ^ That really seems like something that should be better documented.
        • Deferring get and register to a scheduled method call or AttachToPanelEvent both seem to work.
    • ChangeEvent<myEnumType> does not work at all
      • My expectation is it would trigger after making a selection from the dropdown menu.
      • I've only ever gotten it to work with a Vector3Field, actually. Although admittedly I have not tested it extensively (e.g. with floats, integers, text fields, etc).
    • MouseUpEvent works, but is peculiar. Clicking the label triggers it. But clicking on the menu only works if I use the right mouse button.
      • Possibly because Unity is internally eating the Mouse event? Per this post by UnityMat: "Button classes uses a Clickable manipulator internally which eats the MouseDown event and prevents its propagation." I tried the solution he shared, however, and it did not make a difference.
    Here's the component I'm building. It's a simple tween interface. I hide the "From" and "To" parameters, depending on the Mode of the tween, which is selected from a dropdown menu. The implementation captured here is built with scheduling, but as stated above, I'd prefer to use events, if possible.

     
  2. antoine-unity

    antoine-unity

    Unity Technologies

    Joined:
    Sep 10, 2015
    Posts:
    780
    The underlying value is an EnumField is Enum (this is essential the way to store any enum value without generics).
    So registering for
    ChangeEvent<Enum>
    is the way to go.

    This is probably indeed the case but the event system allows you to get first say on such events via the TrickleDown phase. See the "Registering an event callback" on this page.

    Hope this helps!
     
  3. rocarpen

    rocarpen

    Joined:
    Jun 27, 2019
    Posts:
    4
    (Sorry for the slow response; was pulled away from this for the past week. Thank you for your response!)

    Unfortunately neither
    ChangeEvent<Enum>
    nor
    TrickleDown.TrickleDown
    seem to make a difference.

    On a positive note I have successfully gotten ChangeEvent to work for...

    1) ObjectField instantiated via PropertyField (UXML)
    2)
    PopupField<string>
    instantiated via
    new PopupField
    , built from
    List<string>
    (C#)

    Narrowing down the problem: it appears to be enums specifically that are not working with PopupField ChangeEvent callbacks...
     
    Last edited: Jul 9, 2019
  4. rocarpen

    rocarpen

    Joined:
    Jun 27, 2019
    Posts:
    4
    FWIW, when I inspect the PropertyField using UI Debugger, it contains a PopupField—not an EnumField.
     
  5. rastlin

    rastlin

    Joined:
    Jun 5, 2017
    Posts:
    127
    Have you tried ChangeEvent<string> for yout PopupField?
     
  6. rocarpen

    rocarpen

    Joined:
    Jun 27, 2019
    Posts:
    4
    WTF. That works. Why does that work?

    I've been watching True Detective Season 1 again lately. So this moment popped in my head. Don't know how you thought of it, but thanks very much for the suggestion. I'm gonna go strip out a bunch of Schedules now :)
     
  7. rastlin

    rastlin

    Joined:
    Jun 5, 2017
    Posts:
    127