Clamping ScrollRect to Dynamically Created Content

So the title pretty well says it all, currently I’m trying to clamp my scroll rect to never scroll beyond the content that is there. The issue I’ve come into when trying to use something like a scrollbar, it doesn’t do anything because all of the content is created at runtime.

Are there settings on the scrollbar that I can set based on how many items there are? That is being kept track of in code, but I unfortunately can’t seem to figure this out.

In case the question wasn’t clear:

Are there any settings in Scrollbar that I can use with dynamically created content to not scroll past the content on screen?

The way I do it is that I use a Horizontal/vertical layout group so all my items are nicely placed. But the problem is the container size since the more you add, the smaller they get.

So the trick is to reset the container size based on how many items you need to place. The code we use is a little long so here is just the idea:

  1. Get how many items
  2. Get the relevant dimension from the prefab (height if vertical, width in horizontal)
  3. Extend the size of the container to the appropriate size in height/width
  4. Parent all items
  5. LayoutGroup will place them correctly
  6. Disable Layout group as it takes quite a bit of resources (it won’t affect the layout anymore if you do not add new items)
  7. Enjoy my greatfulness (somehow if you get it running at first)