- Home /
ScrollRect resetting scroll position on content change
Is there a way to tell ScrollRect not to reset scroll position when content changes? The scrollable content size changes during runtime as items are added and removed - problem is that the scroll position is reset whenever the content rect transform is changed. I want the scroll position to remain. Is there a way to do this?
I am also wondering this, did you solve it? I tried getting currentPos then applying currentPos within an event. But this caused the scrollRect to snap to top and then snap back to position... obviously not ideal
Answer by Brogan89 · Jul 25, 2017 at 02:33 AM
I found the problem. Having any kind of layout group of the view port will change the behaviour when objects are added to the content pragmatically. Also making sure the Contents anchor points and pivot are set correctly. For a Vertical scrollRect you want them to be like this
What OP described seems to be default behavior in Unity 2021 and if you don't want the Scroll View to remember the position, then you have to use scrollView.GetComponent<ScrollRect>().verticalNormalizedPosition = 1f;
(1=top, 0=bottom). Btw, the pivot is set to x=0 & y=1 by default for all of my "content" objects and I haven't noticed any problems with wrong alignment so far.
Answer by vverma9 · Jul 16, 2018 at 11:32 AM
This worked for me, attached this to the scrollbar
void OnEnable()
{
StartCoroutine(resetScrollPos());
}
IEnumerator resetScrollPos()
{
yield return null; // Waiting just one frame is probably good enough, yield return null does that
gameObject.GetComponent<Scrollbar>().value = 1;
}
Answer by steril · Jul 05, 2021 at 03:11 PM
Create a ScrollRectExtensions class
public static class ScrollRectExtensions
{
public static float GetValue(this ScrollRect scrollRect, float value)
{
return scrollRect.horizontal ?
scrollRect.horizontalNormalizedPosition :
scrollRect.verticalNormalizedPosition;
}
public static void SetValue(this ScrollRect scrollRect, float value)
{
if (scrollRect.horizontal)
{
scrollRect.horizontalNormalizedPosition = value;
}
else
{
scrollRect.verticalNormalizedPosition = value;
}
}
}
In your code, call:
void Awake()
{
StartCoroutine(FixScrollRects());
}
IEnumerator FixScrollRects()
{
yield return new WaitForEndOfFrame();
yield return new WaitForEndOfFrame();
foreach (var scrollRect in GetComponentsInChildren<ScrollRect>())
{
scrollRect.SetValue(0);
}
}
Double WaitForEndOfFrame(); was necessary in my case. (2019.3.1f)
Answer by Pundek · Nov 25, 2016 at 03:15 PM
did you solve the problem?
hahah i just found a solution:`scroll.verticalNormalizedPosition = 1f;`
setting verticalNormalizedPosition worked for me; thanks!
note, per the docs:
The vertical scroll position is a value between 0 and 1, with 0 being at the bottom.
Answer by yusuf91 · Mar 17 at 03:11 PM
I have the same issue, the images in my content reset when I pressed the play button, I use a VerticalLayyoutGroup, can anymore give me the solution, please? THX
Why are you posting this as an answer? This doesn't answer anything and should be a comment!
Your answer
Follow this Question
Related Questions
Move Dropdown using arrow buttons 0 Answers
Scrollbar touch sensitivity 0 Answers
How to control scrolling speed of a scrollbar? 0 Answers
Scrollbar scrolling in the opposite direction 1 Answer
How can I create a Multi line Input Field with Scrollbar? 0 Answers