Search Unity

Question Dropdown binding and event chain for rereshing

Discussion in 'UI Toolkit' started by sas-david, Oct 25, 2021.

  1. sas-david

    sas-david

    Joined:
    Sep 27, 2017
    Posts:
    5
    HiHello - I've been hitting some issues with structure some dropdown elements to update properly within a custom inspector. I'm fairly new to the UI Toolkit, so I imagine that my lack of experience being the main blocker to my sadness.

    I'm currently building a ui with three separate dropdown elements that hold some basic information for setting up a login event. I think I'm hitting two issues that I would appreciate some help / guidance with, the first being how to properly refresh a dropdowns data and the second being how to properly bind to dropdown elements.

    Some context - The basic need for these elements is to update their choices dynamically in order to reflect the correct login data. The first dropdown acts as the first link in the chain of elements and when that value is modified it will trigger the bottom two dropdowns to refresh their choices. I setup this chain of commands for these dropdown elements during CreateInspectorGUI when I search and setup each element with the proper callback and bindingpath. That part works out all fine, however I cannot get the values in the dropdown to update their display without manually triggering the Inspector UI to redraw itself (I usually just click to a different gameobj to trigger this redraw :| ). I had previously built out this ui with PropertyDrawers and PropertyAttribrutes, and I went through the update process manually whenever OnValidate was called. But since UI Toolkit binding stuff looks pretty spiffy, I thought that migrating from IMGUI would clean up and simplify my messy code.


    So, for Question 1 - What is the recommended approach to updating a dropdown element dynamically? Is it expected that I re-assign the values for choices during the callback events? Or is there where bindings are suppose to come in? Which sorta leads into...

    Question 2 - How does one go about binding a dropdown element? :| .... I have been using bindings with other controllers without issue, but I haven't been able to understand how dropdown binding works. I typically set these elements up with a bindingpath to a List<string>, and let the CreateInspectorGUI bind the element....but those bindings don't seem to stick.
     
  2. martinpa_unity

    martinpa_unity

    Unity Technologies

    Joined:
    Oct 18, 2017
    Posts:
    480
    Hey!
    To dynamically change the list of choices, you can mutate the "choices" list directly. In the latest versions, that list was made public and backports have been created for earlier versions. If the list is internal for you at the moment, the quickest way would be create the Dropdown through code, pass it a local list and mutate the local list.

    Binding the dropdown's content will need to be done manually at the moment. The current editor bindings can only support a sub-set of functionality. We are planning to add a generalized, runtime compatible binding solution in the future which should let you do this more easily.
     
    sas-david likes this.
  3. sas-david

    sas-david

    Joined:
    Sep 27, 2017
    Posts:
    5
    ah thank you for the clarity. I am using the latest version of UI toolkit, so I can populate choices like you are mentioning. I will continue forward with manual bindings, for a bit I thought I was going insane when the bindings didnt stick.

    Where do you recommend storing that local list that is mutated? I would imagine that is stored in the mono object so the data is stored rather than storing it in the custom editor class.
     
  4. martinpa_unity

    martinpa_unity

    Unity Technologies

    Joined:
    Oct 18, 2017
    Posts:
    480
    By this, you mean the latest UI Toolkit package? If so, you can store the local list on the mono object or you can use the
    VisualElement.userData
    to store it.

    If you are using the latest version of Unity where UI Toolkit is built-in, you can use the
    choices
    list directly, no need to have another list.
     
    sas-david likes this.
  5. sas-david

    sas-david

    Joined:
    Sep 27, 2017
    Posts:
    5
    oh didn't know about the userdata field there, but I think i'll continue to use the mon object as the main data container and only send the formatted list of strings into the choices field of the visual element.