- Home /
Dynamic Scrollview elements without layout group?
Hi,
First, I already have my dynamic scrollviews working, but they are slow. I add all of the elements on Awake(), and from that point on the list does not change. I used a Horiz Layout Group and ContentSizeFitter to do this.
In a Unity optimization video Ian says Layout Group are really bad for performance as they aggressively dirty elements and include excessive GetComponent calls each frame. He recommends building your own layout system if possible. So that's what I'm trying to do. I've removed the layout group, layout elements, and content size fitter.
So far I've got the buttons being added to the content GO and positioned correctly. After adding and moving them, I try to resize the content rect:
float posX = 0f;
float width = buttons[0].rtrans.rect.width;
for (int i = 0; i < buttonCount; i++)
{
buttons[i].rtrans.anchoredPosition += new Vector2(posX, 0f);
posX += width;
}
content.rect.Set(0f, 0f, width * buttonCount, buttons[0].rtrans.rect.height)
That doesn't work as expected. I'm getting really weird results. The content rect left and right are identical, and the top and bottom are also identical. Basically the rect has no space, so scrolling can't happen. Even when I fix it in the inspector at runtime, the numbers don't seem to make sense.
Has anybody done this?
EDIT - realized the content anchor mode was stretch, so I changed it to top left. Didn't help much though. The content rect is still weirdly offscreen.
Thanks