Scroll Rect: how to detect if the scroll reach to the end of the list
How can I know exactly when the scrolling reach to its both ends? I am using a horizontal scroll and I though I can give a trigger depend on the scroll content's position but it was not work out. Can anyone help me??
I tried call this from Scroll Rect On Value Change and On Drag End. But does not work.
public void OnDragScroll()
{
if (scrollRect.horizontalNormalizedPosition.Equals(0.0f))
{
leftArrowOverLay.SetActive(false);
}
else
{
leftArrowOverLay.SetActive(true);
}
if(scrollRect.horizontalNormalizedPosition.Equals(1.0f))
{
rightArrowOverLay.SetActive(false);
}
else
{
rightArrowOverLay.SetActive(true);
}
}
Hi @ElaxomoeR
Your question is bit vague - what do you mean by "when the scrolling reach to its both ends?" - Do you mean that you want something to happen, when user scrolls to either end of some list?
Also, what did you actually try? Show the code what didn't work maybe?
Did you also check the manual? ScrollRect has many methods and values that you could use. For example, verticalNormalizedPosition. This could be used to detect if you are at either end, combine this with it's velocity property, end drag etc?
So - someone could possibly help you, if you just explain what you have already done and what didn't work.
Also, using question heading "Scroll Rect Things ......." - what does "things" tell and why the dots?
Sorry about the title. I just getting annoying on the scroll rect and wrote it down without any purpose. And, yes I want to happen something if the scroll reach either end of the list. Yap, also tried the verticalNormalizedPosition. When I use that, it does not give exact answer. It is keep changing 1 and 0. I updated the question :)
What is leftArrowOverLay and rightArrowOverLay mean?Am new to the unity.,What does the leftArrowOverLay and rightArrowOverLay? Am very new to unity. If my question looks very stupid pardon me. @eses
They are just variables referencing GameObjects. ElaxomoeR wanted to enable/disable the arrows according to the position of the scrollbar, but you can run the code you want in the conditions.
Answer by eses · Sep 26, 2018 at 12:02 PM
Hi @ElaxomoeR
Don't expect the value of ScrollRect to be exactly something, ScrollRect might stop scrolling, and it's value might be 0.01... or 0.98 for example.
So the position won't equal something, it's either less or more than something.
So try this to check if your Scroll Rect is at the end part of scrolling:
if (scrollRect.verticalNormalizedPosition >= 0.95f) // scrolled near end
And to check beginning part of Scroll Rect:
else if (scrollRect.verticalNormalizedPosition <= 0.05f) // scrolled near start
I think you should definitely call it from ScrollRect.onValueChanged listener, as dragging might end way before scrolling of content stops... I think.
"Yap, also tried the verticalNormalizedPosition. When I use that, it does not give exact answer. It is keep changing 1 and 0. "
It's a float value, and it goes from 0-1.
Thank you! It works perfectly now.
if (scrollRect.horizontalNormalizedPosition <= 0.05f)
{
leftArrowOverLay.SetActive(false);
}
else
{
leftArrowOverLay.SetActive(true);
}
if(scrollRect.horizontalNormalizedPosition >= 0.95f)
{
rightArrowOverLay.SetActive(false);
}
else
{
rightArrowOverLay.SetActive(true);
}
By calling it from onValueChanged listener.
Your answer
