0
\$\begingroup\$

I'm using the UI Dropdown component which lets you set the text of an option (OptionData) only. The value of an option is the index position in the options list, so there is no way to set this.

My problem is that I have a dictionary<string, tile> that I use to populate the dropdown options, but I would like the value of the option to be the id of the tile class, not the index position.


Edit:

I know I can access elements from the list by using the value (index).

list[dropdown.value].id

But I wanted to see about extending the dropdown with this small change, as I have some other changes I would like to make, but wanted to start with something simple first just to get an idea of what is possile.

I do not want someone to post a full code solution. Am mostly after some pointers. Looking at the dropdown class, I don't see how I can extend it to modify some things (value in this case), it looks like I would have to implement by own version?


Some code will make this a little clearer I think:

// All tiles into list.
List<Tile> list = new List<Tile>(mydictionary.Values);

// Sort all tiles based on the tile name.
list.Sort((x,y) => x.name.CompareTo(y.name));

// Current way to add list to dropdown.
for(int i=0; i<list.Count; i++){
    dropdown.options.Add(new Dropdown.OptionData(){
        text = list[i].name // Tile name (string).
    }); 
});

// What I want to do.
for(int i=0; i<list.Count; i++){
    dropdown.options.Add(new Dropdown.OptionData(){
        text = list[i].name, // Tile name (string).
        value = list[i].id // Unqiue tile id (string).
    }); 
});

// Getting changed value
dropdown.onValueChanged.AddListener(delegate {
    Debug.Log(dropdown.value); // Instead of the index selected, I want the tile id
});

Or if the value is needed internally for some reason, then maybe an extra property that can be set and retrieved.

for(int i=0; i<list.Count; i++){
    dropdown.options.Add(new Dropdown.OptionData(){
        text = list[i].name, // Tile name (string).
        tile_id = list[i].id // Unqiue tile id (string).
    }); 
});

dropdown.onValueChanged.AddListener(delegate {
    Debug.Log(dropdown.tile_id);
});

I took a look at the DropDown class to see if I could extend it and add the "tile_id" option in, but I got completely lost on what to do, as I am still quite new to C#.

https://bitbucket.org/Unity-Technologies/ui/src/a3f89d5f7d145e4b6fa11cf9f2de768fea2c500f/UnityEngine.UI/UI/Core/Dropdown.cs

https://docs.unity3d.com/ScriptReference/UI.Dropdown.html

\$\endgroup\$
1
\$\begingroup\$

What's wrong with doing

dropdown.onValueChanged.AddListener(delegate {
    Debug.Log(list[dropdown.value].id); 
});

You can use that id to index into the list you filled the dropdown with.

\$\endgroup\$
2
  • \$\begingroup\$ Yeah, I know I can do it like that. But I wanted to see about extending the dropdown class so I didn't need to rely on the list of options for accessing the selected element id. \$\endgroup\$ – JacketPotatoeFan Mar 28 '18 at 11:24
  • \$\begingroup\$ Edited my question just to clarify what I am after. It's more of me learning to extend something than needing it. However, I don't think I can make the change I want if I am understanding things correctly from looking at the source. \$\endgroup\$ – JacketPotatoeFan Mar 28 '18 at 11:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.