Change the "reference position" of an elastic scroll rect
Hello,
I meet a problem with a Unity ScrollRect :
I have a looooong item list hidden with a Mask. At the beginning, the local position of the ScrollRect content (my item bar) is Pos X=0.
If the user scroll to left to see the hidden items, the movement is Unrestricted
If the user scroll to right before the first item (where there is nothing to see), the movement switch to Elastic then the bar reset its position to 0 with a nice elastic movement.
The problem comes on the other side, when the user scroll to left after the last item (where there is nothing to see). The movement switch to Elastic, but I would like the bar to reset its position not to 0, but to the maximum allowed position I set to turn the movement to Elastic (so not reset to the first item but stay on the last item).
I can't find a simple way to do this. The documentation says :
"Use Elastic or Clamped to force the content to remain within the bounds of the Scroll Rect"
But I am not sure about what it's mean and if there is a simple way to control this. How should I deal with this ?
Answer by mdunstetter · Mar 08, 2016 at 03:18 PM
Ok, I didn't find any simple solution to this porblem. By "simple" I mean something that you do easily with the editor and a few lines of code.
So this is the approach I eventually use :
No switch to the "Elastic" movement
A custom code to make the elastic effect on each side, using a slerp function
/** * Let's say that as long as you are touching the screen, you can scroll * (just slow down the speed when you are out of bounds). * This piece of code show what happen when the screen is not touched * and we are out of bounds on one of the side. */ float vSpeed = 10.0f; if (vPosX > _maxPosScrollable) // Out of bounds { if( vPosX -_maxPosScrollable <= 0.05 ) // consider the movement finished { _scrollableRect.localPosition = new Vector3 (0.1f, 0f, 0f); _scrollRect.velocity = Vector2.zero; // stop the scroll } else // the elastic effect { vSlerp = Vector3.Slerp(vX, vTarget, Time.deltaTime * vSpeed); vPosX = vSlerp.x; // set the current position of the bar with a slerp between the previous position and the wanted position } }
Something like this, adapted to your own case (I guess there is a way to do something cleaner but I didn't have more time to spend on this).
If someone need this one day, I can give more precisions.
Your answer
Follow this Question
Related Questions
Force a uGUI scrollbar/scrollrect to stay at the bottom when the scrollrect is expanded 0 Answers
Unity Button to switch Bool 1 Answer
How to prevent automatic tmp ugui resizing. 0 Answers
Destroying object multiple times. 0 Answers
Unity (default) Fonts going pink in build (pink squares!!!!) 0 Answers