- Home /
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?
@Andrew_Best did you solve this problem? I'd be happy to know how you did it.
Answer by fafase · Nov 03, 2015 at 09:11 AM
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:
Get how many items
Get the relevant dimension from the prefab (height if vertical, width in horizontal)
Extend the size of the container to the appropriate size in height/width
Parent all items
LayoutGroup will place them correctly
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)
Enjoy my greatfulness (somehow if you get it running at first)
Your answer
![](https://koobas.hobune.stream/wayback/20220613174928im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Scroll Rect Min/Max Size 0 Answers
Where can I edit UI.ScrollView? 0 Answers
ScrollRect scrollbar drag being overridden and stuttering to top 0 Answers
How to keep the scroll bar handle at a fixed size? 1 Answer
ScrollRect - Scroll outside the rect!? 0 Answers