I have some GameObjects inside of a ScrollRect (really inside of a content object which the ScrollRect translates).
When the localPosition of the content object is moved, the child objects move as expected but the horizontal divider present at the bottom of each child object doesn't appear in a constant way. Sometimes the lines appear thicker or thinner and "shimmer" while scrolling.
I'd like the horizontal lines not to visibly change. Any ideas on how to do it?
I ran into this question while trying to solve a similar issue myself, so I figured I'd share my solution.
Assuming you want to restrict a ScrollRect to whole pixels, you can do something like this:
Attach this to a GameObject and use it the same way you'd use a ScrollRect. This also assumes that every UI unit is one pixel; you'll have to scale
normalizedPixel
if that isn't the case for your game.This works because ScrollRects can move their
content
RectTransforms up todiff
pixels away from their local-space origin. If ScrollRects used pixel values we could just round those to ints, but since ScrollRects deal in normalized positions, we need to normalize our desired pixel values. If, in "pixel space," you move from 0 todiff
in increments of 1, in "normalized space" you need to move from 0 to 1 in increments of 1/diff
. (You're dividing the entire space bydiff
.) Therefore we need to make sure our normalized value is rounded to the nearest multiple of 1/diff
.