- Home /
I need to instantiate a bunch of objects without performance dips
I just need to make a very very long, scrollable list. I want to use the grid layout group to do so. I can technically create a list of tens of thousands of ojects but then performance drops considerably which is pointless since only about 18 items would ever be on screen at once anyways. Does anyone have a solution for this? It could use draw distance or chunk loading or something, either one would be fine, the problem is I have absolutely no idea how to do either of those, which is why I'm asking for help. Anyone? Please?
My first thought was object pooling ie create the items up front and use as required.
However, you're talking about big big numbers.
You're probably best off, if you only have 18 list items on screen at once, only creating 18 of them and re-populating their contents as required when your backing data changes. Hard to say more without knowing more about your use-case.
Answer by Pangamini · Sep 16, 2021 at 11:24 AM
If your items are all of the same size, or their position is otherwise predictable, then you could indeed just create a scrollview with large but empty content, and only populate the visible part. My approach would be
Determine the visible area of the scrollview content
Determine the range of visible items.
Put visible UI items in their correct positions. Feel free to pool and reuse the items.
Profit
You will need to figure out the correct time when to update your list content, as it depends on the already updated scroll position. You may need to recalculate the canvas layout.... you will figure it out
But is there a way I can use the camera position as a trigger or something? Like, how does the code know when to instantiate or destroy my game objects? How will it tell when I need more objects? I already know that I'm going to need to use the update function to make the changes, but I don't know what code to use.