How to populate dropdown dynamically with list of objects and get selected object?

Note: I’m new to Unity and got thrown onto a project that’s developed around Unity.

I’m using a UnityWebRequest to get some JSON data which is then being parsed into a list of objects corresponding to a specific class, and then I display this on a UI.

I’m tasked with the following:

  1. Dynamically populate a dropdown containing some, but not all, fields of the class with user-friendly names (which fields should be determined in the class itself).
  2. A textbox to type in a string to search for instances of that class where the string value equals the value of the selected property in the dropdown.
  3. Filter the list of objects being displayed based on the above.

The fields for the class could change in the future, so the only place where changes should have to be made are in the class itself.

For example, I have the following two classes:

[Serializable]
public class CarData
{
    public int carID;
    public string carOwner;
    public string makeName;
    public string modelName;
}

[Serializable]
public class CarDataList
{
    public List<CarData> CarList;
}

I parse my JSON data into the above like so:

CarDataList carDataList = JsonUtility.FromJson<CarDataList>(resultData); //where resultData is json data

Now, let’s say the user only wants carID and carName in the dropdown menu, but wants user-friendly names like ID and Owner.

Is there an easy way to do this? Currently, I created my own Attribute class to put custom attributes onto each field such as a DisplayName and IsRequired attribute. I then loop through each field and check if IsRequired is true, and if it is I add it’s DisplayName to the dropdown menu. This seems like it’s harder than it should be.

If I use the above, then the search functionality is even more difficult. I would have to get the text of the selected dropdown item, compare it to each field’s custom attribute in the class to determine what field it corresponds to, and then use more reflection to get the name of that field before finally doing the actual filtering.

I come from an MVVM WPF background. I’m used to being able to bind dropdowns to an ObservableCollection of objects, setting specific display names, etc., and getting the actual selected object back, not just an int value.

In Unity, it looks like I can’t do any of that easily.

Any advice would be greatly appreciated.

Like you, I have years of experience in other programming languages, but have only been in unity for a year. However I have had to solve some problems in UI similar to yours. I actually looked for a screencast they used to have posted, but Unity has changed up their learn section over the past year and I can not find it. However I will give you some insight that may help. Here are the basic steps you need to solve:

  1. Create a UI prefab for you list item. This will likely start out as a button because you want interactivity, so it will have the necessary components attached.
  2. You will need to code a list manager component for handling population and filtering of your list
  3. You will need to code an object pool.
  4. In the case of dynamic lists, understanding events and event delegates will be extremely helpful.

That being said, I have found more fast help on youtube than anywhere else. However I believe the events/delegates, and object pool are under the scripting tutorials on Unity Learn. In fact, being new to unity, I would highly recommend you asking your employer for a crash course day where you can just sit and go through all the posted scripting tutorial videos. Since you have experience, you won’t have to work them, just watch the short videos and you will start to understand how unity works from the scripting side. This will save you a TON of headaches down the road as you become familiar.

Also there is a great UI package called “Unity UI Extensions” that you should get. It’s got a lot of nice UI enhancements that will make your life a lot easier.

OK, so for your list button, just start with a UI button object, add yourself a custom script so you can do stuff like make a public method SetData(myData) to populate any text, sprites, object references, etc. What I also like to do is create an OnClick() public method, and then tie that to the Button Click Event list in the inspector. Then I use the OnClick() to dispatch an event and pass the data though the event.

The ObjectPool is where you will get your list button instances from, and put them back between list loads. It will use your prefab to create new instances when needed, otherwise, pull from existing ones on a previous list load.

In the ListManager, when I load a list of data say, like your json, then you can set a method to the delegate on the button you created for listening to a click event. Just make sure the signatures match in the event you are passing the data. Also make sure to remove them from the delegate when you remove the buttons from the list and put them back in the object pool.

This will actually be a pretty nice exercise for you and once you solve it, you’ll have a grip on whats going on with Unity UI. Sorry I could not give you more specific ‘how to’, but this should at least get you in the right direction for start.