- Home /
How to make sure that after adding an element to the scrollRect, the displayed part of the content does not change?
I have a scrollRect that contains more content than my device can display at a time. And this content is replenished. When I look at content that has been added a long time ago and at that moment a new element is added to the scroll, then what I see is changing. I do not like it. I want that despite the addition of new elements, the displayed content does not change.
I tried before adding an element to remember the entire height of the scroll, and after adding, to determine the difference in height and move the position of the scroll. But for some reason, before and after the addition, the height is the same. Although in the unity itself, I see that the height changes after adding.
public void AddItem()
{
var content = _scroll.viewport.transform.GetChild(0).GetComponent<RectTransform>();
var oldHeight = _content.rect.height;
...
var newHeight = _content.rect.height;
var difference = newHeight - oldHeight;
_content.anchoredPosition -= new Vector2(0, difference);
}
Variables oldHeight and newHeight are equal. Why? Why doesn't the height change in the code?