Cenkisabi
Cenkisabi

Reputation: 1076

Change scrollview position from code while scrolling

I have a problem with Unity's ScrollRect.

I want to make a infinite scrollview with object pooling. While user scrolling a scrollview, I delete unseen objects from list. And I'm using vertical layout system on my 'content' object with upper-center child allignment. So I have to reset the Y position of content.

My problem is; while user scrolling if I delete an object and reset the local position, on next OnDrag method of scrollview it continous from previous position. Let me explain with an example.

Let's say, rectTransform's current y position is 120. I deleted first child from this transform, and set new y position to 120 - 100 = 20. (100 is height of deleted child. So other objects will stay where they are.) But if I continue to scrolling up, the position of rectTransform in next frame is 121-122 etc.

I looked to UI's ScrollRect.cs class. It set's m_contentStartPosition on OnBeginDrag method and set content's position on OnDrag according to its start position. So I need to set this m_contentStartPosition again when I reset the y position of rectTransform. How can I achieve this. Or is there any other way to overcome this problem?

Upvotes: 1

Views: 5839

Answers (1)

Greg Lukosek
Greg Lukosek

Reputation: 1792

Technically you don't need to reset the scroll position when you scroll and update the content. Make sure your layout works correctly and scroll rect content rect transform is changing the size when you spawn new elements.

The normalized scroll position of the content updates automatically.

Upvotes: -1

Related Questions