user9011142
user9011142

Reputation:

Instantiating too many prefabs in the game makes it slow

So for my project, I need to instantiate one single prefab exactly 921 time. All of the instantiated prefab would end up into a vertical scrollview, so the use could go through them. Problem is, this will make the game extremely slow. How can I code it in a way that at first only 10 prefabs get instantiated, and the rest get instantiated only if the user has scrolled already to the bottom of the screen?

Upvotes: 1

Views: 403

Answers (1)

Scornz
Scornz

Reputation: 422

Yes, adding 921 things to a UI ScrollView is not normally a good plan.

However, as you suggested there is a way to solve this. I would suggest viewing the documentation for ScrollView (attached here). A property called scrollOffset returns a Vector2 of the current scroll position. Since you have a vertical ScrollView getting the magnitude of this would get "how far" the user has scrolled.

Suppose each element in your list has an arbitrary height of 50 (just throwing an example number out here). Instantiate your 10 list items, and every time scrollOffset increases by a magnitude of 50, call a method to instantiate another list item.

Upvotes: 0

Related Questions