- Home /
Poor performance with adding List to Dropdown
Hi!
I am having an issue with extremely poor performance when making a list of child components that are displayed in TMP Dropdown. The list is huge (+5000 children) but even with 500 it is not much better.
I have a seen an architectural mobile app that did just the same with even a greater list of child objects so I am certain it is my poor technical execution and not a technical limitation of Unity. I don't need to form a list of anything else but the name of the child.
Here's the execution:
List names = new List() {"test"};
public TMP_Dropdown dropdown;
public GameObject ImportedModel;
private string rename;
void Start()
{
var list0 = ImportedModel.GetComponentsInChildren(typeof(Component));
for (int i0 = 3; i0 < list0.Length; i0++)
{
rename = list0[i0].name;
names.Add(rename.ToString());
}
I would appreciate tips on how to optimize performance to make this work, for example whether I should use something other than a List. The child objects also exist in an XLM. document so they could be read from a file and not inside the program from an object.
EDIT: The issue is not with collection of children components but applying a list formed of them to TMP Dropdown.
What action exactly is giving poor performance?
The script seems fine, although you could add a stopwatch on the Imported$$anonymous$$odel.GetComponentsInChildren(typeof(Component));
call and see how long it takes, but I don't think it should be that bad.
Need some more information, it could also help to take a look at the unity profiler.
Thanks for your reply. It is indeed a bit puzzling. I think the issue is with T$$anonymous$$P_Dropdown itself, as I can run the for loop in another connection, searching for child components with great performance. I'll have a deeper look on profiler, although I already quickly checked it but could not find the issue.
I don't suppose this'll make a difference that makes a difference but the redundant function call in rename.ToString()
niggles me. I guess maybe it gets compiled out.
Also do you need to initial the List before the Start function? If not then you could gain a bit by only creating it once you know how big it'll be, setting the capacity at that point. That should speed up the Adds a little.
I'm not sure that it can be optimized too much, it is a lot of components you are talking about. But as @ShadyProductions suggested ti$$anonymous$$g each part might help to deter$$anonymous$$e the exact cause for the performance loss. That being said, there are a couple of tweaks you can implement, but I don't think they will help you much. 1. Cache the children number 2. Create a new list with that capacity 3. Use the cached number to iterate 4. Don't use ToString(), it looks redundant anyway
So the final code will look like this:
List<string> names;
public T$$anonymous$$P_Dropdown dropdown;
public GameObject Imported$$anonymous$$odel;
void Start()
{
var list0 = Imported$$anonymous$$odel.GetComponentsInChildren(typeof(Component));
int count = list0.Length;
names = new List<string>(count);
for (int i0 = 3; i0 < count; i0++)
{
names.Add(list0[i0].name);
}
}
$$anonymous$$any thanks for the comments. I implemented antonsem's script.
Unfortunately performance is still extremely poor. I think this related to T$$anonymous$$P_Dropdown; I can debug log the names of the +5000 components without an issue but when they are added to T$$anonymous$$P_Dropdown ( dropdown.AddOptions(names); - that I added after the for loop).
The performance is so bad it takes $$anonymous$$utes just to stop running the program.
EDIT: For some reason, every name is applied to three times to the list. So if part1 is one child, it is listed three times in the Dropdown. I tested performance when listing only last 500 children. Then performance is acceptable but it gradually decreases the more children are listed in Dropdown. I use dropdown.AddOptions(names);
at the end of Start after the loop.
Have converted answer to comment as it seems pretty clear we don't have an answer yet!
So are you saying that performance of the code as shown is not actually poor? That it's only when you add the list to the dropdown that it goes slow?
If so then then you ought to tweak the question and title to reflect this because as things stand it appears to be about constructing the list.
Title updated to reflect this since the issue turned out to be with T$$anonymous$$P_Dropdown.
Ah, so what were you expecting the list of names to contain?
Component.name
gives you the name of the GameObject the component is on. So yeah, you'll get plenty of duplicates. Try logging the components' types using GetType() along with their names, and you'll see what I mean. Even the Transforms will cause the gameObject's name to be added to the list. $$anonymous$$aybe it's the types that you were after?
Basically, I would just need the components name as String. What I am trying to achieve is that every component name is available in a list. Then the user can select one of them from Dropdown menu, and then that child object is highlighted. I would just need its name in a String, so I can run a search function that finds that one part when its selected from Dropdown.
Answer by t-heo · Jan 17, 2020 at 01:40 PM
@Bonfire-Boy provided an answer; I was wrongly referencing components where I just wanted the child's name, hence tripling the amount of list items. It now works, although TMP Dropdown performance is quite poor on AR / Mobile when it has many options.
Your answer
![](https://koobas.hobune.stream/wayback/20220612221656im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Question about variabl lists 1 Answer
Add game building items to dropdown list? 1 Answer
How to make a combo box 4 Answers
Create Enumeration from List 0 Answers